<?php include '../init.inc.php'; include "../db.inc.php"; /* @func 引入類 */ include ROOT.'libs/PHPExcelClasses/PHPExcel.php'; // include ROOT.'libs/PHPExcelClasses/PHPExcel/Writer/Excel2007.php'; $expCellName = array('title1','標題2'); $expTableData = array( array('數據一','a1',3,4444), array('數據二','b1',2 ), ); $sheetName = '測試'; outputExcel($expCellName, $expTableData, $sheetName); function outputExcel($expCellName, $expTableData, $sheetName){ /** +---------------------------------------------------------- * @param $sheetName string File name +---------------------------------------------------------- * @param $expCellName array Column name +---------------------------------------------------------- * @param $expTableData array Table data +---------------------------------------------------------- */ $obpe = new PHPExcel(); /* @func 設置文檔基本屬性 */ // $obpe_pro = $obpe->getProperties(); // $obpe_pro->setCreator('eexiang')//設置建立者 // ->setLastModifiedBy(date('y/m/d h:i',time()))//設置時間 // ->setTitle('data')//設置標題 // ->setSubject('beizhu')//設置備註 // ->setDescription('miaoshu')//設置描述 // ->setKeywords('keyword')//設置關鍵字 | 標記 // ->setCategory('catagory');//設置類別 /* 設置寬度 */ //$obpe->getActiveSheet()->getColumnDimension()->setAutoSize(true); //$obpe->getActiveSheet()->getColumnDimension('B')->setWidth(10); // 基礎行名 $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'); // 設置SHEET,默認第一個 $obpe->setactivesheetindex(0); // 設置表 $objSheet = $obpe->getActiveSheet(); $objSheet ->setTitle($sheetName); // 設置行名 foreach($expCellName as $k=>$v){ $obpe->getactivesheet()->setcellvalue($cellName[$k].'1', $v); } // 設置內容 foreach($expTableData as $rid => $row){ $rid = $rid + 2; // 從第二行開始 foreach($row as $cid => $col){ $obpe->getactivesheet()->setcellvalue($cellName[$cid].$rid, $col); } } // 生成多個SHEET // 文件名 $filename = date('_YmdHis').'.xls';// or iconv('utf-8', 'gb2312', $sheetName).'.xls'; //寫入內容 $obwrite = PHPExcel_IOFactory::createWriter($obpe, 'Excel5'); // 儲存 $obwrite->save($filename); /******************************************* 直接在瀏覽器輸出 *******************************************/ // 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/vnd.ms-execl'); // header('Content-Type:application/octet-stream'); // header('Content-Type:application/download'); // header("Content-Disposition:attachment;filename='".$filename."'"); // header('Content-Transfer-Encoding:binary'); // $obwrite->save('php://output'); } ?>