我目前正在使用PHPExcel编写数据导出,并保持获取空文件。 我已经制作了一个测试脚本,该脚本应该给我一个填充3个测试单元的excel文件,但是它也给我一个空的excel文件。
当我print_r $ objphpexcel时,它仍然显示单元格已满。 但是当我print_r writer时,它仅具有可用的标题数据,而没有单元格内容。
<?php
include_once 'models/PHPExcel.php';
include_once 'models/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator('Company')
->setLastModifiedBy('User')
->setTitle('Sample_title');
// Add header (first row) data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'EXCEL EXPORT')
->setCellValue('B1', 'TEST')
->setCellValue('C1', 'CEL');
$next_excelrow=2;
//ITERATION TROUGH DATA HERE
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('A');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
//echo'<pre>'.print_r($objPHPExcel,true).'</pre>';exit; <-- this print_r still has the data
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="sample_export.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
//header('Content-Type: text/html; charset=UTF-8');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
如您所见,我已经将其范围缩小到一个简单的导出,但是仍然可以下载一个损坏的空文件。
有人可以帮忙吗? 谢谢!
答案 0 :(得分:1)
尝试更改$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
到$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
答案 1 :(得分:1)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="file.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('file.xlsx');
尝试将其保存为示例文件,而不是php:// output。为我工作。
答案 2 :(得分:0)
自PHPExcel被弃用以来,我将移至PHPSpreadsheet