简体   繁体   English

PHPExcel强制下载问题

[英]PHPExcel Force Download Issue

I know this might have been asked in several pieces, but I could not find an exact answer to the issue. 我知道这个问题可能已经被问了几遍,但是我找不到这个问题的确切答案。 I am using PHPExcel to generate an Excel file (obviously), and the code works to generate the file, but not when I include the code for Force Download, it corrupts the file. 我正在使用PHPExcel生成Excel文件(显然),并且代码可以生成文件,但是当我包含“强制下载”的代码时却没有,它破坏了文件。 My latest version of the script looks like this: 我最新的脚本版本如下所示:

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);



}

Remember, when I remove the force code section, the file generates and I can FTP it down fine. 记住,当我删除强制代码部分时,将生成文件,并且可以将其传输到FTP。 However, doing both generating and forcing the file gives me a corrupt file. 但是,生成和强制执行文件都会给我一个损坏的文件。 Normally I can click "Open & Repair" (Office2011 MacOSX) but obviously this is not desirable. 通常,我可以单击“打开并修复”(Office2011 MacOSX),但显然这是不可取的。

Could someone please help me understand: 有人可以帮助我理解:

  1. Why it is being generated as corrupt? 为什么将其生成为损坏? And why it works fine when I don't force download. 以及为什么当我不强制下载时它可以正常工作。
  2. What the proper order for saving/forcing is (using PHP's header() function) 保存/强制的正确顺序是什么(使用PHP的header()函数)
  3. If there is a better way of doing this. 如果有更好的方法可以做到这一点。

Much appreciated!! 非常感激!!

**** Update **** Here is the code when I click "Fix & Repair": ****更新****这是我单击“修复并修复”时的代码:

<?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>

// answer actually belongs to @Dagon //答案实际上属于@Dagon

The answer is actually straightforward, only needing a simple exit(); 答案实际上很简单,只需要一个简单的exit(); call. 呼叫。

Final code block: 最终代码块:

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();

}

This spits out the excel file to the browser: 这会将excel文件吐出到浏览器中:

readfile(XLSX_SAVE_PATH . $filename);  

and then you spit this out, which becomes PART of the excel file as downloaded by the browser 然后将其吐出,这成为浏览器下载的excel文件的一部分

echo "DONE!";

essentially you're sending 本质上你是在发送

[excel data]DONE!

when Excel is expecting only 当Excel仅期望

[excel data]

Try to modify File.php like this : 尝试像这样修改File.php:

protected static $_useUploadTempDirectory = TRUE; 受保护的静态$ _useUploadTempDirectory = TRUE;

in folder phpexcel/Classes/PHPExcel/Shared (it's not the best way but it worked for me). 在文件夹phpexcel / Classes / PHPExcel / Shared中(这不是最好的方法,但是对我有用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM