之前写的导出,今天发现了个bug,如果导出的数据列数超过26列会报错:Invalid cell coordinate [1,所以,还得改:
/**
* excel表格导出
* @param string $fileName 文件名称
* @param array $headArr 表头名称
* @param array $data 要导出的数据
* @author mrLv
*/
function excelExport($fileName = '', $headArr = [], $data = []) {
$fileName .= "_" . date("Y_m_d", time()) . ".xls";
$spreadsheet = new Spreadsheet();
$objPHPExcel = $spreadsheet->getActiveSheet();
$key = ord("A"); // 设置表头
$key2 = ord("@");
foreach ($headArr as $v) {
if($key > ord("Z")){
$key2 += 1;
$key = ord("A");
$colum = chr($key2).chr($key);
}else{
if($key2>=ord("A")){
$colum = chr($key2).chr($key);
}else{
$colum = chr($key);
}
}
$objPHPExcel->setCellValue($colum . '1', $v);
$key += 1;
}
$column = 2;
foreach ($data as $key => $rows) { // 行写入
$span = ord("A");
$span2 = ord("@");
foreach ($rows as $keyName => $value) { // 列写入
if($span > ord("Z")){
$span2 += 1;
$span = ord("A");
$j = chr($span2).chr($span);
}else{
if($span2>=ord("A")){
$j = chr($span2).chr($span);
}else{
$j = chr($span);
}
}
$objPHPExcel->setCellValue($j . $column, $value);
$span++;
}
$column++;
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
//删除清空:
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;
}
注意黑色加粗的代码!
Mysql8新增用户,mysql8配置权限,mysql8配置,mysql8配置文件 Linux命令,scp,scp命令,Linux复制 git commit git add centos git 搭建FTP,Linux FTP,禁止FTP登录ssh 上传文件,阿里云OSS上传,文件上传到OSS,OSS文件上传,OSS上传 微信支付,微信支付V3,PHP微信支付,微信nativePay支付,微信jsapi支付 微信支付,微信支付V3,PHP微信支付 bootstrap4 modal, lavarel The subversion command line tools are no longer provided by Xcode. 银联支付,tp5.1银联支付 支付宝即时到账,PHP支付宝 system libzip must be upgraded to version >= 0.11 CMake 3.0.2 or higher is required Class 'ZipArchive' not found chr() expects parameter 1 to be int php7.4 tcpdf unexpected '='