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