2014-05-09 59 views
3

我想創建一個簡單的報告系統,它可以輸出HTML或導出到Excel。我已經通過Codeigniter視圖成功創建了一個HTML表格,但我想知道是否有任何方法可以重新使用該視圖來創建Excel導出,而不是手動執行,因爲我必須更新每次我想對報告進行更改時,都會在兩個地方進行。PHPExcel - 從HTML表格創建電子表格?

任何意見讚賞。

謝謝。

回答

15

這似乎是工作:

// Load the table view into a variable 
$html = $this->load->view('table_view', $data, true); 

// Put the html into a temporary file 
$tmpfile = time().'.html'; 
file_put_contents($tmpfile, $html); 

// Read the contents of the file into PHPExcel Reader class 
$reader = new PHPExcel_Reader_HTML; 
$content = $reader->load($tmpfile); 

// Pass to writer and output as needed 
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007'); 
$objWriter->save('excelfile.xlsx'); 

// Delete temporary file 
unlink($tmpfile); 
+1

日Thnx這真的幫助了,但是當我通過包含Unicode值的Excel文件是完全搞砸HTML表格,我怎麼能解決這個問題。 –

+0

而不是將'$ html'保存到一個臨時文件然後由PHPExcel_Reader讀取,爲什麼不直接通過'PHPExcel_IOFactory :: createWriter'將$ html直接寫入xlxs文件? –

+0

嗨,對我來說,colspan不起作用。有什麼建議? –