0

我知道这可能已被分几部分提出,但我找不到该问题的确切答案。我正在使用 PHPExcel 生成一个 Excel 文件(显然),并且代码可以生成文件,但是当我包含强制下载的代码时,它会损坏文件。我最新版本的脚本如下所示:

function make_xls_spreadsheet(){

/** Error reporting */
error_reporting(E_ALL);

/* Set the save path */
define('XLSX_SAVE_PATH', 'tmp/');

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';


/* Create a new PHPExcel Object */
$objPHPExcel = new PHPExcel();


/* Add some metadata to the file */
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");

/* Set active worksheet to first */
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('Segments');

/* Add some data to the worksheet */
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');


/* Write to server */
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

$filename = "tony1.xlsx";

// Works fine up to here

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//$objWriter->save('php://output');
$objWriter->save(XLSX_SAVE_PATH . $filename);
readfile(XLSX_SAVE_PATH . $filename);


echo "DONE!";

$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);



}

请记住,当我删除强制代码部分时,文件会生成,我可以通过 FTP 下载它。但是,生成和强制文件都会给我一个损坏的文件。通常我可以单击“打开和修复”(Office2011 MacOSX),但显然这是不可取的。

有人可以帮我理解:

  1. 为什么它被生成为腐败?以及为什么当我不强制下载时它可以正常工作。
  2. 保存/强制的正确顺序是什么(使用 PHP 的 header() 函数)
  3. 如果有更好的方法来做到这一点。

非常感激!!

****更新****这是我单击“修复和修复”时的代码:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <logFileName>Repair Result to tony1 03178.xml</logFileName>
    <summary>Errors were detected in file 'Macintosh HD:Users:tony.diloreto:Downloads:tony1.xlsx'</summary>
    <additionalInfo><info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info></additionalInfo>
</recoveryLog>
4

3 回答 3

2

// 答案实际上属于@Dagon

答案其实很简单,只需要一个简单的exit();调用。

最终代码块:

function make_xls_spreadsheet(){

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';


/* Create a new PHPExcel Object */
$objPHPExcel = new PHPExcel();

/** Determine filename **/
$filename = "tony1.xlsx";

/** Set header information **/
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');


/* Add some metadata to the file */
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");

/* Set active worksheet to first */
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('Segments');

/* Add some data to the worksheet */
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');


header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

exit();

}
于 2014-10-08T20:46:37.300 回答
0

这会将 excel 文件输出到浏览器:

readfile(XLSX_SAVE_PATH . $filename);  

然后你把它吐出来,它成为浏览器下载的 excel 文件的一部分

echo "DONE!";

本质上你正在发送

[excel data]DONE!

当 Excel 只期望

[excel data]
于 2014-10-08T19:45:18.383 回答
0

尝试像这样修改 File.php :

受保护的静态 $_useUploadTempDirectory = TRUE;

在文件夹 phpexcel/Classes/PHPExcel/Shared 中(这不是最好的方法,但它对我有用)。

于 2015-05-11T13:18:20.307 回答