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(E_ALL); define('XLSX_SAVE_PATH', 'tmp/'); set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/'); include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; $objPHPExcel = new PHPExcel(); $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."); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setTitle('Segments'); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello'); $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!'); $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello'); $objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!'); $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); $filename = "tony1.xlsx";
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>
source share