多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
>[danger] phpspreadsheet ``` composer require phpoffice/phpspreadsheet ``` [https://packagist.org/packages/phpoffice/phpspreadsheet](https://packagist.org/packages/phpoffice/phpspreadsheet) >[danger] 在安装前先查看当前环境 fileinfo 扩展有没有开启 如果没有开启`fileinfo`扩展安装该扩展库会报错 >[danger] 导出Excel表格封装 ```php use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; ``` ```php /** * 导出Excel表格 Xlsx格式(2007版) * * @author liang <23426945@qq.com> * @datetime 2019-12-22 * * @param array $title 表头单元格内容 * @param array $data 从第二行开始写入的数据 * @param string $path Excel文件保存位置,路径中的目录必须存在 * * @return null 没有设定返回值 */ private function excel($title = [], $data = [], $path = '') { // 获取Spreadsheet对象 $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); // 表头单元格内容 第一行 $titCol = 'A'; foreach ($title as $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++; } $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $result = $writer->save($path . '.xlsx'); } ``` >[danger] 调用示例 ``` $data = Db::table('ay_config')->select(); $title = ['id', '字段名', 'value值', '类型', '排序', '描述']; $this->excel($title, $data, $filename = './excel/03'); ```