標籤:ati 文檔 提交 http 基本 cat direct 模板 str
今天繼續研究PHP匯出excel檔案,把複雜的事情簡單化,一步步實現功能,首先實現模板檔案的匯出,隨後再實現寫入資料後匯出,最終實現功能,這是基本思路。中間可以加一步,先自己寫入資料匯出試試,隨後再資料庫匯入。我首先把程式提交到自建的eubexcel.php檔案,選用post提交,匯出excel檔案的程式在這個頁面裡書寫,參考昨天下載的PHPExcel-1.8組件裡的參考文檔,先部署匯出excel,具體代碼如下
<?php
error_reporting(E_ALL);
ini_set(‘display_errors‘, TRUE);
ini_set(‘display_startup_errors‘, TRUE);
date_default_timezone_set(‘Europe/London‘);
if (PHP_SAPI == ‘cli‘)
die(‘This example should only be run from a Web Browser‘);
/** Include PHPExcel */
require_once dirname(__FILE__) . ‘/../plugins/PHPExcel-1.8/Classes/PHPExcel.php‘;
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Read from Excel2007 (.xlsx) template
//echo date(‘H:i:s‘) , " Load Excel5 template file" , EOL;
$objReader = PHPExcel_IOFactory::createReader(‘Excel5‘);
$objPHPExcel = $objReader->load("../template/eub.xls");
//print_r($objPHPExcel);
//die();
$filename = "eub-".date("YmdHis").".xls";
// Redirect output to a client’s web browser (Excel2007)
header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset="GB2312"‘);
header(‘Content-Disposition: attachment;filename="‘.$filename.‘"‘);
header(‘Cache-Control: max-age=0‘);
// If you‘re serving to IE 9, then the following may be needed
header(‘Cache-Control: max-age=1‘);
// If you‘re serving to IE over SSL, then the following may be needed
header (‘Expires: Mon, 26 Jul 1997 05:00:00 GMT‘); // Date in the past
header (‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s‘).‘ GMT‘); // always modified
header (‘Cache-Control: cache, must-revalidate‘); // HTTP/1.1
header (‘Pragma: public‘); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007‘);
$objWriter->save(‘php://output‘);
exit;
?>
在此期間遇到一個問題代碼中的 createReader(‘Excel5‘);代碼,是關於phpExcel匯入不同excel版本的設定,我用的模板檔案是(97-2003 工作表)是2003年以前的版本,2003以前的excel讀取執行個體化excel5類,2003以後需要執行個體化excel2007類,so,改改$objReader執行個體化的地方:其中createReader(‘Excel2007‘)改為createReader(‘Excel5‘)
PHP匯出excel檔案,第一步先實現PHP模板匯出不帶資料