1. 说明:tp6导入数据,使用phpoffice/phpspreadsheet ,至于详细使用方法请看上一篇tp6导入Excel表格
2.具体方法内容如下,如果是tp6应该是可以直接复制粘贴,只把自己相应的参数改一下就可以了,具体代码如下
public function exportExcel()
{
// 查询要导出的数据
$model = new ErrorModel();
$data = $model
->field('id,api_member_id,title,Chinese,English,example,translate,desc')
->select();
// 实例化
$spreadsheet = new Spreadsheet();
// 获取活动单元格
$sheet = $spreadsheet->getActiveSheet();
// 获取单元格
$cellA = $sheet->getCell('A1');
// 设置单元格的值
$cellA->setValue('ID');
// 设置 A 列 列宽
$sheet->getColumnDimension('A')->setWidth(10);
// 设置第一行 行高
$sheet->getRowDimension(1)->setRowHeight(20);
$cellB = $sheet->getCell('B1');
$cellB->setValue('记录人');
$sheet->getColumnDimension('B')->setWidth(20);
$cellC = $sheet->getCell('C1');
$cellC->setValue('题目名称');
$sheet->getColumnDimension('C')->setWidth(10);
$cellD = $sheet->getCell('D1');
$cellD->setValue('中文');
$sheet->getColumnDimension('D')->setWidth(20);
$cellE = $sheet->getCell('E1');
$cellE->setValue('英文');
$sheet->getColumnDimension('E')->setWidth(40);
$cellE = $sheet->getCell('F1');
$cellE->setValue('其他');
$sheet->getColumnDimension('F')->setWidth(40);
$cellF = $sheet->getCell('G1');
$cellF->setValue('例句');
$sheet->getColumnDimension('G')->setWidth(30);
$cellG = $sheet->getCell('H1');
$cellG->setValue('例句翻译');
$sheet->getColumnDimension('H')->setWidth(30);
$cellH = $sheet->getCell('I1');
$cellH->setValue('注释');
$sheet->getColumnDimension('I')->setWidth(30);
// 设置样式 标题
$styleArray = [
'alignment' => [
'horizontal' => 'center', //水平居中
'vertical' => 'center', //垂直居中
],
'font' => [
'name' => '黑体',
'bold' => false,
'size' => 10
]
];
// 设置样式 正文
$styleArrayBody = [
'alignment' => [
'horizontal' => 'center', //水平居中
'vertical' => 'center', //垂直居中
],
'font' => [
'name' => '宋体',
'bold' => false,
'size' => 10
]
];
// 应用样式
$sheet->getStyle('A1')->applyFromArray($styleArray);
$sheet->getStyle('B1')->applyFromArray($styleArray);
$sheet->getStyle('C1')->applyFromArray($styleArray);
$sheet->getStyle('D1')->applyFromArray($styleArray);
$sheet->getStyle('E1')->applyFromArray($styleArray);
$sheet->getStyle('F1')->applyFromArray($styleArray);
$sheet->getStyle('G1')->applyFromArray($styleArray);
$sheet->getStyle('H1')->applyFromArray($styleArray);
$sheet->getStyle('I1')->applyFromArray($styleArray);
// 从 A2 开始填充数据
foreach ($data as $k => $v) {
$n = $k + 2;
// 获取单元格
$cellA = $sheet->getCell('A' . $n);
// 设置单元格的值
$cellA->setValue($v['id']);
$cellB = $sheet->getCell('B' . $n);
$cellB->setValue('api_member_id');
$cellC = $sheet->getCell('C' . $n);
$cellC->setValue($v['title']);
$cellD = $sheet->getCell('D' . $n);
$cellD->setValue($v['Chinese']);
$cellD = $sheet->getCell('E' . $n);
$cellD->setValue($v['English']);
$cellD = $sheet->getCell('F' . $n);
$cellD->setValue($v['else']);
$cellE = $sheet->getCell('G' . $n);
$cellE->setValue($v['example']);
$cellF = $sheet->getCell('H' . $n);
$cellF->setValue($v['else']);
$cellG = $sheet->getCell('I' . $n);
$cellG->setValue($v['desc']);
}
$file_name = '导出数据.xlsx';
// 实例化导出类
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition:attachment;filename=' . $file_name);
header('Cache-Control:max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
}