A universal method to deal with complex table headers when using PHP to export Excel

When using PHP to export Excel, it is very simple to deal with the first-level header, but what to do if you encounter more complicated headers, such as second-level, third-level, or even more-level headers?

Just like the header below, there are three layers, and each layer is not very regular——

Do we have to split or merge cells column by column every time we process the header?

Of course not, let's change our thinking:

Do not process the header directly, put a ready-made template with complex headers on the server, download the template file when calling the method, then load the data into it, and save it.

Then you don't have to worry about how complicated the header is.

Don't talk nonsense, just upload the ready-made code directly! ! !

 /**
     * 导出固定模板的excel文件(支持.xls、.xlsx格式)
     * (
     *  该方法主要用于导出复杂表头【如:2、3级表头等】的excel文件:
     *  1. 先将模板文件放到系统目录下;
     *  2. 调用方法时,将从系统目录下载模板文件,并将数据插入到excel模板对应的列数【不识别表头】当中
     *  3. 保存文件并返回文件名
     * )
     * @param $modelExcelPath   模板excel的文件路径(推荐使用系统相对路径,如:../runtime/obs/Excel模板.xls)
     * @param $fileName 文件名
     * @param $data 导出数据
     * @return string  $fileName  文件名
     */
    public static function exportExcel($modelExcelPath, $fileName = '', $data = '')
    {
        try{
            // 加载excel
            try {
                $inputFileType = \PHPExcel_IOFactory::identify($modelExcelPath);
                $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
                $objPHPExcel = $objReader->load($modelExcelPath);
            } catch(\Exception $e) {
                die('加载Excel发生错误:"'.pathinfo($modelExcelPath,PATHINFO_BASENAME).'": '.$e->getMessage());
            }

            // excel列数
            $cellName = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];

            // 处理数据
            $sheet = $objPHPExcel->getSheet(0); // 读取第一個工作表
            $maxRow = $sheet->getHighestRow(); // 取得总行数
            foreach($data as $val){
                $i = 0;    //列数
                $maxRow++;  // 循环操作行的行号
                foreach ($val as $key => $item){
                    $objPHPExcel->getActiveSheet()->setCellValue($cellName[$i] . $maxRow, $item);
                    $i++;
                }
            }

            // 文件类型
            $fileType = pathinfo($modelExcelPath, PATHINFO_EXTENSION);
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

            // 文件名
            if (empty($fileName)) {
                $fileName = "excel-" . date("YmdHis", time()) . rand(1000, 9999) . "." . $fileType;
            }else{
                $fileName = $fileName . date("YmdHis", time()) . rand(1000, 9999) . '.' . $fileType;
            }
            $file = \Env::get('runtime_path') . '/export/' . $fileName;

            // 保存文件并返回
            $objWriter->save($file);
            return $fileName;

        }catch(\Exception $exception) {
            throw new \Exception('出错了!');
        }
    }

Guess you like

Origin blog.csdn.net/weixin_49851451/article/details/131128907