问题描述
我知道这可能已经问几件,但我无法找到一个确切答案的问题。我使用PHPExcel生成Excel文件(显然)和code工作生成文件,而不是当我包括code代表队下载,它会损坏文件。我的最新剧本的版本是这样的:
功能make_xls_s preadsheet(){/ **错误报告* /
使用error_reporting(E_ALL);/ *设置保存路径* /
定义('XLSX_SAVE_PATH','TMP /');/ **包含路径** /
set_include_path(get_include_path()PATH_SEPARATOR'类/');/ ** * PHPExcel /
包括PHPExcel.php';/ ** * PHPExcel_Writer_Excel2007 /
包括PHPExcel /作家/ Excel2007.php';
/ *创建一个新PHPExcel对象* /
$ objPHPExcel =新PHPExcel();
/ *一些元数据添加到文件* /
$ objPHPExcel->的GetProperties() - GT; setCreator(马腾Balliauw);
$ objPHPExcel->的GetProperties() - GT; setLastModifiedBy(马腾Balliauw);
$ objPHPExcel->的GetProperties() - GT;的setTitle(Office 2007的XLSX测试文档);
$ objPHPExcel->的GetProperties() - GT; SETSUBJECT(Office 2007的XLSX测试文档);
$ objPHPExcel->的GetProperties() - GT; setDescription(Office 2007的XLSX测试文档,使用PHP类生成的。);/ *设置活动工作表先* /
$ objPHPExcel-> setActiveSheetIndex(0);
$ objPHPExcel-> getActiveSheet() - GT;的setTitle('段');/ *一些数据添加到工作表中* /
$ objPHPExcel-> setActiveSheetIndex(0);
$ objPHPExcel-> getActiveSheet() - GT; SetCellValue('A1','你好');
$ objPHPExcel-> getActiveSheet() - GT; SetCellValue(B2,世界!);
$ objPHPExcel-> getActiveSheet() - GT; SetCellValue('C1','你好');
$ objPHPExcel-> getActiveSheet() - GT; SetCellValue(D2,世界!);
/ *写*服务器/
$ objWriter =新PHPExcel_Writer_Excel2007($ objPHPExcel);$文件名=tony1.xlsx;//工作正常了这里标题(内容类型:应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet');
标题(内容处置:附件;文件名='$文件名。'');
标题(缓存控制:最大年龄= 0');$ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,Excel2007中');
// $ objWriter->保存(PHP://输出');
$ objWriter->保存(XLSX_SAVE_PATH $文件名);
ReadFile的(XLSX_SAVE_PATH $文件名);
回声DONE!;$ objPHPExcel-> disconnectWorksheets();
未设置($ objPHPExcel);}
记住,当我删除力code段,文件生成,我可以FTP下来罚款。但是,这样做既发电,并迫使该文件给我一个损坏的文件。通常情况下我可以点击打开&安培;修(Office2011 MacOSX的),但显然这是不可取的。
可能有人请帮助我明白了:
- 为什么它被作为腐败产生的?而为什么当我不强迫下载它工作正常。
- 什么是正确的顺序保存/强迫是(使用PHP的header()函数)
- 如果有这样做的更好的方法。
大部分AP preciated !!
**** ****更新这里是code,当我点击修复&安培;修:
<?XML版本=1.0编码=UTF-8独立=YES&GT?;
< recoveryLog的xmlns =http://schemas.openxmlformats.org/s$p$padsheetml/2006/main>
<&LOGFILENAME GT;修理结果到tony1 03178.xml< / LOGFILENAME>
<总结>在文件中检测到错误'的Macintosh HD:用户:tony.diloreto:下载:tony1.xlsx'< /总结>
< additionalInfo><信息> Excel中完成的文件级验证和修复。该工作簿的某些部分可能已被修复或丢弃< /信息>< / additionalInfo>
< / recoveryLog>
//真正回答属于@Dagon
答案其实简单,只需要一个简单的退出();
呼叫
最后code座:
功能make_xls_s preadsheet(){/ **包含路径** /
set_include_path(get_include_path()PATH_SEPARATOR'类/');/ ** * PHPExcel /
包括PHPExcel.php';/ ** * PHPExcel_Writer_Excel2007 /
包括PHPExcel /作家/ Excel2007.php';
/ *创建一个新PHPExcel对象* /
$ objPHPExcel =新PHPExcel();/ **确定文件名** /
$文件名=tony1.xlsx;/ **设置标题信息** /
标题(内容类型:应用程序/ vnd.ms - Excel中');
标题(内容处置:附件;文件名='$文件名。'');
标题(缓存控制:最大年龄= 0');
/ *一些元数据添加到文件* /
$ objPHPExcel->的GetProperties() - GT; setCreator(马腾Balliauw);
$ objPHPExcel->的GetProperties() - GT; setLastModifiedBy(马腾Balliauw);
$ objPHPExcel->的GetProperties() - GT;的setTitle(Office 2007的XLSX测试文档);
$ objPHPExcel->的GetProperties() - GT; SETSUBJECT(Office 2007的XLSX测试文档);
$ objPHPExcel->的GetProperties() - GT; setDescription(Office 2007的XLSX测试文档,使用PHP类生成的。);/ *设置活动工作表先* /
$ objPHPExcel-> setActiveSheetIndex(0);
$ objPHPExcel-> getActiveSheet() - GT;的setTitle('段');/ *一些数据添加到工作表中* /
$ objPHPExcel-> setActiveSheetIndex(0);
$ objPHPExcel-> getActiveSheet() - GT; SetCellValue('A1','你好');
$ objPHPExcel-> getActiveSheet() - GT; SetCellValue(B2,世界!);
$ objPHPExcel-> getActiveSheet() - GT; SetCellValue('C1','你好');
$ objPHPExcel-> getActiveSheet() - GT; SetCellValue(D2,世界!);
标题(内容类型:应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet');
标题(内容处置:附件;文件名='$文件名。'');
标题(缓存控制:最大年龄= 0');$ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,Excel2007中');
$ objWriter->保存(PHP://输出');出口();}
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. 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. 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.
Could someone please help me understand:
- Why it is being generated as corrupt? And why it works fine when I don't force download.
- What the proper order for saving/forcing is (using PHP's header() function)
- 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
The answer is actually straightforward, only needing a simple 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();
}
这篇关于PHPExcel强制下载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!