因爲很難處理不同瀏覽器之間的不同標準,所以我放棄了試圖使用js或jQuery導出html表格。我想知道我是否可以在HTML中將表格發回服務器,並在服務器上生成一個.xls文件供用戶下載。如何使用PHPExcel將html表格導出爲excel?
現在使用PHPExcel服務器端,我的代碼是這樣的:
$filename = "DownloadReport";
$table = $_POST['table'];
ini_set('zlib.output_compression','Off');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
//the folowing two lines make sure it is saved as a xls file
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
$objReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $objReader->load($table);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
問題是我不能直接加載HTML表格。我該如何處理?
還有一個問題是,當我在php中設置標題時,單擊按鈕時文件不會下載。其實我可以查看POST響應的Header的所有屬性,以及響應的內容(在FireBug中),這些都是正確的。
輕微相關 - 你很可能有使用http://excelbuilderjs.com/在JS中創建導出。 – Stephen
將$表保存到一個臨時文件,然後從該文件加載 –
http://stackoverflow.com/questions/23562380/phpexcel-create-spreadsheet-from-html-table – Aba