上面是本人测试过的实例,最下面是我自己封装的一个方法,配置好下面的文件后可以直接传参调用!
首先下载Excel类库放到vendor文件夹中。点击下载
本人测试后成功的实例:
public function export(){
$xlsData = Db::table('stu')
->join('class','stu.classid=class.classid','left')
->join('school','school.schoolid=stu.schoolid','left')
->select();
Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
$objExcel = new \PHPExcel();
//set document Property
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
$objActSheet = $objExcel->getActiveSheet();
$key = ord("A");
$letter =explode(',',"A,B,C,D,E,F");
$arrHeader = array('姓名','性别','入学时间','班级','学校');
//填充表头信息
$lenth = count($arrHeader);
for($i = 0;$i < $lenth;$i++) {
$objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
};
//填充表格信息
foreach($xlsData as $k=>$v){
$k +=2;
$objActSheet->setCellValue('A'.$k,$v['stuname']);
$objActSheet->setCellValue('B'.$k, $v['sex']);
$objActSheet->setCellValue('C'.$k, $v['stime']);
$objActSheet->setCellValue('D'.$k, $v['classname']);
$objActSheet->setCellValue('E'.$k, $v['schoolname']);
// 表格高度
$objActSheet->getRowDimension($k)->setRowHeight(20);
}
$width = array(20,20,15,10,10,30,10,15);
//设置表格的宽度
$objActSheet->getColumnDimension('A')->setWidth($width[5]);
$objActSheet->getColumnDimension('B')->setWidth($width[1]);
$objActSheet->getColumnDimension('C')->setWidth($width[0]);
$objActSheet->getColumnDimension('D')->setWidth($width[5]);
$objActSheet->getColumnDimension('E')->setWidth($width[5]);
$outfile = "学生信息表.xls";
ob_end_clean();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$outfile.'"');
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
}
以下是本人自己封装的方法,大牛勿喷,写在common下:
/**
* Excel下载
* 变量 作用 数据类型
* $xls 文件名 string
* $data 需要打印的数据 array
* $table 需要打印的信息在数据库中数据的字段名(顺序同下) array
* $name Excel表格显示时的名字(顺序同上) array
*/
function down($xls,$data,$name,$table)
{
//调用类库,路径是基于vendor文件夹的
Vendor('PHPExcel.PHPExcel');
Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
$objExcel = new \PHPExcel();
//set document Property
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
$objActSheet = $objExcel->getActiveSheet();
$key = ord("A");
$letter =explode(',',"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T");//列数,不够自己加
$arrHeader = $name;
//填充表头信息
$lenth = count($arrHeader);
$objActSheet->setCellValue($letter[0]."1","序号");
for($i = 0;$i < $lenth;$i++) {
$objActSheet->setCellValue($letter[$i+1]."1","$arrHeader[$i]");
};
//填充表格信息
//定义序号
$j=1;
foreach($data as $k=>$v){
$k +=2;
// 表格内容
$t=0;
$objActSheet->setCellValue($letter[0].$k,$j++);
for($i=0;$i<count($name);$i++){
$objActSheet->setCellValue($letter[$t+1].$k,$v[$table[$t++]]);
}
// 表格高度
$objActSheet->getRowDimension($k)->setRowHeight(20);
}
//表格名字
$outfile = $xls.date('Y-m-d H时m分s秒',time()).".xls";
//其他配置信息
ob_end_clean();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$outfile.'"');
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
}