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

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

#今天接到一个新的需求 需要读取银行指定的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

推荐文章

Nginx 性能优化有这篇就够了!
2024-11-19 01:57:41 +0800 CST
filecmp,一个Python中非常有用的库
2024-11-19 03:23:11 +0800 CST
使用Ollama部署本地大模型
2024-11-19 10:00:55 +0800 CST
php strpos查找字符串性能对比
2024-11-19 08:15:16 +0800 CST
防止 macOS 生成 .DS_Store 文件
2024-11-19 07:39:27 +0800 CST
Plyr.js 播放器介绍
2024-11-18 12:39:35 +0800 CST
动态渐变背景
2024-11-19 01:49:50 +0800 CST
Vue3结合Driver.js实现新手指引功能
2024-11-19 08:46:50 +0800 CST
一个收银台的HTML
2025-01-17 16:15:32 +0800 CST
jQuery中向DOM添加元素的多种方法
2024-11-18 23:19:46 +0800 CST
nuxt.js服务端渲染框架
2024-11-17 18:20:42 +0800 CST
内网穿透技术详解与工具对比
2025-04-01 22:12:02 +0800 CST
网站日志分析脚本
2024-11-19 03:48:35 +0800 CST
Vue3中如何处理WebSocket通信?
2024-11-19 09:50:58 +0800 CST
Claude:审美炸裂的网页生成工具
2024-11-19 09:38:41 +0800 CST
如何在Rust中使用UUID?
2024-11-19 06:10:59 +0800 CST
前端如何优化资源加载
2024-11-18 13:35:45 +0800 CST
介绍Vue3的Tree Shaking是什么?
2024-11-18 20:37:41 +0800 CST
html流光登陆页面
2024-11-18 15:36:18 +0800 CST
MySQL 主从同步一致性详解
2024-11-19 02:49:19 +0800 CST
程序员茄子在线接单