php代码
/**
* 导出Excel文件
* @throws \PHPExcel_Exception
* @throws \PHPExcel_Writer_Exception
*/
public function exports(Request $request){
// $phpexcelSrc=new \PHPExcel();//我们将手动下载的phpexcel放置到了application同级目录
// dump(APP_PATH);die;
// include ($phpexcelSrc);//引入phpExcel
$phpexcel=new \PHPExcel();//实例化PHPExcel类对象,方便操作即将生成的excel表格
$phpexcel->setActiveSheetIndex(0);//选中我们生成的excel文件的第一张工作表
$sheet=$phpexcel->getActiveSheet();//获取到选中的工作表,方面后面数据插入操作
$comid = $request->get('comid');
if (empty($request->get('qipingbianhao')))
{
$jinzhandengjis = \app\common\model\Jinzhandengji::where('comid',$comid)
->order('id')
->page($request->get('page'),$request->get('limit'))
->select();
}else{
$qipingbianhao = $request->get('qipingbianhao');
$jinzhandengjis = \app\common\model\Jinzhandengji::where('comid',$comid)
->where('qipingbianhao','like',"%$qipingbianhao%")
->order('id')
->page($request->get('page'),$request->get('limit'))
->select();
}
//此处设置的是生成的excel表的第一行标题
$arr=[
'id'=>'进站登记id',
'chongzhuangdanwei'=>'充装单位',
'qipingbianhao'=>'气瓶编号',
'qipingxinghao'=>'气瓶型号',
'zhizaodanwei'=>'制造单位',
'zhizaoriqi'=>'制造日期',
'gongchengyali'=>'公称压力',
'gongchengrongji'=>'公称容积',
'gangpingzhongliang'=>'钢瓶重量',
'gangpingbihou'=>'钢瓶壁厚',
'daozhanriqi'=>'到站日期',
'waiguanjianchaqingkuang'=>'外观检查情况',
'songjianrenyuan'=>'检送人员',
'jieshourenyuan'=>'接收人员',
'beizhu'=>'备注',
];
//压入数组数据
$jinzhandengjis1[0] = $arr;
foreach ($jinzhandengjis as $v){
array_push($jinzhandengjis1,$v);
}
//dump($jinzhandengjis1);die;
$currow=0;//因为我们生成的excel表格的行数是从1开始,所以我们预先设置好一个变量,供下面foreach循环的$k使用
foreach ($jinzhandengjis1 as $k => $v) {
$currow=$k+1;//表示从生成的excel表格的第一行开始插入数据
$sheet->setCellValue('A'.$currow,$v['id'])//为A列循环插入订单id
->setCellValue('B'.$currow,$v['chongzhuangdanwei'])//为B列循环插入订单编号
->setCellValue('C'.$currow,$v['qipingbianhao'])//为C列循环插入商品总额
->setCellValue('D'.$currow,$v['qipingxinghao'])//为D列循环插入支付状态
->setCellValue('E'.$currow,$v['zhizaodanwei'])//为E列循环插入订单状态
->setCellValue('F'.$currow,$v['zhizaoriqi'])//为F列循环插入配送状态
->setCellValue('G'.$currow,$v['gongchengyali'])//为G列循环插入配送方式
->setCellValue('H'.$currow,$v['gongchengrongji'])//为H列循环插入支付方式
->setCellValue('I'.$currow,$v['gangpingzhongliang'])//为I列循环插入收货人
->setCellValue('J'.$currow,$v['gangpingbihou'])//为J列循环插入手机号
->setCellValue('K'.$currow,$v['daozhanriqi'])//为K列循环插入下单时间
->setCellValue('L'.$currow,$v['waiguanjianchaqingkuang'])//为L列循环插入用户名
->setCellValue('M'.$currow,$v['songjianrenyuan'])//为L列循环插入用户名
->setCellValue('N'.$currow,$v['jieshourenyuan'])//为L列循环插入用户名
->setCellValue('O'.$currow,$v['beizhu'])//为L列循环插入用户名
;
}
header('Content-Type: application/vnd.ms-excel');//设置下载前的头信息
header('Content-Disposition: attachment;filename="气瓶进站登记表.xlsx"');
header('Cache-Control: max-age=0');
$phpwriter=new \PHPExcel_Writer_Excel5($phpexcel);//此处的2007代表的是高版本的excel表格
$phpwriter->save('php://output');//生成并下载excel表格
}