欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > tp8读取mysql导出excel

tp8读取mysql导出excel

2025/1/31 14:10:24 来源:https://blog.csdn.net/quweiie/article/details/145311000  浏览:    关键词:tp8读取mysql导出excel

环境:php8.3, thinkphp8.0, mysql8.0

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use think\facade\Db;
use think\response\Json;class Index
{public function index(): Json{// 查询 MySQL 数据表数据$employees = Db::name('employee')->field('emp_name, emp_sex, emp_phone, create_time')->select()->toArray();// 创建 PhpSpreadsheet 对象$spreadsheet = new Spreadsheet();$sheet = $spreadsheet->getActiveSheet();// 设置表头$sheet->setCellValue('A1', '姓名');$sheet->setCellValue('B1', '性别');$sheet->setCellValue('C1', '电话');$sheet->setCellValue('D1', '日期');// 设置表头样式:居中对齐和加粗$headerStyle = ['font' => ['bold' => true,  // 加粗'size' => 12,    // 字体大小],'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER,  // 水平居中'vertical' => Alignment::VERTICAL_CENTER,      // 垂直居中]];// 应用样式到表头$sheet->getStyle('A1:D1')->applyFromArray($headerStyle);// 填充数据$rowNum = 2;  // 从第二行开始填充数据foreach ($employees as $employee) {$sheet->setCellValue('A' . $rowNum, $employee['emp_name']);$sheet->setCellValue('B' . $rowNum, $employee['emp_sex']);$sheet->setCellValue('C' . $rowNum, $employee['emp_phone']);$sheet->setCellValue('D' . $rowNum, $employee['create_time']);$rowNum++;}/*$writer = new Xlsx($spreadsheet);$writer->save('demo.xlsx');*/header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');header('Content-Disposition: attachment;filename="myfile.xlsx"');header('Cache-Control: max-age=0');$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');$writer->save('php://output');return json(['code'=>200,'msg'=>'success']);}
}
composer require phpoffice/phpspreadsheet

根据phpspreadsheet官方文档,写了上述代码,如有不懂的地方,请查阅官方文档。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com