作者 景龙
1 个管道 的构建 通过 耗费 0 秒

修改用户导出功能

@@ -16,6 +16,7 @@ use think\Db; @@ -16,6 +16,7 @@ use think\Db;
16 use think\db\Query; 16 use think\db\Query;
17 use PhpOffice\PhpSpreadsheet\Spreadsheet; 17 use PhpOffice\PhpSpreadsheet\Spreadsheet;
18 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; 18 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  19 +use PhpOffice\PhpSpreadsheet\IOFactory;
19 20
20 /** 21 /**
21 * Class AdminIndexController 22 * Class AdminIndexController
@@ -144,7 +145,20 @@ class AdminIndexController extends AdminBaseController @@ -144,7 +145,20 @@ class AdminIndexController extends AdminBaseController
144 //导出excel 145 //导出excel
145 public function export(){//导出Excel 146 public function export(){//导出Excel
146 $ids = $this->request->param('ids'); 147 $ids = $this->request->param('ids');
147 - $ids = explode(',',$ids); 148 + if(isset($ids) && !empty($ids)){
  149 + $ids = explode(',',$ids);
  150 + $xlsData = Db::name('user')
  151 + ->whereIn('id',$ids)
  152 + ->field('id,user_nickname,user_status,mobile,create_time,last_login_time')
  153 + ->select()
  154 + ->toArray();
  155 + }else{
  156 + $xlsData = Db::name('user')
  157 + ->where('user_type',2)
  158 + ->field('id,user_nickname,user_status,mobile,create_time,last_login_time')
  159 + ->select()
  160 + ->toArray();
  161 + }
148 $xlsName = "用户表数据"; 162 $xlsName = "用户表数据";
149 $xlsCell = array( 163 $xlsCell = array(
150 array('id','序号'), 164 array('id','序号'),
@@ -154,11 +168,7 @@ class AdminIndexController extends AdminBaseController @@ -154,11 +168,7 @@ class AdminIndexController extends AdminBaseController
154 array('last_login_time','最后登录时间'), 168 array('last_login_time','最后登录时间'),
155 array('user_status','状态'), 169 array('user_status','状态'),
156 ); 170 );
157 - $xlsData = Db::name('user')  
158 - ->whereIn('id',$ids)  
159 - ->field('id,user_nickname,user_status,mobile,create_time,last_login_time')  
160 - ->select()  
161 - ->toArray(); 171 +
162 foreach ($xlsData as &$v) { 172 foreach ($xlsData as &$v) {
163 $v['user_status'] = $v['user_status'] == 1?'正常':'已拉黑'; 173 $v['user_status'] = $v['user_status'] == 1?'正常':'已拉黑';
164 $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']); 174 $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
@@ -170,7 +180,6 @@ class AdminIndexController extends AdminBaseController @@ -170,7 +180,6 @@ class AdminIndexController extends AdminBaseController
170 //导出表格 180 //导出表格
171 public function exportExcel($expTitle,$expCellName,$expTableData){ 181 public function exportExcel($expTitle,$expCellName,$expTableData){
172 182
173 - $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称  
174 $fileName = '用户表';//or $xlsTitle 文件名称可根据自己情况设定 183 $fileName = '用户表';//or $xlsTitle 文件名称可根据自己情况设定
175 // $filePath = 'upload/user/'.date('Y-m-d',time()).'.xlsx'; 184 // $filePath = 'upload/user/'.date('Y-m-d',time()).'.xlsx';
176 $topNumber = 1;//表头有几行占用 185 $topNumber = 1;//表头有几行占用
@@ -184,7 +193,7 @@ class AdminIndexController extends AdminBaseController @@ -184,7 +193,7 @@ class AdminIndexController extends AdminBaseController
184 193
185 $spreadsheet = new Spreadsheet(); 194 $spreadsheet = new Spreadsheet();
186 $sheet = $spreadsheet->getActiveSheet(); 195 $sheet = $spreadsheet->getActiveSheet();
187 - $sheet->setTitle($xlsTitle); 196 + $sheet->setTitle($expTitle);
188 // $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s')); 197 // $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
189 //处理表头 198 //处理表头
190 foreach ($expCellName as $k=>$v) { 199 foreach ($expCellName as $k=>$v) {
@@ -211,11 +220,12 @@ class AdminIndexController extends AdminBaseController @@ -211,11 +220,12 @@ class AdminIndexController extends AdminBaseController
211 } 220 }
212 } 221 }
213 ob_end_clean();//清除缓冲区,避免乱码 222 ob_end_clean();//清除缓冲区,避免乱码
214 -// header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件  
215 - header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件  
216 - header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"'); 223 + header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
  224 +// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件
  225 + header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
217 header('Cache-Control: max-age=0'); 226 header('Cache-Control: max-age=0');
218 - $writer = new Xlsx($spreadsheet); 227 +// $writer = new Xlsx($spreadsheet);
  228 + $writer = IOFactory::createWriter($spreadsheet, 'Xls'); //按照指定格式生成Excel文件
219 $writer->save('php://output'); 229 $writer->save('php://output');
220 exit; 230 exit;
221 } 231 }
@@ -109,10 +109,10 @@ @@ -109,10 +109,10 @@
109 $(function(){ 109 $(function(){
110 $('#export').click(function(){ 110 $('#export').click(function(){
111 var check = $('.js-check:checked'); 111 var check = $('.js-check:checked');
112 - if(check.length < 1){  
113 - alert('请选择要导出的数据');  
114 - return false;  
115 - } 112 +// if(check.length < 1){
  113 +// $('#from1').submit();
  114 +// return false;
  115 +// }
116 var chk_value =[]; 116 var chk_value =[];
117 $.each(check,function(){ 117 $.each(check,function(){
118 chk_value.push($(this).val()); 118 chk_value.push($(this).val());