0 // 注释解开,并且换成一个空的 select 库,redis 库默认是 0-15 共 16 个库 ]; $redis = (new Redis($options))->getRedis(); $pool = new \Cache\Adapter\Redis\RedisCachePool($redis); $simpleCache = new \Cache\Bridge\SimpleCache\SimpleCacheBridge($pool); \PhpOffice\PhpSpreadsheet\Settings::setCache($simpleCache); } else if ($cache_type == 'file' && class_exists(FilesystemCachePool::class)) { // 将数据暂存磁盘,可以降低内存,但是导出速度会大幅下降 需要安装扩展包 composer require cache/filesystem-adapter $path = ROOT_PATH . 'runtime' . DS . 'export/'; @mkdir($path); $filesystemAdapter = new Local($path); $filesystem = new Filesystem($filesystemAdapter); $pool = new FilesystemCachePool($filesystem); \PhpOffice\PhpSpreadsheet\Settings::setCache($pool); } // 实例化excel $spreadsheet = new Spreadsheet(); // 初始化工作簿 $sheet = $spreadsheet->getActiveSheet(0); // 给表头设置边框 $sheet->getStyle('A1:' . $cellName[$cellNum - 1] . '1')->getFont()->setBold(true); // 表头 $i = 0; foreach ($expCellName as $key => $cell) { $sheet->setCellValue($cellName[$i] . '1', $cell); $i++; } } // for ($i = 0; $i < $cellNum; $i++) { // $sheet->getColumnDimension($cellName[$i])->setWidth(30); // } // 写入数据 for ($i = 0; $i < $dataNum; $i++) { if ($is_last_page && $i == ($dataNum - 1)) { $sheet->mergeCells('A' . ($i + $base_cell) . ':' . $cellName[$cellNum - 1] . ($i + $base_cell)); $sheet->setCellValue('A' . ($i + $base_cell), $expTableData[$i][key($expCellName)]); } else { $j = 0; foreach ($expCellName as $key => $cell) { $sheet->setCellValue($cellName[$j] . ($i + $base_cell), $expTableData[$i][$key]); $j++; } } } if ($is_last_page) { // ini_set('memory_limit', '256M'); ob_end_clean(); header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $fileName . '.xlsx"'); header("Content-Disposition:attachment;filename=$fileName.xlsx"); //attachment新窗口打印inline本窗口打印 $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); } } }