PHPExcel 导出

时间:2023-11-25 14:26:02
<?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'); }
?>