A-A+

phpexcel导出excel无法打开,提示文件格式或文件名无效,文件损毁,解决办法

2018年04月13日 php 暂无评论 阅读 11,857 views 次

使用过很多次phpexcel了,有时需要保存文件到磁盘,有时需要浏览器弹出下载。保存到磁盘一半不会出现问题,关键是浏览器弹出保存,经常会发生导出的excel文件无法打开,提示文件格式或文件名无效,文件损毁。在此,记录一下解决办法。

 

 

1、xls还是xlsx?首先确定导出的excel文件扩展名

2、添加header,不同的文件类型,不同的header。

我就是这里出了问题,xlsx用了xls的header,导致导出的excel无法打开。

2007excel:xlsx如下:

$excelName = '绩效得分统计'.time();

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header('Content-Disposition: attachment;filename="'.$excelName.'.xlsx"');

header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save('php://output');

exit;

 

2003excel:xls如下:

header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="links_out'.$timestamp.'.xls"');

header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

exit;

 

3、末尾添加exit。

$objWriter->save('php://output');

exit;

 

 

例如xlsx,完整参考如下:

require_once ('inc/PHPExcel-1.8/Classes/PHPExcel.php');

$objPHPExcel = new PHPExcel();

$objSheet = $objPHPExcel->getActiveSheet();

$objPHPExcel->getProperties()->setCreator("文档创建者添翼博客")

->setTitle("添翼博客PHPExcel" . time())

->setSubject("添翼博客phpexcel导出". time())

->setDescription("phpexcel导出excel无法打开,提示文件格式或文件名无效,文件损毁,解决办法". time())

->setKeywords("phpexcel");

ob_end_clean();

 

//填充数据

$objSheet->setTitle("绩效得分统计". time());

 

//输入表头1

foreach ($titlearray as $k=>$v){

$num = $k;

$objSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($num) . (1), $v);

}

 

//输入表头2

foreach ($leadnames as $k=>$v){

$num = $k;

$objSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($num) . (2), $v);

}

$head3 = ['',''];

$head4 = array_merge($head3,$dept_data);

foreach ($dept_data as $k2=>$v2){

$num2 = ($k2+1)*2-1;

$num3 = ($k2+1)*2;

$num4 = ($k2+2)*2-1;

$num5 = ($k2+2)*2;

 

//合并单元格
$first = 'A'.$num2;
$first2 = 'A'.$num3;
$first3 = 'A'.$num4;
$first4 = 'B'.$num4;
$first5 = 'B'.$num5;

$objPHPExcel->getActiveSheet()->mergeCells("$first:$first2");
$objSheet->setCellValue($first3, $v2);
$objSheet->setCellValue($first4, '应得分');
$objSheet->setCellValue($first5, '实得分');
}

$col = count($projectName);
$row = count($dept_data);
for ($i=0;$i<$col;$i++){
for ($j=0;$j<$row;$j++){
$n1 = $i+2;
$n2 = PHPExcel_Cell::stringFromColumnIndex($n1);
$m1 = ($j+2)*2-1;
$m2 = ($j+2)*2;
$kou1 = $n2.$m1;
$kou2 = $n2.$m2;
// echo $new2Data1[$i][$j];
if (!empty($new2Data1[$i][$j])){
$objSheet->setCellValue($kou1, $new2Data1[$i][$j]);
}
if (!empty($new2Data2[$i][$j])){
$objSheet->setCellValue($kou2, $new2Data2[$i][$j]);
}
}
}

$excelName = '绩效得分统计'.time();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$excelName.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

 

 

 

标签:

给我留言