代码仓库:https://github.com/PHPOffice/PhpSpreadsheet
1、进入/dayrui目录
2、使用命令行安装类
composer require phpoffice/phpspreadsheet
也可以使用宸逸的第三方类仓库:https://gitee.com/dayrui/composer,将下载的文件放到dayrui/Vendor/目录中。
3、需要将vendor目录命名为Vendor(首字母大写)
4、新建控制器文件 :
dayrui/App/Demo/Controllers/Excel.php
'111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'] ]; $title = ['第一行标题', '第二行标题']; // Create new Spreadsheet object $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getColumnDimension('A')->setWidth(50);// 给A列设置宽度 $sheet->getColumnDimension('B')->setAutoSize(true); // 给B列设置自动宽度 $sheet->getRowDimension('1')->setRowHeight(30);// 给第一行设置行高,一般第一行是字段名称 // 设置第一行背景和居中等 $abc = array('A', 'B', 'C', 'D', 'E', 'F', 'G'); // 有多少列就列多少字母 foreach ($abc as $a) { $sheet->getStyle($a.'1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setRGB("efefef"); $sheet->getStyle($a.'1')->getFont()->setBold(true); $sheet->getStyle($a.'1')->getFont()->setSize(14); // 第一行居中 //$sheet->getStyle($a.'1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); // 第一行居中对齐 $sheet->getStyle($a.'1')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); } $sheet->setTitle('导出结果');// 工作表格名称 // 方法一,使用 setCellValueByColumnAndRow //表头 //设置单元格内容 foreach ($title as $key => $value) { // 单元格内容写入 $sheet->setCellValueByColumnAndRow($key + 1, 1, $value); } $row = 2; // 从第二行开始 foreach ($data as $item) { $column = 1; foreach ($item as $value) { // 单元格内容写入 $sheet->setCellValueByColumnAndRow($column, $row, $value); $column++; } $row++; } // Redirect output to a client’s web browser (Xlsx) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="01simple.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); exit; } // 导出 方法二,使用 setCellValue public function index2() { $data = [ ['title1' => '111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'] ]; $title = ['第一行标题', '第二行标题']; // Create new Spreadsheet object $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); // 方法二,使用 setCellValue //表头 //设置单元格内容 $titCol = 'A'; foreach ($title as $key => $value) { // 单元格内容写入 $sheet->setCellValue($titCol . '1', $value); $titCol++; } $row = 2; // 从第二行开始 foreach ($data as $item) { $dataCol = 'A'; foreach ($item as $value) { // 单元格内容写入 $sheet->setCellValue($dataCol . $row, $value); $dataCol++; } $row++; } // Redirect output to a client’s web browser (Xlsx) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="01simple.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); exit; } // excel文件读取 public function read() { $file = "excel.xls"; // 这里写路径 $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file); $sheet = $spreadsheet->getActiveSheet(); $res = []; // 读取excel文件表格 foreach ($sheet->getRowIterator(1) as $row) { $tmp = []; foreach ($row->getCellIterator() as $cell) { $tmp[] = $cell->getFormattedValue(); } $res[$row->getRowIndex()] = $tmp; } print_r($res); } }
5、访问url,即可下载excel文件
index.php?s=demo&c=excel&m=index
具体参数配置参考PhpOffice官方的文档介绍
https://phpspreadsheet.readthedocs.io/