使用ThinkPHP8實現(xiàn)導出Excel數(shù)據(jù)表格功能
1、開發(fā)版本
Think PHP8.0、PHP8.0,并非低版不能用,僅因本人當前版本如此。
部分參數(shù)需自行進行修改,具體查看執(zhí)行代碼.
Excel有默認的表格樣式,如需修改,根據(jù)實際應(yīng)用場景進行設(shè)置即可。
2、實現(xiàn)原理
1.安裝Spreadsheet
composer require phpoffice/phpspreadsheet
2.確定數(shù)據(jù)表頭
$header = [ ['key' => 'index', 'title' => '序號'], ['key' => 'activity_title', 'title' => '列1名稱'], ['key' => 'room_name', 'title' => '列2名稱'], ];
3.確定數(shù)據(jù)列
$list = []; // 定義數(shù)據(jù)內(nèi)容,根據(jù)實際應(yīng)用場景來寫即可。
4.調(diào)用封裝類,導出數(shù)據(jù)
3、核心代碼
1.調(diào)用示例
// 表頭 $header = [ ['key' => 'index', 'title' => '序號'], ['key' => 'activity_title', 'title' => '列1名稱'], ['key' => 'room_name', 'title' => '列2名稱'], ]; $list = []; // 實例化excel $sheet = new Spreadsheet(); // 實例化導出類 $export = new Excel($sheet, 0); // 設(shè)置單元格表頭 $export->setHeader($header); // 設(shè)置單元格數(shù)據(jù) $export->setContent($list, $header); // 導出:文件名稱、sheet名稱,返回結(jié)果為本地文件存儲路徑 $res = $export->export($fileName, $sheetName);
2.Excel核心控制器
<?php namespace app\common\controller; /** * @note Excel操作 */ class Excel { // 定義表格對象 protected object $sheet; public function __construct(object $sheet, $sheetIndex = 0) { $this->sheet = $sheet; if (!is_object($this->sheet)) $this->sheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $this->sheet->getActiveSheet($sheetIndex); } /** * @notes 設(shè)置表頭 * @param array $header 表頭數(shù)據(jù) * @param string|int $startRow 默認第一行 */ public function setHeader(array $header, string|int $startRow = 1): object { $header = array_values($header); // 計算總列數(shù) $column = $this->getColumn(count($header)); foreach ($header as $key => $value) { $columnName = $column[$key] . $startRow; // 設(shè)置單元格值 $this->sheet->getActiveSheet()->setCellValue($columnName, $value['title']); // 設(shè)置單元格自適應(yīng)寬度 $this->sheet->getActiveSheet()->getColumnDimension($column[$key])->setAutoSize(true); // 設(shè)置單元格自適應(yīng)高度 $this->sheet->getActiveSheet()->getRowDimension($startRow)->setRowHeight(24); } $startColumn = $column[0] . $startRow; $endColumn = $column[count($header) - 1] . $startRow; // 設(shè)置字體大小及加粗 $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getFont()->setBold(true)->setSize(12); // 設(shè)置單元格水平居中 $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); // 設(shè)置單元格垂直居中 $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); // 設(shè)置單元格邊框 $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN); return $this->sheet; } /** * @notes 設(shè)置單元格值 * @param array $data 數(shù)據(jù) * @param array $header 表頭數(shù)據(jù) * @param string|int $startRow 默認第二行開始 */ public function setContent(array $data, array $header, string|int $startRow = 2): object { // 獲取總列數(shù) $column = $this->getColumn(count($header)); // 遍歷數(shù)據(jù) foreach ($data as $key => $value) { // 遍歷表頭 for ($i = 0; $i < count($header); $i++) { // 獲取單元格名稱 $columnName = $column[$i] . ($key + $startRow); // 設(shè)置單元格值 $this->sheet->getActiveSheet()->setCellValue($columnName, $value[$header[$i]['key']] ?? ''); // 設(shè)置單元格自適應(yīng)寬度 $this->sheet->getActiveSheet()->getColumnDimension($column[$i])->setAutoSize(true); // 設(shè)置單元格自適應(yīng)高度 $this->sheet->getActiveSheet()->getRowDimension($key + $startRow)->setRowHeight(24); } } $startColumn = $column[0] . $startRow; $endColumn = $column[count($column) - 1] . count($data) + $startRow - 1; // 設(shè)置字體大小及加粗 $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getFont()->setBold(false)->setSize(11); // 設(shè)置單元格水平居中 $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); // 設(shè)置單元格垂直居中 $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); // 設(shè)置單元格邊框 $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN); return $this->sheet; } /** * @notes 導出數(shù)據(jù) * @param string $fileName 文件名 * @param string $sheetName 表名 * @return string */ public function export(string $fileName, string $sheetName = 'Sheet1'): string { // 設(shè)置表格標題 $this->sheet->getActiveSheet()->setTitle($sheetName); // 設(shè)置表格格式 $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->sheet); // 設(shè)置存儲路徑 $basePath = public_path(); $path = 'activity_sequence_template/'; $fullPath = $basePath . $path . $fileName . '.xlsx'; if (!is_dir($basePath . $path)) mkdir($basePath . $path, 0777, true); $writer->save($fullPath); return $path . $fileName . '.xlsx'; // // 設(shè)置響應(yīng)頭 // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"'); // header('Cache-Control: max-age=0'); // // 導出數(shù)據(jù) // $writer->save('php://output'); } /** * @notes 自動計算列數(shù) * @param int|string $colNumber * @return array */ protected function getColumn(int|string $colNumber = 1): array { // 生成A-Z的數(shù)組 $arr = range('A', 'Z'); // 計算循環(huán)次數(shù) $no = ceil($colNumber / count($arr)); // 定義數(shù)組 $data = []; if ($no <= 1) { for ($i = 0; $i < $colNumber; $i++) { $data[] = $arr[$i]; } } else { for ($i = 0; $i < count($arr); $i++) { $data[] = $arr[$i]; } for ($i = 0; $i < $colNumber - count($arr); $i++) { $list = (($i + count($arr)) % count($arr)); $data[] = $arr[ceil(($i + 1) / count($arr)) - 1] . $arr[$list]; } } return $data; } }
到此這篇關(guān)于使用ThinkPHP8實現(xiàn)導出Excel數(shù)據(jù)表格功能的文章就介紹到這了,更多相關(guān)ThinkPHP8導出Excel數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP學習筆記 (1) 環(huán)境配置與代碼調(diào)試
學習php第一步就是需要配置php運行環(huán)境,這個是基礎(chǔ),需要的朋友可以參考下。2011-06-06