作者 jinglong

调试导出

... ... @@ -6,9 +6,11 @@ use app\common\controller\Backend;
use app\admin\library\Auth;
use Exception;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\exception\PDOException;
use think\Db;
/**
... ... @@ -103,4 +105,85 @@ class Free extends Backend
public function import(){
return parent::import();
}
//导出excel
public function export(){//导出Excel
$ids = $this->request->param('ids');
if(!$ids){
return;
}
$ids = explode(',',$ids);
$xlsData = Db::name('free')
->whereIn('id',$ids)
->field('id,user_name,mobile,province,city,district,address,advice,status,createtime')
->select();
$xlsName = "预约体验表";
$xlsCell = [
['id','ID'],
['user_name','姓名'],
['mobile','电话'],
['province','省'],
['city','市'],
['district','区'],
['address','详细地址'],
['status','申请状态'],
['advice','所属顾问'],
['createtime','申请时间'],
];
foreach ($xlsData as &$v) {
$v['status'] = $v['status'] == 0?'未处理':'已处理';
$v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
}
$this->exportExcel($xlsName,$xlsCell,$xlsData);
}
//导出表格
public function exportExcel($expTitle,$expCellName,$expTableData){
$fileName = '预约体验表';//or $xlsTitle 文件名称可根据自己情况设定
// $filePath = 'upload/user/'.date('Y-m-d',time()).'.xlsx';
$topNumber = 1;//表头有几行占用
$cellKey = array(
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM',
'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'
);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle($expTitle);
// $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
//处理表头
foreach ($expCellName as $k=>$v) {
$sheet->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据
// $spreadsheet->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口
$sheet->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗
}
//处理数据
//设置单元格居中显示
$styleArray = [
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
//表头居中
foreach ($expCellName as $k2=>$v2) {
$sheet->getStyle($cellKey[$k2].'1')->applyFromArray($styleArray);
}
foreach ($expTableData as $k=>$v) {
foreach ($expCellName as $k1=>$v1) {
$sheet->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]);
$sheet->getColumnDimension($cellKey[$k1])->setWidth(30);//每列宽度
$sheet->getStyle($cellKey[$k1].($k+1+$topNumber))->applyFromArray($styleArray);
}
}
ob_end_clean();//清除缓冲区,避免乱码
header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xls'); //按照指定格式生成Excel文件
$writer->save('php://output');
exit;
}
}
... ...
... ... @@ -64,12 +64,4 @@ class Platform extends Backend
}
return $this->view->fetch();
}
/**
* 导入
*/
public function import(){
return parent::import();
}
}
... ...
... ... @@ -11,6 +11,7 @@
<!--<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>-->
<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>
<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>
<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>
<!--<div class="dropdown btn-group {:$auth->check('free/multi')?'':'hide'}">-->
<!--<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 @@
<!--<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('platform/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>-->
<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>
<!--<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>-->
<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>
<!--<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>-->
<!--<div class="dropdown btn-group {:$auth->check('platform/multi')?'':'hide'}">-->
<!--<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
});
var table = $("#table");
var submitForm = function (ids, layero) {
if(!ids){
return false;
}
$("input[name=ids]", layero).val(ids);
$("form", layero).submit();
};
$(document).on("click", ".btn-export", function () {
var ids = Table.api.selectedids(table);
Layer.confirm("选择导出的选项<form action='" + Fast.api.fixurl("free/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value=''/></form>", {
title: '导出数据',
btn: ["选中项(" + ids.length + "条)"],
success: function (layero, index) {
$(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
}, yes: function (index, layero) {
submitForm(ids.join(","), layero);
}
});
});
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
... ...
... ... @@ -10,7 +10,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
edit_url: 'platform/edit',
// del_url: 'platform/del',
multi_url: 'platform/multi',
import_url: 'platform/import',
// import_url: 'platform/import',
table: 'platform',
}
});
... ...