...
|
...
|
@@ -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;
|
|
|
}
|
|
|
} |
...
|
...
|
|