吴峰的博客

使用phpexcel时,有时需要保存文件到磁盘,有时需要浏览器弹出下载。下载本地后,经常会发生导出的excel无法打开文件,提示文件格式或文件名无效,文件损毁。在此,记录一下解决办法。

1、在输出Excel前,缓冲区中处理BOM头(可能是其他字符)

ob_end_clean();

ob_start();

在header()函数调用之前,清楚之前的错误输出!

通过ob_get_contents()查看导出内容,查看BOM头。

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

3、添加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;


末尾添加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;

Tags:
PHP
评论 (0)
    说点什么吧... (取消回复)
Copyright 吴峰的博客 © 2014-2016 管理员邮箱:phpwufeng@163.com   统计:   ICP备案:鲁ICP备16004939号-1