...
|
...
|
@@ -16,6 +16,7 @@ use think\Db; |
|
|
use think\db\Query;
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
|
|
|
/**
|
|
|
* Class AdminIndexController
|
...
|
...
|
@@ -144,7 +145,20 @@ class AdminIndexController extends AdminBaseController |
|
|
//导出excel
|
|
|
public function export(){//导出Excel
|
|
|
$ids = $this->request->param('ids');
|
|
|
if(isset($ids) && !empty($ids)){
|
|
|
$ids = explode(',',$ids);
|
|
|
$xlsData = Db::name('user')
|
|
|
->whereIn('id',$ids)
|
|
|
->field('id,user_nickname,user_status,mobile,create_time,last_login_time')
|
|
|
->select()
|
|
|
->toArray();
|
|
|
}else{
|
|
|
$xlsData = Db::name('user')
|
|
|
->where('user_type',2)
|
|
|
->field('id,user_nickname,user_status,mobile,create_time,last_login_time')
|
|
|
->select()
|
|
|
->toArray();
|
|
|
}
|
|
|
$xlsName = "用户表数据";
|
|
|
$xlsCell = array(
|
|
|
array('id','序号'),
|
...
|
...
|
@@ -154,11 +168,7 @@ class AdminIndexController extends AdminBaseController |
|
|
array('last_login_time','最后登录时间'),
|
|
|
array('user_status','状态'),
|
|
|
);
|
|
|
$xlsData = Db::name('user')
|
|
|
->whereIn('id',$ids)
|
|
|
->field('id,user_nickname,user_status,mobile,create_time,last_login_time')
|
|
|
->select()
|
|
|
->toArray();
|
|
|
|
|
|
foreach ($xlsData as &$v) {
|
|
|
$v['user_status'] = $v['user_status'] == 1?'正常':'已拉黑';
|
|
|
$v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
|
...
|
...
|
@@ -170,7 +180,6 @@ class AdminIndexController extends AdminBaseController |
|
|
//导出表格
|
|
|
public function exportExcel($expTitle,$expCellName,$expTableData){
|
|
|
|
|
|
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
|
|
|
$fileName = '用户表';//or $xlsTitle 文件名称可根据自己情况设定
|
|
|
// $filePath = 'upload/user/'.date('Y-m-d',time()).'.xlsx';
|
|
|
$topNumber = 1;//表头有几行占用
|
...
|
...
|
@@ -184,7 +193,7 @@ class AdminIndexController extends AdminBaseController |
|
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
$sheet->setTitle($xlsTitle);
|
|
|
$sheet->setTitle($expTitle);
|
|
|
// $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
|
|
|
//处理表头
|
|
|
foreach ($expCellName as $k=>$v) {
|
...
|
...
|
@@ -211,11 +220,12 @@ class AdminIndexController extends AdminBaseController |
|
|
}
|
|
|
}
|
|
|
ob_end_clean();//清除缓冲区,避免乱码
|
|
|
// header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
|
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件
|
|
|
header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"');
|
|
|
header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
|
|
|
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件
|
|
|
header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
|
|
|
header('Cache-Control: max-age=0');
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
// $writer = new Xlsx($spreadsheet);
|
|
|
$writer = IOFactory::createWriter($spreadsheet, 'Xls'); //按照指定格式生成Excel文件
|
|
|
$writer->save('php://output');
|
|
|
exit;
|
|
|
}
|
...
|
...
|
|