💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
> phpspreadsheet ~~~ composer require phpoffice/phpspreadsheet ~~~ [https://packagist.org/packages/phpoffice/phpspreadsheet](https://packagist.org/packages/phpoffice/phpspreadsheet) > 在安装前先查看当前环境 fileinfo 扩展有没有开启 如果没有开启`fileinfo`扩展安装该扩展库会报错 > 导出Excel表格封装 ~~~ use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; ~~~ ~~~ /** * 导出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'); } ~~~ > 调用示例 ~~~ $data = Db::table('ay_config')->select(); $title = ['id', '字段名', 'value值', '类型', '排序', '描述']; $this->excel($title, $data, $filename = './excel/03'); ~~~