作者 jinglong

调试导出

@@ -6,9 +6,11 @@ use app\common\controller\Backend; @@ -6,9 +6,11 @@ use app\common\controller\Backend;
6 use app\admin\library\Auth; 6 use app\admin\library\Auth;
7 use Exception; 7 use Exception;
8 use PhpOffice\PhpSpreadsheet\Cell\Coordinate; 8 use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  9 +use PhpOffice\PhpSpreadsheet\IOFactory;
9 use PhpOffice\PhpSpreadsheet\Reader\Xlsx; 10 use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
10 use PhpOffice\PhpSpreadsheet\Reader\Xls; 11 use PhpOffice\PhpSpreadsheet\Reader\Xls;
11 use PhpOffice\PhpSpreadsheet\Reader\Csv; 12 use PhpOffice\PhpSpreadsheet\Reader\Csv;
  13 +use PhpOffice\PhpSpreadsheet\Spreadsheet;
12 use think\exception\PDOException; 14 use think\exception\PDOException;
13 use think\Db; 15 use think\Db;
14 /** 16 /**
@@ -103,4 +105,85 @@ class Free extends Backend @@ -103,4 +105,85 @@ class Free extends Backend
103 public function import(){ 105 public function import(){
104 return parent::import(); 106 return parent::import();
105 } 107 }
  108 +
  109 + //导出excel
  110 + public function export(){//导出Excel
  111 + $ids = $this->request->param('ids');
  112 + if(!$ids){
  113 + return;
  114 + }
  115 + $ids = explode(',',$ids);
  116 + $xlsData = Db::name('free')
  117 + ->whereIn('id',$ids)
  118 + ->field('id,user_name,mobile,province,city,district,address,advice,status,createtime')
  119 + ->select();
  120 + $xlsName = "预约体验表";
  121 + $xlsCell = [
  122 + ['id','ID'],
  123 + ['user_name','姓名'],
  124 + ['mobile','电话'],
  125 + ['province','省'],
  126 + ['city','市'],
  127 + ['district','区'],
  128 + ['address','详细地址'],
  129 + ['status','申请状态'],
  130 + ['advice','所属顾问'],
  131 + ['createtime','申请时间'],
  132 + ];
  133 +
  134 + foreach ($xlsData as &$v) {
  135 + $v['status'] = $v['status'] == 0?'未处理':'已处理';
  136 + $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
  137 + }
  138 + $this->exportExcel($xlsName,$xlsCell,$xlsData);
  139 + }
  140 +
  141 + //导出表格
  142 + public function exportExcel($expTitle,$expCellName,$expTableData){
  143 +
  144 + $fileName = '预约体验表';//or $xlsTitle 文件名称可根据自己情况设定
  145 +// $filePath = 'upload/user/'.date('Y-m-d',time()).'.xlsx';
  146 + $topNumber = 1;//表头有几行占用
  147 + $cellKey = array(
  148 + 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  149 + 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  150 + 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM',
  151 + 'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'
  152 + );
  153 + $spreadsheet = new Spreadsheet();
  154 + $sheet = $spreadsheet->getActiveSheet();
  155 + $sheet->setTitle($expTitle);
  156 +// $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
  157 + //处理表头
  158 + foreach ($expCellName as $k=>$v) {
  159 + $sheet->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据
  160 +// $spreadsheet->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口
  161 + $sheet->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗
  162 + }
  163 + //处理数据
  164 + //设置单元格居中显示
  165 + $styleArray = [
  166 + 'alignment' => [
  167 + 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  168 + ],
  169 + ];
  170 + //表头居中
  171 + foreach ($expCellName as $k2=>$v2) {
  172 + $sheet->getStyle($cellKey[$k2].'1')->applyFromArray($styleArray);
  173 + }
  174 + foreach ($expTableData as $k=>$v) {
  175 + foreach ($expCellName as $k1=>$v1) {
  176 + $sheet->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]);
  177 + $sheet->getColumnDimension($cellKey[$k1])->setWidth(30);//每列宽度
  178 + $sheet->getStyle($cellKey[$k1].($k+1+$topNumber))->applyFromArray($styleArray);
  179 + }
  180 + }
  181 + ob_end_clean();//清除缓冲区,避免乱码
  182 + header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
  183 + header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
  184 + header('Cache-Control: max-age=0');
  185 + $writer = IOFactory::createWriter($spreadsheet, 'Xls'); //按照指定格式生成Excel文件
  186 + $writer->save('php://output');
  187 + exit;
  188 + }
106 } 189 }
@@ -64,12 +64,4 @@ class Platform extends Backend @@ -64,12 +64,4 @@ class Platform extends Backend
64 } 64 }
65 return $this->view->fetch(); 65 return $this->view->fetch();
66 } 66 }
67 -  
68 - /**  
69 - * 导入  
70 - */  
71 - public function import(){  
72 - return parent::import();  
73 - }  
74 -  
75 } 67 }
@@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
11 <!--<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('free/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>--> 11 <!--<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('free/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>-->
12 <a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('free/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a> 12 <a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('free/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
13 <a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('free/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a> 13 <a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('free/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>
  14 + <a href="javascript:;" class="btn btn-success btn-export {:$auth->check('free/export')?'':'hide'}" title="{:__('Export')}" id="btn-export-file"><i class="fa fa-download"></i> {:__('Export')}</a>
14 15
15 <!--<div class="dropdown btn-group {:$auth->check('free/multi')?'':'hide'}">--> 16 <!--<div class="dropdown btn-group {:$auth->check('free/multi')?'':'hide'}">-->
16 <!--<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>--> 17 <!--<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>-->
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 <!--<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('platform/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>--> 10 <!--<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('platform/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>-->
11 <a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('platform/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a> 11 <a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('platform/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
12 <!--<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('platform/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>--> 12 <!--<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('platform/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>-->
13 - <a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('platform/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a> 13 + <!--<a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('platform/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>-->
14 14
15 <!--<div class="dropdown btn-group {:$auth->check('platform/multi')?'':'hide'}">--> 15 <!--<div class="dropdown btn-group {:$auth->check('platform/multi')?'':'hide'}">-->
16 <!--<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>--> 16 <!--<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>-->
@@ -16,7 +16,25 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin @@ -16,7 +16,25 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
16 }); 16 });
17 17
18 var table = $("#table"); 18 var table = $("#table");
19 - 19 + var submitForm = function (ids, layero) {
  20 + if(!ids){
  21 + return false;
  22 + }
  23 + $("input[name=ids]", layero).val(ids);
  24 + $("form", layero).submit();
  25 + };
  26 + $(document).on("click", ".btn-export", function () {
  27 + var ids = Table.api.selectedids(table);
  28 + Layer.confirm("选择导出的选项<form action='" + Fast.api.fixurl("free/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value=''/></form>", {
  29 + title: '导出数据',
  30 + btn: ["选中项(" + ids.length + "条)"],
  31 + success: function (layero, index) {
  32 + $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
  33 + }, yes: function (index, layero) {
  34 + submitForm(ids.join(","), layero);
  35 + }
  36 + });
  37 + });
20 // 初始化表格 38 // 初始化表格
21 table.bootstrapTable({ 39 table.bootstrapTable({
22 url: $.fn.bootstrapTable.defaults.extend.index_url, 40 url: $.fn.bootstrapTable.defaults.extend.index_url,
@@ -10,7 +10,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin @@ -10,7 +10,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
10 edit_url: 'platform/edit', 10 edit_url: 'platform/edit',
11 // del_url: 'platform/del', 11 // del_url: 'platform/del',
12 multi_url: 'platform/multi', 12 multi_url: 'platform/multi',
13 - import_url: 'platform/import', 13 + // import_url: 'platform/import',
14 table: 'platform', 14 table: 'platform',
15 } 15 }
16 }); 16 });