php常用的导出表格有两种方法,第一种是输出表格,这种方法打开的时候有警告提示,一般导出表格会用phpexcel,这个导出比较灵活,而且还可以设置表格的样式。
第一种导出例子
/**
* 执行导出
* @param unknown $result 数据源
*/
function Excel($result,$fileName=‘‘,$Key=‘‘){
$fileName = iconv(‘utf-8‘, ‘gbk‘,$fileName);
header("Content-type:text/html;charset=utf-8");
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=$fileName".date("Y-m-d").".xls");
header("Pragma: no-cache");
header("Expires: 0");
//第一行显示key
if(!$Key){
$Key = array("姓名 ","性别 ","手机号 ","意向车型 ","购买计划","提交时间");
}
echo iconv(‘utf-8‘, ‘gbk‘, implode("\t", $Key))."\n";
//第三行显示剩余val
foreach($result as $key=>$val){
echo iconv(‘utf-8‘, ‘gbk‘, implode("\t", $val))."\n";
}
}
第二种导出使用phpexcel
/**
* 导出数据(生成excel)
* @param $data excel第一行数据
* 例:1、array(‘字段‘ => ‘1‘);
* @param $list 二维数组 导出的数据
* @param string $title 工作表名称
* @param string $filename 文件名称
* @return boolean
*/
public function output1($data , $list , $title = ‘数据模板‘ , $filename = ‘数据模板‘){
$count = count($data);//数组长度
if(!is_array($data) || $count > $this->maxstr){//数据是否符合要求
return false;
}
// Create new PHPExcel object
$objPHPExcel = new \PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data 第一行
// 添加一些数据 第一行
$SheetIndex = $objPHPExcel->setActiveSheetIndex(0);
$this->setAttendance($SheetIndex);//写入数据
//设置第一个单元格样式
$obActiveSheet = $objPHPExcel->getActiveSheet();
//设置高度
$obActiveSheet->getRowDimension(‘1‘)->setRowHeight(40);
$obActiveSheet->getRowDimension(‘6‘)->setRowHeight(100);
$obActiveSheet->getRowDimension(‘7‘)->setRowHeight(90);
$obActiveSheet->getRowDimension(‘8‘)->setRowHeight(90);
$obActiveSheet->getRowDimension(‘9‘)->setRowHeight(90);
$obActiveSheet->getRowDimension(‘10‘)->setRowHeight(80);
$obActiveSheet->getRowDimension(‘17‘)->setRowHeight(20);
$obActiveSheet->getRowDimension(‘18‘)->setRowHeight(30);
$obActiveSheet->getRowDimension(‘19‘)->setRowHeight(30);
//设置宽度
$obActiveSheet->getColumnDimension(‘A‘)->setWidth(12);
$obActiveSheet->getColumnDimension(‘B‘)->setWidth(18);
$obActiveSheet->getColumnDimension(‘C‘)->setWidth(15);
$obActiveSheet->getColumnDimension(‘D‘)->setWidth(15);
$obActiveSheet->getColumnDimension(‘E‘)->setWidth(15);
$obActiveSheet->getColumnDimension(‘H‘)->setWidth(15);
//导出
$this->outputPHPExcel($objPHPExcel, $filename);
}
/**
* 考勤数据写入
* @param $SheetIndex 单元格对象
* @param $Cycle 周期数据列表
* @param $_clock 员工考勤数据
* @return number 返回当前最大行数
*/
private function setAttendance($SheetIndex){
$c = 1;
/** ----------------------------考勤头部开始--------------------------------------- */
$chrA = chr($this->asciiA + 0);//A
$chrB= chr($this->asciiA + 1);//B
$chrC= chr($this->asciiA + 2);//C
$chrD= chr($this->asciiA + 3);//D
$chrE= chr($this->asciiA + 4);//E
$chrF= chr($this->asciiA + 5);//F
$chrG= chr($this->asciiA + 6);//G
$chrH= chr($this->asciiA + 7);//G
$chrI= chr($this->asciiA + 8);//G
$chrJ= chr($this->asciiA + 9);//G
$chrK= chr($this->asciiA + 10);//G
$chrL= chr($this->asciiA + 11);//G
$chrM= chr($this->asciiA + 12);//G
$chrN= chr($this->asciiA + 13);//G
$chrO= chr($this->asciiA + 14);//G
$SheetIndex->setCellValueExplicit($chrA.($c), ‘岗位考核模板‘);
$SheetIndex->mergeCells($chrA.($c).‘:‘.$chrO.($c));//合并单元格 A2,A3
$SheetIndex->setCellValueExplicit($chrA.($c+1), ‘被考核人‘);
$SheetIndex->mergeCells($chrA.($c+1).‘:‘.$chrA.($c+2));//合并单元格 A2,A3
$SheetIndex->setCellValueExplicit($chrB.($c+1), ‘姓名‘);
$SheetIndex->setCellValueExplicit($chrB.($c+2), ‘职务‘);
$SheetIndex->setCellValueExplicit($chrC.($c+1), ‘xxx‘);
$SheetIndex->setCellValueExplicit($chrD.($c+1), ‘部门‘);
$SheetIndex->setCellValueExplicit($chrD.($c+2), ‘入职时间‘);
$SheetIndex->setCellValueExplicit($chrE.($c+1), ‘网站开发部‘);
$SheetIndex->mergeCells($chrE.($c+1).‘:‘.$chrG.($c+1));//合并单元格
$SheetIndex->mergeCells($chrE.($c+2).‘:‘.$chrG.($c+2));//合并单元格
$SheetIndex->setCellValueExplicit($chrH.($c+1), ‘被考核人主管‘);
$SheetIndex->mergeCells($chrH.($c+1).‘:‘.$chrI.($c+1));//合并单元格
$SheetIndex->mergeCells($chrH.($c+2).‘:‘.$chrI.($c+2));//合并单元格
$SheetIndex->setCellValueExplicit($chrJ.($c+1), ‘姓名‘);
$SheetIndex->setCellValueExplicit($chrJ.($c+2), ‘职务‘);
$SheetIndex->setCellValueExplicit($chrK.($c+1), ‘xxx);
$SheetIndex->mergeCells($chrK.($c+1).‘:‘.$chrO.($c+1));//合并单元格
$SheetIndex->mergeCells($chrK.($c+2).‘:‘.$chrO.($c+2));//合并单元格
$SheetIndex->mergeCells($chrA.($c+3).‘:‘.$chrO.($c+3));//合并单元格
//第5行
$SheetIndex->setCellValueExplicit($chrA.($c+4), ‘序号‘);
$SheetIndex->setCellValueExplicit($chrB.($c+4), ‘指标名称‘);
$SheetIndex->setCellValueExplicit($chrC.($c+4), ‘权重‘);
$SheetIndex->setCellValueExplicit($chrD.($c+4), ‘衡量方法‘);
$SheetIndex->setCellValueExplicit($chrM.($c+4), ‘评定‘);
$SheetIndex->setCellValueExplicit($chrN.($c+4), ‘评分‘);
$SheetIndex->setCellValueExplicit($chrO.($c+4), ‘最终得分‘);
$SheetIndex->mergeCells($chrD.($c+4).‘:‘.$chrL.($c+4));//合并单元格
//第6行
$SheetIndex->setCellValueExplicit($chrA.($c+5), ‘1‘);
$SheetIndex->setCellValueExplicit($chrB.($c+5), ‘工作完成度‘);
$SheetIndex->setCellValueExplicit($chrC.($c+5), ‘30%‘);
$SheetIndex->setCellValueExplicit($chrD.($c+5), "A+:测试没有崩溃、严重并且无高级别bug
A:测试没有崩溃、严重并且高级别bug控制在所有bug的1%以内,中级别bug控制在10%
100:测试没有崩溃、严重并且高级别bug控制在所有bug的2%以内,中级别bug控制在20%
B :测试没有崩溃、严重并且高级别bug控制在所有bug的5%,使用方反馈系统中出现文案、兼容性等对系统造成较小影响的bug
B-: 有崩溃严重bug或者无奔溃严重bug但是有5%以上的高级别bug,使用方反馈在使用中出现功能无法使用、500、404等严重故障");
$SheetIndex->setCellValueExplicit($chrM.($c+5), ‘100‘);
$SheetIndex->setCellValueExplicit($chrN.($c+5), ‘100‘);
$SheetIndex->setCellValueExplicit($chrO.($c+5), ‘30‘);
$SheetIndex->mergeCells($chrD.($c+5).‘:‘.$chrL.($c+5));//合并单元格
}
这只是部分代码