Cell formatting lost completely

Topics: Developer Forum
Aug 20, 2011 at 10:49 AM

Hello,

I have a Excel FIle. I open it using PHPExcel, save it back with a new name, and all cells lose their formatting. No Cell Borders, No Cell Background, Just the text, nothing else.

I created a new excel file in Open Office, bg filled the first cell, put some text, again went through the phpexcel code and again the bg fill vanished.

Here is my code.

$folder = "files/";
$template = "MS2.xls";
require_once(BASEPATH.'lib/PHPExcel/PHPExcel.php');
	
$excel_readers = array('excel' => 'Excel5' , 'excel2003' => 'Excel2003XML' , 'excel2007' => 'Excel2007');
$reader = PHPExcel_IOFactory::createReader($excel_readers['excel']);
$reader->setReadDataOnly(true);
		
$excel = $reader->load($folder.$template);

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$write_path = "$folder/MS.xls";
$writer->save($write_path);

I need to preserve all the cell formatting.

Aug 22, 2011 at 12:52 PM

Try removing this line...

$reader->setReadDataOnly(true);

Aug 28, 2011 at 4:56 AM

Thanks that worked. But because the Excel sheet is very large it takes more time to generate the file compared to when I manually assign styles. Any solution for that ?