简体   繁体   中英

PHPExcel - Templates work but mine are corrupt

I am trying to write data to a template and download it. If i use the sample files as a template, it works but once I use my own it says its corrupt.

Any thoughts?

My code is:

<?php
require_once 'PHPExcel.php';

// Create new PHPExcel object
$fileName = '31docproperties.xlsx'; //location of template for PID type
$fileType = 'Excel2007';
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);

$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B4', 'Test Data');


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
ob_end_clean();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>

This is an old topic, but exactly related with my current problem.

I've got a very similar situation : I create a PHP script which create a excel file from a template using PHPExcel library.

For some time, it work with no problem. But when I changed the environnement, the downloaded file was not readable anymore.

I've search for a long time. I didn't find anything as a possible solution. I could find some ideas, but nothing works.

Finally, I started to watch the examples given with the library. I've found the example named '30template.php'. The example is exactly what I need. If I use the script directly, the downloaded file is not readable, like my own. But if a modify the script in order to save the file locally, then the file is OK !

I couldn't find any clues in order to solve this problem.

Maybe that point of view can help to find a solution.

Is it possible that the way the original template encoding as something to do with the problem ? Is it related to the encoding of the script file ? I can understand there is a link but I don't know how to control the encoding of the template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM