编程 PHPExcel读取xlsx模板文件并写入文件导出

2024-11-17 17:33:15 +0800 CST views 3146

#今天接到一个新的需求 需要读取银行指定的xlsx文件模板 并按要求进行写入文件

1.png

    /**
     * 按银行的模板导出
     * @author wapele
     * @param array $data
     * @return void
     */
    public static function bank($data=[]){
        $path = "./config/TransBat.xlsx";
        $fileType = \PHPExcel_IOFactory::identify($path);
        $objReader = \PHPExcel_IOFactory::createReader($fileType);
        $objPHPExcel = $objReader->load($path);
        $currentSheet = $objPHPExcel->getSheet(0); //第一个工作簿
        $allRow = $currentSheet->getHighestRow(); //行数
        $i = 3;        
        foreach ($data as $v) {
            $x =0;
            foreach ($v as $vv) {
                $name = self::num2alpha($x).$i;
                $objPHPExcel->getActiveSheet()->setCellValue($name,$vv);
                $x++;
            }
           $i ++;
        }
        $file=date('YmdHis');
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$file.'.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
        $objWriter->save('php://output');
        exit();     
    }
    /**
     * 数字到字母列
     * @author wapele
     * @param int
     * @param int
     * @return string
     */
    protected static function num2alpha($index, $start = 65)
    {
        $str = '';
        if (floor($index / 26) > 0) {
            $str .= self::num2alpha(floor($index / 26)-1);
        }
        return $str . chr($index % 26 + $start);
    }

https://github.com/PHPOffice/PHPExcel

复制全文 生成海报 php github

推荐文章

使用 Git 制作升级包
2024-11-19 02:19:48 +0800 CST
20个超实用的CSS动画库
2024-11-18 07:23:12 +0800 CST
15 个 JavaScript 性能优化技巧
2024-11-19 07:52:10 +0800 CST
mysql 优化指南
2024-11-18 21:01:24 +0800 CST
mysql时间对比
2024-11-18 14:35:19 +0800 CST
使用 sync.Pool 优化 Go 程序性能
2024-11-19 05:56:51 +0800 CST
CSS 奇技淫巧
2024-11-19 08:34:21 +0800 CST
JavaScript 的模板字符串
2024-11-18 22:44:09 +0800 CST
禁止调试前端页面代码
2024-11-19 02:17:33 +0800 CST
2024年公司官方网站建设费用解析
2024-11-18 20:21:19 +0800 CST
Dropzone.js实现文件拖放上传功能
2024-11-18 18:28:02 +0800 CST
支付宝批量转账
2024-11-18 20:26:17 +0800 CST
robots.txt 的写法及用法
2024-11-19 01:44:21 +0800 CST
Vue 3 中的 Watch 实现及最佳实践
2024-11-18 22:18:40 +0800 CST
Web 端 Office 文件预览工具库
2024-11-18 22:19:16 +0800 CST
MySQL 日志详解
2024-11-19 02:17:30 +0800 CST
程序员茄子在线接单