作者 jinglong

调试导出

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
/**
* 后台公共
*
* @icon fa fa-circle-o
*/
class AdminCommon extends Backend
{
//导出表格
public static function exportExcel($expTitle,$expCellName,$expTableData,$fileName){
$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(20);//每列宽度
$sheet->getColumnDimension($cellKey[$k1])->setWidth($v1[2]);//每列宽度
$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;
}
}
... ...
... ... @@ -3,15 +3,6 @@
namespace app\admin\controller;
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;
/**
* 免费预约管理
... ... @@ -113,12 +104,11 @@ class Free extends Backend
return;
}
$ids = explode(',',$ids);
$xlsData = Db::name('free')
$expTableData = Db::name('free')
->whereIn('id',$ids)
->field('id,user_name,mobile,province,city,district,address,advice,status,createtime')
->select();
$xlsName = "预约体验表";
$xlsCell = [
$expCellName = [
['id','ID',5],
['user_name','姓名',10],
['mobile','电话',20],
... ... @@ -131,60 +121,12 @@ class Free extends Backend
['createtime','申请时间',20],
];
foreach ($xlsData as &$v) {
foreach ($expTableData 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(20);//每列宽度
$sheet->getColumnDimension($cellKey[$k1])->setWidth($v1[2]);//每列宽度
$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;
$expTitle = "预约体验表";
$fileName = "预约体验表";//文件名
AdminCommon::exportExcel($expTitle,$expCellName,$expTableData,$fileName);
}
}
... ...