Get corrupted xls file on upload using PHPExcel

In my current project, I used PHPExcel 1.7.8 to export data to excel. As suggested, I successfully configured it in my project and successfully generated the xls file and saved it, but when I tried to download the xls file, then I messed up the xls file without receiving an error message. I have a code for it.

/** Error reporting */ if (PHP_SAPI == 'cli') die('This example should only be run from a Web Browser'); /** Include PHPExcel */ require_once '../Classes/PHPExcel.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("PHPExcel Test Document") ->setSubject("PHPExcel Test Document") ->setDescription("Test document for PHPExcel, generated using PHP classes.") ->setKeywords("office PHPExcel php") ->setCategory("Test result file"); // Add some data $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B1', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D1', 'world!'); // Miscellaneous glyphs, UTF-8 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A2', 'Miscellaneous glyphs') ->setCellValue('B2', 'This is test text by me'); // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client's web browser (Excel5) header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=\"filename.xls\""); header("Cache-Control: max-age=0"); // Save Excel 2007 file $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save(str_replace('.php', '.xls', __FILE__)); $objWriter->save("php://output"); exit; 

Thanks.

+4
source share
1 answer

You can try? Shortly before your

 header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=\"filename.xls\""); header("Cache-Control: max-age=0"); 

put ob_end_clean(); .

There may be something wrong with your output buffer.

+1
source

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


All Articles