|
|
@ -23,6 +23,10 @@ use cores\exception\BaseException; |
|
|
|
use cores\exception\DebugException; |
|
|
|
use cores\exception\DebugException; |
|
|
|
use think\exception\HttpResponseException; |
|
|
|
use think\exception\HttpResponseException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
|
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
|
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 打印调试函数 html |
|
|
|
* 打印调试函数 html |
|
|
|
* @param $content |
|
|
|
* @param $content |
|
|
@ -466,3 +470,51 @@ function calc_time($startTime, $endTime) { |
|
|
|
} |
|
|
|
} |
|
|
|
return date('i分钟s秒', (3600 - $diffTime)); |
|
|
|
return date('i分钟s秒', (3600 - $diffTime)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('downLoadExcel')){ |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 导出excel |
|
|
|
|
|
|
|
* @param $name excel名称 |
|
|
|
|
|
|
|
* @param $titles 标题 [['name'=>'姓名'],['gender'=>'性别']] |
|
|
|
|
|
|
|
* @param array $data |
|
|
|
|
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
|
|
|
|
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function downLoadExcel($name, $titles, $data=[]) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$count = count($titles); //计算表头数量 |
|
|
|
|
|
|
|
$spreadsheet = new Spreadsheet(); |
|
|
|
|
|
|
|
$styleArray = [ |
|
|
|
|
|
|
|
'alignment' => [ |
|
|
|
|
|
|
|
'horizontal' => Alignment::HORIZONTAL_CENTER_CONTINUOUS, |
|
|
|
|
|
|
|
'vertical' => Alignment::VERTICAL_CENTER, |
|
|
|
|
|
|
|
'wrapText' => true, |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
|
|
|
|
|
|
|
for ($i = 65; $i < $count + 65; $i++) { //数字转字母从65开始,循环设置表头 |
|
|
|
|
|
|
|
$sheet->getStyle(strtoupper(chr($i)))->applyFromArray($styleArray); |
|
|
|
|
|
|
|
$sheet->getCell(strtoupper(chr($i)).'1')->getStyle()->getFont()->setBold(true); |
|
|
|
|
|
|
|
$index = $i - 65; |
|
|
|
|
|
|
|
$sheet->setCellValue(strtoupper(chr($i)) . '1', $titles[$index][key($titles[$index])] ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*--------------开始从数据库提取信息插入Excel表中------------------*/ |
|
|
|
|
|
|
|
foreach ($data as $key => $item) { //循环设置单元格: |
|
|
|
|
|
|
|
//$key+2,因为第一行是表头,所以写到表格时 从第二行开始写 |
|
|
|
|
|
|
|
for ($i = 65; $i < $count + 65; $i++) { //数字转字母从65开始: |
|
|
|
|
|
|
|
$sheet->setCellValue(strtoupper(chr($i)) . ($key + 2),$item[key($titles[$i - 65])]); |
|
|
|
|
|
|
|
$spreadsheet->getActiveSheet()->getColumnDimension(strtoupper(chr($i)))->setAutoSize(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
header('Content-Type: application/vnd.ms-excel'); |
|
|
|
|
|
|
|
header('Content-Disposition: attachment;filename="' . $name . '.xlsx"'); |
|
|
|
|
|
|
|
header('Cache-Control: max-age=0'); |
|
|
|
|
|
|
|
$writer = IOFactory::createWriter($spreadsheet,'Xlsx'); |
|
|
|
|
|
|
|
$writer->save('php://output'); |
|
|
|
|
|
|
|
//删除清空 |
|
|
|
|
|
|
|
$spreadsheet->disconnectWorksheets(); |
|
|
|
|
|
|
|
unset($spreadsheet); |
|
|
|
|
|
|
|
exit; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|