隨著網路的快速發展,Excel已成為公司和個人日常辦公室中重要的工具之一。因此,Excel導入導出的功能已經成為許多應用程式的必要組成部分。如何使用ThinkPHP6實作Excel導入導出呢?下面,本文將為您詳細介紹。
一、ThinkPHP6系統環境驗證
使用ThinkPHP6實作Excel導入匯出,首先需要滿足系統環境需求。在ThinkPHP6中,可以使用composer安裝phpoffice/phpspreadsheet函式庫,實作Excel處理功能。在命令列中執行以下命令進行安裝:
composer require phpoffice/phpspreadsheet
安裝完畢後,在controller層中引入PhpOfficePhpSpreadsheetSpreadsheet和PhpOfficePhpPhpOfficePhplssheetWriterSpcxWriter庫將在我們的後面程式碼中使用。
二、Excel匯出
- 匯出Excel資料
#使用ThinkPHP6實作Excel匯出,首先需要將資料匯入Excel。我們可以根據需要,在controller層中編寫相應的程式碼。例如,在匯出學生資訊時,可以透過查詢資料庫取得相關資訊。
use appcommonmodelStudent; public function export() { $students = Student::all(); }
- 設定Excel表格的頭部
Excel的表格頭部是非常重要的一部分。為了使Excel表格的頭部清晰明了,我們可以編寫程式碼來產生表格的頭部資訊。在下面的程式碼中,我們使用了循環來實現。
$spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('学生信息表'); $sheet->setCellValue('A1', '序号'); $sheet->setCellValue('B1', '学号'); $sheet->setCellValue('C1', '姓名'); $sheet->setCellValue('D1', '性别'); $sheet->setCellValue('E1', '日期');
- 將資料寫入Excel表格
接下來,將取得的資料寫入Excel表格中。循環遍歷數據,將每個學生的資料依序寫入表格中。
foreach ($students as $key => $item) { $num = $key + 2; $sheet->setCellValue('A' . $num, $num - 1); $sheet->setCellValue('B' . $num, $item->number); $sheet->setCellValue('C' . $num, $item->name); $sheet->setCellValue('D' . $num, $item->sex); $sheet->setCellValue('E' . $num, $item->date); }
- 將Excel表格輸出並儲存到本機
最後,將產生的Excel表格輸出並儲存到本機。在下面的程式碼中,我們使用writer來將Excel表格儲存為.xlsx格式並輸出到瀏覽器中。
$writer = new Xlsx($spreadsheet); $filename = "学生信息表.xlsx"; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename=' . $filename); header('Cache-Control: max-age=0'); $writer->save('php://output'); exit();
三、Excel導入
除了匯出Excel,ThinkPHP6還可以實現Excel的導入功能。接下來,我們將為您介紹ThinkPHP6中實作Excel導入的方法。
- 上傳Excel檔案並讀取資料
在匯入Excel之前,我們需要先上傳Excel檔案。在這裡,我們使用了ThinkPHP6自備的文件上傳類別庫。上傳成功之後,使用PhpOfficePhpSpreadsheetIOFactory類別庫中的方法,讀取上傳的Excel檔案中的資料。
use thinkacadeFilesystem; $uploadFile = request()->file('file'); $saveName = Filesystem::disk('public')->putFile('excel', $uploadFile); $filePath = Filesystem::disk('public')->path($saveName); $spreadsheet = PhpOfficePhpSpreadsheetIOFactory::load($filePath); $sheet = $spreadsheet->getActiveSheet(); $highestRow = $sheet->getHighestRow(); $data = [];
- 遍歷Excel表格並得到資料
取得到Excel表格資料後,我們需要將資料遍歷一遍,使用陣列保存Excel表格的每一行資料。
for($i = 2; $i <= $highestRow; $i++){ $item['number'] = $sheet->getCellByColumnAndRow(1, $i)->getValue(); $item['name'] = $sheet->getCellByColumnAndRow(2, $i)->getValue(); $item['sex'] = $sheet->getCellByColumnAndRow(3, $i)->getValue(); $item['date'] = date('Y-m-d H:i:s',$sheet->getCellByColumnAndRow(4, $i)->getValue() - 25569)*86400; $data[] = $item; }
- 將Excel表格匯入到資料庫中
最後,我們將Excel表格中的資料匯入到資料庫中。在這裡,我們使用ThinkPHP6自帶的ORM操作,將資料保存到資料庫表中。
use appcommonmodelStudent; Db::startTrans(); try { Student::insertAll($data); Db::commit(); } catch (Exception $e) { Db::rollback(); return json(['code' => '500', 'msg' => '操作失败']); } return json(['code' => '200', 'msg' => '操作成功']);
總結
本文詳細介紹如何使用ThinkPHP6實作Excel匯入匯出功能。透過使用PhpOfficePhpSpreadsheet類別庫,我們可以輕鬆地完成Excel相關的操作。 Excel導入匯出功能在企業管理系統中用到非常廣泛,理解和掌握相關技能將對開發者有很大的幫助。
以上是如何使用ThinkPHP6實作Excel導入導出的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

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

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