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

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

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

推荐文章

Vue3中的v-slot指令有什么改变?
2024-11-18 07:32:50 +0800 CST
MySQL数据库的36条军规
2024-11-18 16:46:25 +0800 CST
JavaScript设计模式:发布订阅模式
2024-11-18 01:52:39 +0800 CST
防止 macOS 生成 .DS_Store 文件
2024-11-19 07:39:27 +0800 CST
Redis函数在PHP中的使用方法
2024-11-19 04:42:21 +0800 CST
#免密码登录服务器
2024-11-19 04:29:52 +0800 CST
html一些比较人使用的技巧和代码
2024-11-17 05:05:01 +0800 CST
五个有趣且实用的Python实例
2024-11-19 07:32:35 +0800 CST
JavaScript中设置器和获取器
2024-11-17 19:54:27 +0800 CST
Go 单元测试
2024-11-18 19:21:56 +0800 CST
Java环境中使用Elasticsearch
2024-11-18 22:46:32 +0800 CST
PyMySQL - Python中非常有用的库
2024-11-18 14:43:28 +0800 CST
Python 获取网络时间和本地时间
2024-11-18 21:53:35 +0800 CST
Vue3中如何实现插件?
2024-11-18 04:27:04 +0800 CST
Nginx 防止IP伪造,绕过IP限制
2025-01-15 09:44:42 +0800 CST
38个实用的JavaScript技巧
2024-11-19 07:42:44 +0800 CST
纯CSS实现3D云动画效果
2024-11-18 18:48:05 +0800 CST
程序员茄子在线接单