隨著互聯(lián)網(wǎng)的快速發(fā)展,excel已經(jīng)成為公司和個人日常辦公中重要的工具之一。因此,excel導(dǎo)入導(dǎo)出的功能已經(jīng)成為許多應(yīng)用程序的必要組成部分。如何使用thinkphp6實現(xiàn)excel導(dǎo)入導(dǎo)出呢?下面,本文將為您詳細介紹。
一、thinkphp6系統(tǒng)環(huán)境驗證
使用ThinkPHP6實現(xiàn)Excel導(dǎo)入導(dǎo)出,首先需要滿足系統(tǒng)環(huán)境要求。在ThinkPHP6中,可以使用composer安裝phpoffice/phpspreadsheet庫,實現(xiàn)Excel處理功能。在命令行中執(zhí)行以下命令進行安裝:
composer require phpoffice/phpspreadsheet
安裝完畢后,在controller層中引入PhpOfficePhpSpreadsheetSpreadsheet和PhpOfficePhpSpreadsheetWriterXlsx類庫,這些類庫將在我們的后面代碼中使用。
立即學(xué)習(xí)“PHP免費學(xué)習(xí)筆記(深入)”;
二、Excel導(dǎo)出
- 導(dǎo)出Excel數(shù)據(jù)
使用ThinkPHP6實現(xiàn)Excel導(dǎo)出,首先需要將數(shù)據(jù)導(dǎo)入到Excel中。我們可以根據(jù)需要,在controller層中編寫相應(yīng)的代碼。例如,在導(dǎo)出學(xué)生信息時,可以通過查詢數(shù)據(jù)庫獲取相關(guān)信息。
use appcommonmodelStudent; public function export() { $students = Student::all(); }
- 設(shè)置Excel表格的頭部
Excel的表格頭部是非常重要的一部分。為了使Excel表格的頭部清晰明了,我們可以編寫代碼來生成表格的頭部信息。在下面的代碼中,我們使用了循環(huán)來實現(xiàn)。
$spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('學(xué)生信息表'); $sheet->setCellValue('A1', '序號'); $sheet->setCellValue('B1', '學(xué)號'); $sheet->setCellValue('C1', '姓名'); $sheet->setCellValue('D1', '性別'); $sheet->setCellValue('E1', '日期');
- 將數(shù)據(jù)寫入Excel表格
接下來,將獲取的數(shù)據(jù)寫入Excel表格中。循環(huán)遍歷數(shù)據(jù),將每個學(xué)生的信息依次寫入表格中。
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 = "學(xué)生信息表.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導(dǎo)入
除了導(dǎo)出Excel,ThinkPHP6還可以實現(xiàn)Excel的導(dǎo)入功能。接下來,我們將為您介紹ThinkPHP6中實現(xiàn)Excel導(dǎo)入的方法。
- 上傳Excel文件并讀取數(shù)據(jù)
在導(dǎo)入Excel之前,我們需要先上傳Excel文件。在這里,我們使用了ThinkPHP6自帶的文件上傳類庫。上傳成功之后,使用PhpOfficePhpSpreadsheetIOFactory類庫中的方法,讀取上傳的Excel文件中的數(shù)據(jù)。
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表格并得到數(shù)據(jù)
獲取到Excel表格數(shù)據(jù)后,我們需要將數(shù)據(jù)遍歷一遍,使用數(shù)組保存Excel表格的每一行數(shù)據(jù)。
for($i = 2; $i 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表格導(dǎo)入到數(shù)據(jù)庫中
最后,我們將Excel表格中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫中。在這里,我們使用ThinkPHP6自帶的ORM操作,將數(shù)據(jù)保存到數(shù)據(jù)庫表中。
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' => '操作成功']);
總結(jié)
本文詳細介紹了如何使用ThinkPHP6實現(xiàn)Excel導(dǎo)入導(dǎo)出功能。通過使用PhpOfficePhpSpreadsheet類庫,我們可以輕松地完成Excel相關(guān)的操作。Excel導(dǎo)入導(dǎo)出功能在企業(yè)管理系統(tǒng)中用到非常廣泛,理解和掌握相關(guān)技能將會對開發(fā)者有很大的幫助。