使用ThinkPHP8实现导出Excel数据表格功能
1、开发版本Think PHP8.0、PHP8.0,并非低版不能用,仅因本人当前版本如此。
部分参数需自行进行修改,具体查看执行代码.
Excel有默认的表格样式,如需修改,根据实际应用场景进行设置即可。
2、实现原理
1.安装Spreadsheet
composer require phpoffice/phpspreadsheet2.确定数据表头
$header = [
['key' => 'index', 'title' => '序号'],
['key' => 'activity_title', 'title' => '列1名称'],
['key' => 'room_name', 'title' => '列2名称'],
];3.确定数据列
$list = [];// 定义数据内容,根据实际应用场景来写即可。4.调用封装类,导出数据
3、核心代码
1.调用示例
//表头$header = [
['key' => 'index', 'title' => '序号'],
['key' => 'activity_title', 'title' => '列1名称'],
['key' => 'room_name', 'title' => '列2名称'],
];$list = [];//实例化excel$sheet = new Spreadsheet();//实例化导出类$export = new Excel($sheet, 0);//设置单元格表头$export->setHeader($header);//设置单元格数据$export->setContent($list, $header);//导出:文件名称、sheet名称,返回结果为本地文件存储路径$res = $export->export($fileName, $sheetName);2.Excel核心控制器
<?php
namespace app\common\controller;
/**
* @note Excel操作
*/
class Excel
{
//定义表格对象
protected object $sheet;
public function __construct(object $sheet, $sheetIndex = 0)
{
$this->sheet = $sheet;
if (!is_object($this->sheet)) $this->sheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$this->sheet->getActiveSheet($sheetIndex);
}
/**
* @notes 设置表头
* @param array $header 表头数据
* @param string|int $startRow 默认第一行
*/
public function setHeader(array $header, string|int $startRow = 1): object
{
$header = array_values($header);
//计算总列数
$column = $this->getColumn(count($header));
foreach ($header as $key => $value) {
$columnName = $column[$key] . $startRow;
//设置单元格值
$this->sheet->getActiveSheet()->setCellValue($columnName, $value['title']);
//设置单元格自适应宽度
$this->sheet->getActiveSheet()->getColumnDimension($column[$key])->setAutoSize(true);
//设置单元格自适应高度
$this->sheet->getActiveSheet()->getRowDimension($startRow)->setRowHeight(24);
}
$startColumn = $column . $startRow;
$endColumn = $column . $startRow;
//设置字体大小及加粗
$this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getFont()->setBold(true)->setSize(12);
//设置单元格水平居中
$this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
//设置单元格垂直居中
$this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
//设置单元格边框
$this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
return $this->sheet;
}
/**
* @notes 设置单元格值
* @param array $data 数据
* @param array $header 表头数据
* @param string|int $startRow 默认第二行开始
*/
public function setContent(array $data, array $header, string|int $startRow = 2): object
{
//获取总列数
$column = $this->getColumn(count($header));
//遍历数据
foreach ($data as $key => $value) {
//遍历表头
for ($i = 0; $i < count($header); $i++) {
//获取单元格名称
$columnName = $column[$i] . ($key + $startRow);
//设置单元格值
$this->sheet->getActiveSheet()->setCellValue($columnName, $value[$header[$i]['key']] ?? '');
//设置单元格自适应宽度
$this->sheet->getActiveSheet()->getColumnDimension($column[$i])->setAutoSize(true);
//设置单元格自适应高度
$this->sheet->getActiveSheet()->getRowDimension($key + $startRow)->setRowHeight(24);
}
}
$startColumn = $column . $startRow;
$endColumn = $column . count($data) + $startRow - 1;
//设置字体大小及加粗
$this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getFont()->setBold(false)->setSize(11);
//设置单元格水平居中
$this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
//设置单元格垂直居中
$this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
//设置单元格边框
$this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
return $this->sheet;
}
/**
* @notes 导出数据
* @param string $fileName 文件名
* @param string $sheetName 表名
* @return string
*/
public function export(string $fileName, string $sheetName = 'Sheet1'): string
{
//设置表格标题
$this->sheet->getActiveSheet()->setTitle($sheetName);
//设置表格格式
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->sheet);
//设置存储路径
$basePath = public_path();
$path = 'activity_sequence_template/';
$fullPath = $basePath . $path . $fileName . '.xlsx';
if (!is_dir($basePath . $path)) mkdir($basePath . $path, 0777, true);
$writer->save($fullPath);
return $path . $fileName . '.xlsx';
// //设置响应头
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
// header('Cache-Control: max-age=0');
// //导出数据
// $writer->save('php://output');
}
/**
* @notes 自动计算列数
* @param int|string $colNumber
* @return array
*/
protected function getColumn(int|string $colNumber = 1): array
{
//生成A-Z的数组
$arr = range('A', 'Z');
//计算循环次数
$no = ceil($colNumber / count($arr));
//定义数组
$data = [];
if ($no <= 1) {
for ($i = 0; $i < $colNumber; $i++) {
$data[] = $arr[$i];
}
} else {
for ($i = 0; $i < count($arr); $i++) {
$data[] = $arr[$i];
}
for ($i = 0; $i < $colNumber - count($arr); $i++) {
$list = (($i + count($arr)) % count($arr));
$data[] = $arr . $arr[$list];
}
}
return $data;
}
}到此这篇关于使用ThinkPHP8实现导出Excel数据表格功能的文章就介绍到这了,更多相关ThinkPHP8导出Excel数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://www.jb51.net/program/320533mk6.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]