下载PHPExcel类文件到vendor目录
https://pan.baidu.com/s/1QfiRJhifM12OYbHJzLF3vw
HTML代码
<form class="layui-form layui-form-pane" action="{:url('admin/express_order/index')}" method="get">
<div class="layui-inline">
<label class="layui-form-label">名字</label>
<div class="layui-input-inline">
<input type="text" name="keyword" value="{$keyword}" placeholder="请输入名字" class="layui-input">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">学校</label>
<div class="layui-input-inline">
<select name="school_id">
<option value="">请选择</option>
{foreach name="school_list" item="vo"}
<option value="{$vo.id}"{if condition="$school_id==$vo.id"} selected="selected"{/if}>{$vo.name}</option>
{/foreach}
</select>
</div>
</div>
<div class="layui-inline">
<button class="layui-btn">搜索</button>
</div>
</form>
<div class="layui-inline">
<button class="layui-btn"><a href="{:url('admin/express_order/daochu',['keyword'=>$keyword,'school_id'=>$school_id])}">导出表格</a></button>
</div>
TP5代码
//导出xls
public function daochu($keyword = '', $school_id = '')
{
//1.从数据库中取出数据
$map = [];
if ($keyword) {
$map['name'] = ['like', "%{$keyword}%"];
}
if ($school_id) {
$map[] = ['exp', "FIND_IN_SET($school_id,school_id)"];
}
$list = $this->express_order_model->where($map)->order('id DESC')->select();
foreach ($list as $key => $value) {
$list[$key]['school_name'] = SchoolModel::get_school_name($value['school_id']);
$receive = ReceiveModel::where('user_id', $value['receiver_id'])->find();
$list[$key]['receive_name'] = $receive['name'];
if($value['status']==1){
$list[$key]['status'] = "未付款";
}elseif($value['status']==2){
$list[$key]['status'] = "已付款";
}elseif($value['status']==3){
$list[$key]['status'] = "派送中";
}elseif($value['status']==4){
$list[$key]['status'] = "已完成";
}elseif($value['status']==5){
$list[$key]['status'] = "已取消";
}elseif($value['status']==6){
$list[$key]['status'] = "取消审核中";
}
}
//halt($list);
//2.加载PHPExcle类库
vendor('PHPExcel.PHPExcel');
//3.实例化PHPExcel类
$objPHPExcel = new \PHPExcel();
//4.激活当前的sheet表
$objPHPExcel->setActiveSheetIndex(0);
//5.设置表格头(即excel表格的第一行)
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ID')
->setCellValue('B1', '订单编号')
->setCellValue('C1', '学校')
->setCellValue('D1', '接单员名字')
->setCellValue('E1', '名字')
->setCellValue('F1', '手机')
->setCellValue('G1', '地址')
->setCellValue('H1', '价格')
->setCellValue('I1', '状态')
->setCellValue('J1', '创建时间');
//设置A列水平居中
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置单元格宽度
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
//6.循环刚取出来的数组,将数据逐一添加到excel表格。
//
for ($i = 0; $i < count($list); $i++) {
$objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['id']); //ID
$objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['order_sn']); //
$objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['school_name']); //
$objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['receive_name']); //
$objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['name']); //
$objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['mobile']); //
$objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['address']); //
$objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['price']);
$objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['status']); //
$objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $list[$i]['create_time']); //
}
//7.设置保存的Excel表格名称
$filename = '快递订单' . date('ymd', time()) . '.xls';
//8.设置当前激活的sheet表格名称;
$objPHPExcel->getActiveSheet()->setTitle('快递订单');
//9.设置浏览器窗口下载表格
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="' . $filename . '"');
//生成excel文件
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//下载文件在浏览器窗口
$objWriter->save('php://output');
exit;
}
本文为Pasa吴原创文章,转载无需和我联系,但请注明来自Pasa吴博客https://www.pasawu.top
最新评论