使用PHPExcel保存时文件格式无效

时间:2017-05-02 11:33:21

标签: php excel phpexcel

$file = '{full path and filename}';

//Read file
$file_type = PHPExcel_IOFactory::identify( $file );
$excel_reader = PHPExcel_IOFactory::createReader($file_type);              
$this->php_excel = $excel_reader->load($file);              

//Set some values
$this->php_excel->setActiveSheetIndex(0);
$this->php_excel->getActiveSheet()->SetCellValue('A1', 'Hello');
$this->php_excel->getActiveSheet()->SetCellValue('B2', 'world!');
$this->php_excel->getActiveSheet()->SetCellValue('C1', 'Hello');
$this->php_excel->getActiveSheet()->SetCellValue('D2', 'world!');

//Write to file new location    
$excel_writer = PHPExcel_IOFactory::createWriter($this->php_excel, 'Excel2007' );
$excel_writer->save('c:\test.xls');

c:\test.xls已创建,但我在尝试加载Excel文件时出错(来自OpenOffice,但我不认为这是问题)。我读取的文件大约是260kb,而创建的test.xls大约是64kb。它表示无效和OpenOffice在打开时尝试修复文件但没有成功。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

感谢@Mark Ba​​ker,我发现我可以将创建Writer的行更改为:

//Given that the file that are going to be saved should be of the same
//format that the read file has. 
$excel_writer = PHPExcel_IOFactory::createWriter($this->php_excel, $file_type );