首頁  >  文章  >  php框架  >  如何使用ThinkPHP6實作Excel導入導出

如何使用ThinkPHP6實作Excel導入導出

WBOY
WBOY原創
2023-06-21 18:01:414666瀏覽

隨著網路的快速發展,Excel已成為公司和個人日常辦公室中重要的工具之一。因此,Excel導入導出的功能已經成為許多應用程式的必要組成部分。如何使用ThinkPHP6實作Excel導入導出呢?下面,本文將為您詳細介紹。

一、ThinkPHP6系統環境驗證

使用ThinkPHP6實作Excel導入匯出,首先需要滿足系統環境需求。在ThinkPHP6中,可以使用composer安裝phpoffice/phpspreadsheet函式庫,實作Excel處理功能。在命令列中執行以下命令進行安裝:

composer require phpoffice/phpspreadsheet

安裝完畢後,在controller層中引入PhpOfficePhpSpreadsheetSpreadsheet和PhpOfficePhpPhpOfficePhplssheetWriterSpcxWriter庫將在我們的後面程式碼中使用。

二、Excel匯出

  1. 匯出Excel資料

#使用ThinkPHP6實作Excel匯出,首先需要將資料匯入Excel。我們可以根據需要,在controller層中編寫相應的程式碼。例如,在匯出學生資訊時,可以透過查詢資料庫取得相關資訊。

use appcommonmodelStudent;

public function export()
{
    $students = Student::all();
}
  1. 設定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', '日期');
  1. 將資料寫入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);
}
  1. 將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導入的方法。

  1. 上傳Excel檔案並讀取資料

在匯入Excel之前,我們需要先上傳Excel檔案。在這裡,我們使用了ThinkPHP6自備的文件上傳類別庫。上傳成功之後,使用PhpOfficePhpSpreadsheetIOFactory類別庫中的方法,讀取上傳的Excel檔案中的資料。

use thinkacadeFilesystem;

$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 = [];
  1. 遍歷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;
}
  1. 將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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn