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