這篇文章帶給大家的內容是關於phpspreadsheet匯出資料到Excel的方法介紹(程式碼範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
之前我們使用PHP導出Excel資料時使用的是PHPExcel函式庫,但是phpoffice已經官方宣布PHPExcel已經被廢棄不在維護,推薦使用phpspreadsheet,如下圖所示
#我們可以透過composer安裝phpspreadsheet
composer require phpoffice/phpspreadsheet
使用phpspreadsheet匯出學生資訊到瀏覽器程式碼範例
/** * 输出到浏览器(需要设置header头) * @param string $fileName 文件名 * @param string $fileType 文件类型 */ function excelBrowserExport($fileName, $fileType) { //文件名称校验 if(!$fileName) { trigger_error('文件名不能为空', E_USER_ERROR); } //Excel文件类型校验 $type = ['Excel2007', 'Xlsx', 'Excel5', 'xls']; if(!in_array($fileType, $type)) { trigger_error('未知文件类型', E_USER_ERROR); } if($fileType == 'Excel2007' || $fileType == 'Xlsx') { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"'); header('Cache-Control: max-age=0'); } else { //Excel5 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$fileName.'.xls"'); header('Cache-Control: max-age=0'); } } $data = [ [1, 'jack', 10], [2, 'mike', 12], [3, 'jane', 21], [4, 'paul', 26], [5, 'kitty', 25], [6, 'yami', 60], ]; $title = ['id', 'name', 'age']; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); //设置工作表标题名称 $worksheet->setTitle('工作表格1'); //表头 //设置单元格内容 foreach ($title as $key => $value) { $worksheet->setCellValueByColumnAndRow($key+1, 1, $value); } $row = 2; //从第二行开始 foreach ($data as $item) { $column = 1; foreach ($item as $value) { $worksheet->setCellValueByColumnAndRow($column, $row, $value); $column++; } $row++; } $fileName = '学生信息'; $fileType = 'Xlsx'; //1.下载到服务器 //$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); //$writer->save($fileName.'.'.$fileType); //2.输出到浏览器 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); //按照指定格式生成Excel文件 excelBrowserExport($fileName, 'Xlsx'); $writer->save('php://output');
#匯出結果如圖所示:
以上是phpspreadsheet導出資料到Excel的方法介紹(程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

利用會話構建高效購物車系統的步驟包括:1)理解會話的定義與作用,會話是服務器端的存儲機制,用於跨請求維護用戶狀態;2)實現基本的會話管理,如添加商品到購物車;3)擴展到高級用法,支持商品數量管理和刪除;4)優化性能和安全性,通過持久化會話數據和使用安全的會話標識符。

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3漢化版
中文版,非常好用

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境