Phpexcel - Saved file is corrupted

When I try to open the excel spreadsheet that was recently saved, i get the error message: “There was a problem sending the command to the program.”

But when i finally get it to open. It only has data in the cell A1 with essentially corrupted-unreadable data.

Problem: My code needs to be fixed somehow so that I no longer receive corrupt data.

This is my code:

[php]

//include PHPExcel library//
require_once “Classes/PHPExcel/IOFactory.php”;

// Load Files //
$objTpl = PHPExcel_IOFactory::load(“template.xlsx”);
$objTpl->setActiveSheetIndex(); //set first sheet as active
$objTpl2 = PHPExcel_IOFactory::load(“file.xls”);

// Set the Cells with values //
$objTpl->getActiveSheet()->getStyle(‘C2’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); //C1 is right-justified
$objTpl->getActiveSheet()->setCellValue(‘F3’, date(‘Y-m-d’)); //set C1 to current date
$objTpl->getActiveSheet()->getStyle(‘C4’)->getAlignment()->setWrapText(true);
$objTpl->getActiveSheet()->getColumnDimension(‘C’)->setWidth(40); //set column C width
$objTpl->getActiveSheet()->getRowDimension(‘4’)->setRowHeight(120); //set row 4 height
$objTpl->getActiveSheet()->getStyle(‘A4:C4’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); //A4 until C4 is vertically top-aligned
$objTpl->getActiveSheet()->setCellValue(‘F16’,$objTpl2->getActiveSheet()->GetCell(‘BM3’)->getValue());
$objTpl->getActiveSheet()->setCellValue(‘F17’,$objTpl2->getActiveSheet()->GetCell(‘BP3’)->getValue());
$objTpl->getActiveSheet()->setCellValue(‘F18’,$objTpl2->getActiveSheet()->GetCell(‘BS3’)->getValue());
$objTpl->getActiveSheet()->setCellValue(‘F19’,$objTpl2->getActiveSheet()->GetCell(‘BT3’)->getValue());

// Prepare Download //
$filename=‘goodFile.xlsx’; //Sets the Filename
header(‘Content-Type: application/vnd.ms-excel’);
header(‘Content-Disposition: attachment;filename="’.$filename.’"’);
header(‘Cache-Control: max-age=0’);

$objWriter = PHPExcel_IOFactory::createWriter($objTpl, ‘Excel5’);
$objWriter->save(‘php://output’); //Save File

exit;
[/php]

Thanks,
Nel

Sponsor our Newsletter | Privacy Policy | Terms of Service