最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用ThinkPHP8實(shí)現(xiàn)導(dǎo)出Excel數(shù)據(jù)表格功能

 更新時(shí)間:2024年05月02日 08:17:57   作者:一一程序  
這篇文章主要為大家詳細(xì)介紹了如何使用ThinkPHP8導(dǎo)出Excel數(shù)據(jù)表格功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1、開發(fā)版本

Think PHP8.0、PHP8.0,并非低版不能用,僅因本人當(dāng)前版本如此。

部分參數(shù)需自行進(jìn)行修改,具體查看執(zhí)行代碼.

Excel有默認(rèn)的表格樣式,如需修改,根據(jù)實(shí)際應(yīng)用場景進(jìn)行設(shè)置即可。

2、實(shí)現(xiàn)原理

1.安裝Spreadsheet

composer require phpoffice/phpspreadsheet

2.確定數(shù)據(jù)表頭

$header = [
  ['key' => 'index', 'title' => '序號'],
  ['key' => 'activity_title', 'title' => '列1名稱'],
  ['key' => 'room_name', 'title' => '列2名稱'],
];

3.確定數(shù)據(jù)列

$list = [];  //    定義數(shù)據(jù)內(nèi)容,根據(jù)實(shí)際應(yīng)用場景來寫即可。

4.調(diào)用封裝類,導(dǎo)出數(shù)據(jù)

3、核心代碼

1.調(diào)用示例

 //  表頭
$header = [
  ['key' => 'index', 'title' => '序號'],
  ['key' => 'activity_title', 'title' => '列1名稱'],
  ['key' => 'room_name', 'title' => '列2名稱'],
];
$list = [];
//  實(shí)例化excel
$sheet = new Spreadsheet();
//  實(shí)例化導(dǎo)出類
$export = new Excel($sheet, 0);
//  設(shè)置單元格表頭
$export->setHeader($header);
//  設(shè)置單元格數(shù)據(jù)
$export->setContent($list, $header);
//  導(dǎo)出:文件名稱、sheet名稱,返回結(jié)果為本地文件存儲路徑
$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 設(shè)置表頭
     * @param array $header 表頭數(shù)據(jù)
     * @param string|int $startRow 默認(rèn)第一行
     */
    public function setHeader(array $header, string|int $startRow = 1): object
    {
        $header = array_values($header);
        //  計(jì)算總列數(shù)
        $column = $this->getColumn(count($header));
        foreach ($header as $key => $value) {
            $columnName = $column[$key] . $startRow;
            //  設(shè)置單元格值
            $this->sheet->getActiveSheet()->setCellValue($columnName, $value['title']);
            //  設(shè)置單元格自適應(yīng)寬度
            $this->sheet->getActiveSheet()->getColumnDimension($column[$key])->setAutoSize(true);
            //  設(shè)置單元格自適應(yīng)高度
            $this->sheet->getActiveSheet()->getRowDimension($startRow)->setRowHeight(24);
        }
        $startColumn = $column[0] . $startRow;
        $endColumn = $column[count($header) - 1] . $startRow;
        //  設(shè)置字體大小及加粗
        $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getFont()->setBold(true)->setSize(12);
        //  設(shè)置單元格水平居中
        $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
        //  設(shè)置單元格垂直居中
        $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
        //  設(shè)置單元格邊框
        $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
        return $this->sheet;
    }
 
    /**
     * @notes 設(shè)置單元格值
     * @param array $data 數(shù)據(jù)
     * @param array $header 表頭數(shù)據(jù)
     * @param string|int $startRow 默認(rèn)第二行開始
     */
    public function setContent(array $data, array $header, string|int $startRow = 2): object
    {
        //  獲取總列數(shù)
        $column = $this->getColumn(count($header));
        //  遍歷數(shù)據(jù)
        foreach ($data as $key => $value) {
            //  遍歷表頭
            for ($i = 0; $i < count($header); $i++) {
                //  獲取單元格名稱
                $columnName = $column[$i] . ($key + $startRow);
                //  設(shè)置單元格值
                $this->sheet->getActiveSheet()->setCellValue($columnName, $value[$header[$i]['key']] ?? '');
                //  設(shè)置單元格自適應(yīng)寬度
                $this->sheet->getActiveSheet()->getColumnDimension($column[$i])->setAutoSize(true);
                //  設(shè)置單元格自適應(yīng)高度
                $this->sheet->getActiveSheet()->getRowDimension($key + $startRow)->setRowHeight(24);
            }
        }
        $startColumn = $column[0] . $startRow;
        $endColumn = $column[count($column) - 1] . count($data) + $startRow - 1;
        //  設(shè)置字體大小及加粗
        $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getFont()->setBold(false)->setSize(11);
        //  設(shè)置單元格水平居中
        $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
        //  設(shè)置單元格垂直居中
        $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
        //  設(shè)置單元格邊框
        $this->sheet->getActiveSheet()->getStyle($startColumn . ':' . $endColumn)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
        return $this->sheet;
    }
 
    /**
     * @notes 導(dǎo)出數(shù)據(jù)
     * @param string $fileName 文件名
     * @param string $sheetName 表名
     * @return string
     */
    public function export(string $fileName, string $sheetName = 'Sheet1'): string
    {
        //  設(shè)置表格標(biāo)題
        $this->sheet->getActiveSheet()->setTitle($sheetName);
        //  設(shè)置表格格式
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->sheet);
        //  設(shè)置存儲路徑
        $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';
//        //  設(shè)置響應(yīng)頭
//        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//        header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
//        header('Cache-Control: max-age=0');
//        //  導(dǎo)出數(shù)據(jù)
//        $writer->save('php://output');
    }
 
    /**
     * @notes 自動(dòng)計(jì)算列數(shù)
     * @param int|string $colNumber
     * @return array
     */
    protected function getColumn(int|string $colNumber = 1): array
    {
        //  生成A-Z的數(shù)組
        $arr = range('A', 'Z');
        //  計(jì)算循環(huán)次數(shù)
        $no = ceil($colNumber / count($arr));
        //  定義數(shù)組
        $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[ceil(($i + 1) / count($arr)) - 1] . $arr[$list];
            }
        }
        return $data;
    }
}

到此這篇關(guān)于使用ThinkPHP8實(shí)現(xiàn)導(dǎo)出Excel數(shù)據(jù)表格功能的文章就介紹到這了,更多相關(guān)ThinkPHP8導(dǎo)出Excel數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

启东市| 房产| 运城市| 金塔县| 高碑店市| 潍坊市| 黄龙县| 仲巴县| 秦安县| 财经| 彭阳县| 隆德县| 北海市| 玉林市| 星子县| 光泽县| 高密市| 河北区| 平利县| 麟游县| 盐边县| 华安县| 丽水市| 东乌珠穆沁旗| 绥中县| 鄂温| 大英县| 黎川县| 靖安县| 吕梁市| 中宁县| 柘城县| 台东县| 上饶市| 白朗县| 汾西县| 丰顺县| 隆尧县| 建阳市| 万山特区| 宜兴市|