php - PHPExcel Force Download Issue -
i know might have been asked in several pieces, not find exact reply issue. using phpexcel generate excel file (obviously), , code works generate file, not when include code forcefulness download, corrupts file. latest version of script looks this:
function make_xls_spreadsheet(){ /** error reporting */ error_reporting(e_all); /* set 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 new phpexcel object */ $objphpexcel = new phpexcel(); /* add together metadata 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 office 2007 xlsx, generated using php classes."); /* set active worksheet first */ $objphpexcel->setactivesheetindex(0); $objphpexcel->getactivesheet()->settitle('segments'); /* add together info 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 server */ $objwriter = new phpexcel_writer_excel2007($objphpexcel); $filename = "tony1.xlsx"; // works fine 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 remove forcefulness code section, file generates , can ftp downwards fine. however, doing both generating , forcing file gives me corrupt file. can click "open & repair" (office2011 macosx) not desirable.
could please help me understand:
why beingness generated corrupt? , why works fine when don't forcefulness download. what proper order saving/forcing (using php's header() function) if there improve way of doing this.much appreciated!!
**** update **** here code when click "fix & repair":
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <recoverylog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <logfilename>repair result tony1 03178.xml</logfilename> <summary>errors detected in file 'macintosh hd:users:tony.diloreto:downloads:tony1.xlsx'</summary> <additionalinfo><info>excel completed file level validation , repair. parts of workbook may have been repaired or discarded.</info></additionalinfo> </recoverylog>
// reply belongs @dagon
the reply straightforward, needing 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 new phpexcel object */ $objphpexcel = new phpexcel(); /** determine filename **/ $filename = "tony1.xlsx"; /** set header info **/ header('content-type: application/vnd.ms-excel'); header('content-disposition: attachment;filename="' . $filename . '"'); header('cache-control: max-age=0'); /* add together metadata 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 office 2007 xlsx, generated using php classes."); /* set active worksheet first */ $objphpexcel->setactivesheetindex(0); $objphpexcel->getactivesheet()->settitle('segments'); /* add together info 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(); }
php apache phpexcel xlsx force-download
No comments:
Post a Comment