这几天在整PHP的导出.相对5.0版本.5.1废弃了vendor和import助手函数.通过composer自动加载完成引入.废话少说.直接抱起键盘就是干 ?
第一步.通过composer安装phpexcel
在项目根目录运行 composer require phpoffice/phpexcel
第二步.因为通过composer加载的第三方类,所以直接在头部引入一下就可以了.
代码语言:javascript复制use PHPExcel_IOFactory;
use PHPExcel;
代码语言:javascript复制$excel = new PHPExcel();
dump($excel);
这样就可以将类引入了.
导出效果如下
这里也是封装了一个方法.直接调用下就可以.需要改一个表头和数据库字段
代码语言:javascript复制public function clientExcel($data = [],$name = 'excel')
{
$excel = new PHPExcel(); //引用phpexcel
iconv('UTF-8', 'gb2312', $name); //针对中文名转码
$header = ['订单编号', '订单状态', '客户名字', '订单名字', '创建时间','订单备注','开料尺寸','印件色数','印件色别','印刷方式','成品尺寸','印刷板数','千印数量']; //表头,名称可自定义
$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->setTitle($name); //设置表名
$excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(18);
$excel->getActiveSheet()->getColumnDimension('B')->setWidth(80);
$letter = ['A', 'B', 'C', 'D', 'E','F','G','H','I','J','K','L','M'];//列坐标
//设置表头
$excel->setActiveSheetIndex(0)
->setCellValue('A1', '订单编号')
->setCellValue('B1', '订单状态')
->setCellValue('C1', '客户名字')
->setCellValue('D1', '订单名字')
->setCellValue('E1', '创建时间')
->setCellValue('F1', '订单备注')
->setCellValue('G1', '开料尺寸')
->setCellValue('H1', '印件色数')
->setCellValue('I1', '印件色别')
->setCellValue('J1', '印刷方式')
->setCellValue('K1', '成品尺寸')
->setCellValue('L1', '印刷板数')
->setCellValue('M1', '千印数量');
//写入数据
foreach ($data as $k => $v) {
//从第二行开始写入数据(第一行为表头)
//$excel->getActiveSheet()->setCellValue('A'.($k 2),$v['gid']);
$excel->getActiveSheet()->setCellValue('A' . ($k 2), $v['order_id']."t");
$excel->getActiveSheet()->setCellValue('B' . ($k 2), $v['order_status']);
$excel->getActiveSheet()->setCellValue('C' . ($k 2), $v['name']);
$excel->getActiveSheet()->setCellValue('D' . ($k 2), $v['order_name']);
$excel->getActiveSheet()->setCellValue('E' . ($k 2), $v['create_time']);
$excel->getActiveSheet()->setCellValue('F' . ($k 2), $v['remark']);
$excel->getActiveSheet()->setCellValue('G' . ($k 2), $v['poper_size']);
$excel->getActiveSheet()->setCellValue('H' . ($k 2), $v['color_num']);
$excel->getActiveSheet()->setCellValue('I' . ($k 2), $v['color_type']);
$excel->getActiveSheet()->setCellValue('J' . ($k 2), $v['print_type']);
$excel->getActiveSheet()->setCellValue('K' . ($k 2), $v['final_dimension']);
$excel->getActiveSheet()->setCellValue('L' . ($k 2), $v['print_num']);
$excel->getActiveSheet()->setCellValue('M' . ($k 2), $v['prints_num']);
}
//设置单元格边框
$excel->getActiveSheet()->getStyle("A1:E" . (count($data) 1))->getBorders()->getAllBorders()->setBorderStyle();
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $name . '.xls"');
header('Cache-Control: max-age=0');
$res_excel = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$res_excel->save('php://output');
}