标签:tab 否则 head factory www. ima www session cto
我使用的是tp3.2框架(下载地址:http://www.thinkphp.cn/topic/38123.html)
1.首先要下载PhpExcel类库,放在如下图目录下
2.调用方法
public function exportExcel($expTitle,$expCellName,$expTableData)
{
$xlsTitle = iconv(‘utf-8‘, ‘gb2312‘, $expTitle);//文件名称
$fileName = $_SESSION[‘account‘].date(‘_YmdHis‘);//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
vendor("PHPExcel.PHPExcel");//引入类库,一定要引对,否则会报错找不到这个类
$objPHPExcel = new \PHPExcel();
$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‘);
$objPHPExcel->getActiveSheet(0)->mergeCells(‘A1:‘.$cellName[$cellNum-1].‘1‘);//合并单元格
// $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘A1‘, $expTitle.‘ Export time:‘.date(‘Y-m-d H:i:s‘));
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].‘2‘, $expCellName[$i][1]);
}
// Miscellaneous glyphs, UTF-8
for($i=0;$i<$dataNum;$i++){
for($j=0;$j<$cellNum;$j++){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
}
}
header(‘pragma:public‘);
header(‘Content-type:application/vnd.ms-excel;charset=utf-8;name="‘.$xlsTitle.‘.xls"‘);
header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
ob_clean();//把数据从PHP的缓冲(buffer)中释放出来。
flush();//把不在缓冲(buffer)中的或者说是被释放出来的数据发送到浏览器。
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,‘Excel5‘);
$objWriter->save(‘php://output‘);
exit;
}
/**
*
* 导出Excel
*/
function daochu()
{//导出Excel
$xlsName = "User";
$xlsCell = array(
array(‘id‘,‘账号序列‘),
array(‘u_name‘,‘用户名字‘),
array(‘u_addres‘,‘地址‘),
array(‘u_tel‘,‘电话‘),
array(‘u_card‘,‘身份证号‘),
array(‘u_time‘,‘申报时间‘),
// array(‘u_imgz‘,‘申报时间‘),
// array(‘u_imgf‘,‘申报时间‘),
array(‘b_one_beizhu‘,‘办公人员备注‘),
array(‘b_shenhe_p‘,‘办公室审核人员‘),
array(‘water_kj‘,‘水表口径‘),
array(‘water_wz‘,‘水表具体位置‘),
array(‘water_quyu‘,‘区域位置‘),
array(‘water_start_time‘,‘施工日期‘),
array(‘water_start_peo‘,‘施工人员‘),
array(‘water_start_jingli‘,‘施工经理‘),
array(‘water_beizhu‘,‘施工经理备注‘),
array(‘water_xingzhi‘,‘用水性质‘),
array(‘watch_num‘,‘水表底数‘),
array(‘watch_time‘,‘抄表日期‘),
array(‘watch_peo‘,‘抄表人员‘),
array(‘moneyjingli_name‘,‘收费经理‘),
array(‘moneyjingli_beizhu‘,‘收费经理备注‘),
array(‘jingli_name‘,‘经理名称‘)
);
$xlsModel = M(‘api_aliucheng‘);
$xlsData = $xlsModel->Field(‘id,u_name,u_addres,u_tel,u_card,u_time,b_one_beizhu,b_shenhe_p,water_kj,water_wz,water_quyu,water_start_time,water_start_peo,water_start_jingli,water_beizhu,water_xingzhi,watch_num,watch_time,watch_peo,moneyjingli_name,moneyjingli_beizhu,jingli_name‘)->where(array(‘jingli_state_san‘=>1))->select();
$this->exportExcel($xlsName,$xlsCell,$xlsData);
}
3.导出结果(上面标红的地方要特别注意)
标签:tab 否则 head factory www. ima www session cto
原文地址:https://www.cnblogs.com/wrld/p/9073195.html