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

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

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

1、開發(fā)版本

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

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

Excel有默認的表格樣式,如需修改,根據(jù)實際應用場景進行設置即可。

2、實現(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ù)內容,根據(jù)實際應用場景來寫即可。

4.調用封裝類,導出數(shù)據(jù)

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);
//  設置單元格數(shù)據(jù)
$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 表頭數(shù)據(jù)
     * @param string|int $startRow 默認第一行
     */
    public function setHeader(array $header, string|int $startRow = 1): object
    {
        $header = array_values($header);
        //  計算總列數(shù)
        $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[0] . $startRow;
        $endColumn = $column[count($header) - 1] . $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 數(shù)據(jù)
     * @param array $header 表頭數(shù)據(jù)
     * @param string|int $startRow 默認第二行開始
     */
    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);
                //  設置單元格值
                $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[0] . $startRow;
        $endColumn = $column[count($column) - 1] . 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 導出數(shù)據(jù)
     * @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');
//        //  導出數(shù)據(jù)
//        $writer->save('php://output');
    }
 
    /**
     * @notes 自動計算列數(shù)
     * @param int|string $colNumber
     * @return array
     */
    protected function getColumn(int|string $colNumber = 1): array
    {
        //  生成A-Z的數(shù)組
        $arr = range('A', 'Z');
        //  計算循環(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;
    }
}

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

相關文章

最新評論

梅河口市| 乐都县| 浮山县| 寿光市| 徐水县| 永康市| 荆门市| 柘荣县| 徐水县| 菏泽市| 嘉黎县| 廉江市| 邹城市| 新蔡县| 宽甸| 通城县| 安溪县| 汉沽区| 罗田县| 浑源县| 光泽县| 玉溪市| 青河县| 桃园市| 新河县| 林芝县| 泸西县| 新昌县| 瓦房店市| 金溪县| 平舆县| 泽州县| 唐海县| 大荔县| 长沙市| 土默特左旗| 克拉玛依市| 赤城县| 承德县| 云阳县| 新巴尔虎左旗|