2017-05-02 48 views
0
$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尝试修复文件时打开没有成功。无效的文件格式PHPExcel

我在做什么错?

+2

'Excel2007'是'.xlsx'文件的作家; 'Excel5'是'.xls'文件的编写者......这个区别非常重要 –

+0

啊哈。非常感谢你!这就是诀窍。 – bestprogrammerintheworld

回答

0

感谢@马克·贝克我想通了,我简直可以改变,我创建了作家到该行:

//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);