想用TP6使用phpspreadsheet但是说我PHP版本低,原来是PHP7.0 composer要求至少7.4
直接修改环境变量,把PHP目录切换到7.4
composer升级比较简单,在PHP目录下CMD然后官网的命令执行下即可
下面就可以在TP根目录下执行命令安装PHPSPREADSHEET
程序就简单了
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet = new Spreadsheet();
// 获取活动工作薄
$sheet = $spreadsheet->getActiveSheet();
// 获取单元格
$cellA = $sheet->getCell('A1');
// 设置单元格的值
$cellA->setValue('姓名');
// 设置 A 列 列宽
$sheet->getColumnDimension('A')->setWidth(30);
// 设置第一行 行高
$sheet->getRowDimension(1)->setRowHeight(20);
$cellB = $sheet->getCell('B1');
$cellB->setValue('手机号');
$sheet->getColumnDimension('B')->setWidth(30);
$cellC = $sheet->getCell('C1');
$cellC->setValue('学历');
$sheet->getColumnDimension('C')->setWidth(30);
$cellD = $sheet->getCell('D1');
$cellD->setValue('考试目的');
$sheet->getColumnDimension('D')->setWidth(30);
$cellE = $sheet->getCell('E1');
$cellE->setValue('考试方式');
$sheet->getColumnDimension('E')->setWidth(30);
$cellF = $sheet->getCell('F1');
$cellF->setValue('报名时间');
$sheet->getColumnDimension('F')->setWidth(30);
$cellG = $sheet->getCell('G1');
$cellG->setValue('来源');
$sheet->getColumnDimension('G')->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);
// 从 A2 开始填充数据
foreach ($data as $k => $v) {
if($v['aid'] == 99999){
$laiyuan = "专题";
}else{
$laiyuan = "文章";
}
$n = $k + 2;
// 获取单元格
$cellA = $sheet->getCell('A'.$n);
// 设置单元格的值
$cellA->setValue($v['uname']);
$cellB = $sheet->getCell('B'.$n);
$cellB->setValue($v['phone']);
$cellC = $sheet->getCell('C'.$n);
$cellC->setValue($v['xueli']);
$cellD = $sheet->getCell('D'.$n);
$cellD->setValue($v['mudi']);
$cellE = $sheet->getCell('E'.$n);
$cellE->setValue($v['fangshi']);
$cellF = $sheet->getCell('F'.$n);
$cellF->setValue($v['createtime']);
$cellG = $sheet->getCell('G'.$n);
$cellG->setValue($laiyuan);
// 再给表格体设置样式
$sheet->getStyle('A'.$n)->applyFromArray($styleArrayBody);
$sheet->getStyle('B'.$n)->applyFromArray($styleArrayBody);
$sheet->getStyle('C'.$n)->applyFromArray($styleArrayBody);
$sheet->getStyle('D'.$n)->applyFromArray($styleArrayBody);
$sheet->getStyle('E'.$n)->applyFromArray($styleArrayBody);
$sheet->getStyle('F'.$n)->applyFromArray($styleArrayBody);
$sheet->getStyle('G'.$n)->applyFromArray($styleArrayBody);
}
// MIME 协议的扩展
$filename = date('Y-m-d').'报名表单.xlsx';
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition:attachment;filename='.$filename);
header('Cache-Control:max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');