导出数据时,希望按模板显示,可用此法。

if (PHP_SAPI == 'cli')

	die('This example should only be run from a Web Browser');


/** Include PHPExcel */
require_once  '../PHPExcel-1.8/Classes/PHPExcel.php';
require_once '../class/db.class.php';
require_once '../PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';//added


// Create new PHPExcel object
//$objPHPExcel = new PHPExcel();
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load ("FCR-SAMPLE.xlsx");
// Miscellaneous glyphs, UTF-8
//print_r($objPHPExcel);exit;


$objPHPExcel->setActiveSheetIndex(0)->setCellValue("A2",$_GET["receiveNumber"])->setCellValue("G3",$_GET['receiveNumber']);


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="cargoReceipt.xlsx"');
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;