多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
​ ## 导入扩展: ~~~ 安装命令:composer require phpoffice/phpexcel ~~~ ## 核心代码: ~~~php /*导出*/ public function exports() { $adminList = Orders::all(); /*TODO 这个条件需要处理*/ $newExcel = new Spreadsheet(); //创建一个新的excel文档 $objSheet = $newExcel->getActiveSheet(); //获取当前操作sheet的对象 //设置宽度为true,不然太窄了 $newExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); //设置第一栏的标题 $objSheet ->setCellValue('A1', '订单号') ->setCellValue('B1', '金额'); foreach ($adminList as $k => $val) { $k = $k + 2; $objSheet ->setCellValue('A' . $k, $val['order_sn']) ->setCellValue('B' . $k, $val['total_amount']); } $this->downloadExcel($newExcel, date('YmsHis', time()) . time(), 'Xls'); } //公共文件,用来传入xls并下载 public function downloadExcel($newExcel, $filename, $format) { if ($format == 'Xlsx') { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); } elseif ($format == 'Xls') { header('Content-Type: application/vnd.ms-excel'); } header("Content-Disposition: attachment;filename=" . $filename . date('Y-m-d') . '.' . strtolower($format)); header('Cache-Control: max-age=0'); $objWriter = IOFactory::createWriter($newExcel, $format); $objWriter->save('php://output'); exit; } ~~~ ​