PHPExcel Force Download Problem

I know that this could be asked in several parts, but I could not find the exact answer to this question. I use PHPExcel to generate the Excel file (obviously), and the code works to generate the file, but not when I turn on 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 that when I delete the power code section, the file is generated and I can FTP stop it. However, both generating and forcing the file gives me a damaged file. Usually I can click Open and Repair (Office2011 MacOSX), but obviously this is not desirable.

Can someone please help me understand:

  • Why is it generated as damaged? And why does this work great when I don't force download.
  • What is the correct save / force order (using the PHP header () function)
  • If there is a better way to do this.

Really appreciate !!

**** Update **** Here is the code when I click "Fix and 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> 
0
source share
3 answers

// The answer actually belongs to @Dagon

The answer is actually simple, you just need a simple call to exit(); .

The final block of code:

 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(); } 
+2
source

This splashes the excel file into the browser:

 readfile(XLSX_SAVE_PATH . $filename); 

and then you spit it out, which will become a PART of the excel file downloaded by the browser

 echo "DONE!"; 

essentially you send

 [excel data]DONE! 

when Excel only expects

 [excel data] 
0
source

Try modifying File.php as follows:

protected static $ _useUploadTempDirectory = TRUE;

in the phpexcel / Classes / PHPExcel / Shared folder (this is not the best way, but it worked for me).

0
source

Source: https://habr.com/ru/post/1215274/


All Articles