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

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

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

推荐文章

PHP如何进行MySQL数据备份?
2024-11-18 20:40:25 +0800 CST
JavaScript设计模式:桥接模式
2024-11-18 19:03:40 +0800 CST
Go 1.23 中的新包:unique
2024-11-18 12:32:57 +0800 CST
Nginx 性能优化有这篇就够了!
2024-11-19 01:57:41 +0800 CST
小技巧vscode去除空格方法
2024-11-17 05:00:30 +0800 CST
Redis和Memcached有什么区别?
2024-11-18 17:57:13 +0800 CST
Vue3中哪些API被废弃了?
2024-11-17 04:17:22 +0800 CST
windon安装beego框架记录
2024-11-19 09:55:33 +0800 CST
markdowns滚动事件
2024-11-19 10:07:32 +0800 CST
Golang中国地址生成扩展包
2024-11-19 06:01:16 +0800 CST
js常用通用函数
2024-11-17 05:57:52 +0800 CST
底部导航栏
2024-11-19 01:12:32 +0800 CST
在 Rust 生产项目中存储数据
2024-11-19 02:35:11 +0800 CST
PyMySQL - Python中非常有用的库
2024-11-18 14:43:28 +0800 CST
服务器购买推荐
2024-11-18 23:48:02 +0800 CST
程序员茄子在线接单