本文所用的CodeIgniter框架版本为3.1.4,由于官方没有集成Composer,所以这里要先集成;
在项目根目录执行:
composer require phpoffice/phpspreadsheet:1.14
拓展安装后在application/libraries目录内创建Composer.php文件
application/libraries/Composer.php 文件内容示例:
<?php /** * CodeIgniter引入Composer * * @author barry */ class Composer { function __construct() { include("./vendor/autoload.php"); } }
config/autoload.php中的libraries数组追加一项: Composer(刚才创建的文件名)
$autoload['libraries'] = array('Composer', 'database');
接下来就可以正常使用了
phpspreadsheet导出示例代码:
if (!function_exists('spreadsheet_export')) { /** * 数据导出 * @param string $xlsx_name 表格标题 * @param array $ExportFields ['字段'=>'字段标题'] * @param array $ExportDatas 要导出的数量 * @return bool */ function spreadsheet_export($xlsx_name, $ExportFields, $ExportDatas) { ob_end_clean(); empty($xlsx_name) && $xlsx_name = '数据导出'; $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->setTitle('列表数据'); //设置表标题名称 $worksheet->setCellValueByColumnAndRow(1, 1, $xlsx_name); //表头标题 //设置单元格表头内容 foreach (array_values($ExportFields) as $key => $value) { $worksheet->getColumnDimensionByColumn($key + 1)->setWidth(20); $worksheet->setCellValueByColumnAndRow($key + 1, 2, $value); } // 创建 A~AZ 的数组 $AssignArr = []; for ($i = 'A'; $i != 'BA'; $i++) { $AssignArr[] = $i; } $rows = 3; $fields = array_keys($ExportFields); $CellEnd = $AssignArr[count($fields) - 1] ?? 'AZ'; //获取单元格最后一项位置 //合并单元格 $worksheet->mergeCells("A1:{$CellEnd}1"); foreach ($ExportDatas as $key => $value) { $worksheet->getColumnDimensionByColumn($rows)->setWidth(20); foreach ($fields as $fk => $field) { $worksheet->setCellValueByColumnAndRow($fk + 1, $rows, ($value[$field] ?? '')); } $rows++; } $styleArray = [ 'font' => [ 'bold' => true ], 'alignment' => [ 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, ], ]; //设置单元格样式 $worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28); $worksheet->getStyle("A2:{$CellEnd}2")->applyFromArray($styleArray)->getFont()->setSize(12); //添加所有边框/居中 $worksheet->getStyle("A1:{$CellEnd}{$rows}")->applyFromArray([ 'borders' => [ 'allBorders' => [ 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, 'color' => ['argb' => '666666'], ], ], 'alignment' => [ 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, ], ]); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="' . $xlsx_name . '.xlsx"'); header('Cache-Control: max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); } }
使用示例:
// 表格列 $ExportFields = [ 'user_name' => '姓名', 'user_phone' => '电话', 'create_time' => '创建时间', ]; // 列表数据 $ExportDatas = [ [ 'user_name' => '张三', 'user_phone' => '12345678911', 'create_time' => '2021-12-15 12:48:47', ], [ 'user_name' => '李四', 'user_phone' => '12345678911', 'create_time' => '2021-12-15 12:48:47', ] ]; spreadsheet_export('列表数据导出', $ExportFields, $ExportDatas);
关于 phpoffice/phpspreadsheet 导出的xlsx文件无法打开的解决办法:
在导出输出内容前需要清空缓冲区,否则会造成导出的文件无法打开或者打开后是乱码的情况;
转载请注明本文标题和链接:《 CI框架3 使用 phpspreadsheet 导出数据 》
网友评论 0