SchoolPhysicalExamination/application/admin/controller/Download.php

141 lines
4.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\bj\controller\Common;
use think\Log;
use \think\Validate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
class Download extends Controller{
protected $ceshiyong_token = 'caadd1be045a65f30b92aa805f1de54a';
// // 测试后端判断是否微信环境
// function is_wechat() {
// // 获取 User-Agent 字符串
// $userAgent = $_SERVER['HTTP_USER_AGENT'];
// // 检查 User-Agent 字符串中是否包含 "MicroMessenger"
// if (strpos($userAgent, 'MicroMessenger') !== false) {
// echo "是微信浏览器";
// }else{
// echo "不是微信浏览器";
// }
// }
/**
* 下载 Excel 文件
*
* @param array $data 要导出的数据
* @param string $filename 文件名
* @return void
*/
public function downloadExcel($ceshiyong_data, $filename = 'data.xlsx')
{
// 创建一个新的 Spreadsheet 对象
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 获取列数
$columnCount = count($ceshiyong_data[0]);
// 添加标题到工作表中
$titles = $ceshiyong_data[0];
for ($colIndex = 0; $colIndex < $columnCount; $colIndex++) {
$sheet->setCellValueByColumnAndRow($colIndex + 1, 1, $titles[$colIndex]);
}
// 添加数据到工作表中
$data = array_slice($ceshiyong_data, 1);
foreach ($data as $rowIndex => $rowData) {
for ($colIndex = 0; $colIndex < $columnCount; $colIndex++) {
$sheet->setCellValueByColumnAndRow($colIndex + 1, $rowIndex + 2, $rowData[$colIndex]);
}
}
// 设置表头样式
$headerStyle = [
'font' => [
'bold' => true,
],
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => [
'argb' => 'FFFFC000', // 橙色
],
],
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
],
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
],
],
];
$sheet->getStyle('A1:' . $this->getExcelColumnName($columnCount) . '1')->applyFromArray($headerStyle);
// 设置所有内容居中
$allContentStyle = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
],
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
],
],
];
$sheet->getStyle('A1:' . $this->getExcelColumnName($columnCount) . (count($ceshiyong_data) + 1))->applyFromArray($allContentStyle);
// 设置斑马纹效果
$rowCount = count($ceshiyong_data) - 1; // 数据行数
for ($rowIndex = 1; $rowIndex <= $rowCount; $rowIndex++) {
$row = $rowIndex + 1; // 数据行从第二行开始
$rowStyle = [
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => [
'argb' => ($rowIndex % 2 == 0) ? 'FFDDDDDD' : 'FFFFFFFF', // 偶数行浅灰色,奇数行白色
],
],
];
$sheet->getStyle('A' . $row . ':' . $this->getExcelColumnName($columnCount) . $row)->applyFromArray($rowStyle);
}
// 设置响应头以触发文件下载
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
// 保存并输出 Excel 文件
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit;
}
/**
* 获取 Excel 列名
*
* @param int $index 列索引从1开始
* @return string Excel 列名
*/
private function getExcelColumnName($index)
{
$columnName = '';
while ($index > 0) {
$remainder = ($index - 1) % 26;
$columnName = chr(65 + $remainder) . $columnName;
$index = intval(($index - 1) / 26);
}
return $columnName;
}
}