php-xlswriter实现数据导出excel单元格合并,内容从指定行开始写

最终效果图:

代码:

php 复制代码
public function export_data() {
        $list = $this->get_list_organ();
        $content = [];
        $content[] = []; // 第2行不设置内容,设置为空
        foreach ($list as $key => $value) {
            $content[] = [$value['organ_name'], $value['class_name'], $value['num1'], $value['num2'], $value['num3'], $value['money2'], $value['money3']];
        }
        // 文件存储目录
        $path = $_SERVER['DOCUMENT_ROOT'] . '/statistics/';
        $fileName = 'data_'.date('YmdHis').'.xlsx';
        if(!file_exists($path)){
            mkdir($path, 0777);
        }
        $excel_config = [
            'path' => $path // xlsx文件保存路径
        ];
        $excel  = new \Vtiful\Kernel\Excel($excel_config);
        $fileObject = $excel->fileName($fileName, 'sheet1');

         // // 设置样式
         $fileHandle = $fileObject->getHandle();
         $format = new \Vtiful\Kernel\Format($fileHandle);
         $alignStyle = $format
                         ->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER_ACROSS)
                         ->toResource();
         $boldStyle = $format
             ->bold() // 加粗
             // ->wrap() // 文本换行
             // ->background(0xFFB6C1) // 设置背景颜色 颜色常量和16进制数
             ->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER) // 文本居中
             ->toResource();
         // fileName 会自动创建一个工作表,你可以自定义该工作表名称,工作表名称为可选参数
         $fileObject
         ->header(['救助机构', '救助项目', '受理数量(次)', '完成救助数量(次)', '完成救助金额(元)'])
         ->mergeCells('A1:A2', '救助机构', $boldStyle)
         ->mergeCells('B1:B2', '救助项目', $boldStyle)
         ->mergeCells('C1:C2', '受理数量(次)', $boldStyle)
         ->mergeCells('D1:E1', '完成救助数量(次)', $boldStyle)
         ->mergeCells('D2:D2', '小计', $boldStyle)
         ->mergeCells('E2:E2', '其中异地户籍救助', $boldStyle)
         ->mergeCells('F1:G1', '完成救助金额(元)', $boldStyle)
         ->mergeCells('F2:F2', '小计', $boldStyle)
         ->mergeCells('G2:G2', '其中异地户籍救助金额', $boldStyle)
        //  ->defaultFormat($alignStyle)
         ->setRow('A1', 30)
         ->setRow('D2:D9999', 20) // 行宽
         ->setColumn('A:G', '20')
         ->data($content)
         ->output(); // 列宽

         $file_dir = '/statistics/'.$fileName;
         return $file_dir;
    }
相关推荐
消失的旧时光-19438 小时前
从 Android 组件化到 Flutter 组件化
android·flutter·架构
徐赛俊8 小时前
Excel 图表一键导出到 PPT(单页精准排版)教程
powerpoint·excel
Android轮子哥9 小时前
Android 12 SplashScreen 一种另类的适配方案
android·github
YJlio9 小时前
PsPing 学习笔记(14.7):一条龙网络体检脚本——连通性、延迟、带宽全都要
开发语言·网络·笔记·python·学习·pdf·php
粤M温同学9 小时前
Android OkHttp 下载限速方案实现
android·okhttp
2501_9151063210 小时前
Perfdog 成本变高之后,Windows 上还能怎么做 iOS APP 性能测试
android·ios·小程序·https·uni-app·iphone·webview
愤怒的代码11 小时前
从开发调试到生产上线:全维度 Android 内存监控与分析体系构建
android·java·kotlin
jzlhll12311 小时前
Android最简化发布模块到mavenCentral
android·mavencentral
2501_9151063211 小时前
iOS 安装了证书,HTTPS 还是抓不到
android·网络协议·ios·小程序·https·uni-app·iphone
好奇龙猫11 小时前
【人工智能学习-AI-MIT公开课13.- 学习:遗传算法】
android·人工智能·学习