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

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

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

推荐文章

MySQL 优化利剑 EXPLAIN
2024-11-19 00:43:21 +0800 CST
使用 sync.Pool 优化 Go 程序性能
2024-11-19 05:56:51 +0800 CST
开源AI反混淆JS代码:HumanifyJS
2024-11-19 02:30:40 +0800 CST
`Blob` 与 `File` 的关系
2025-05-11 23:45:58 +0800 CST
PHP 如何输出带微秒的时间
2024-11-18 01:58:41 +0800 CST
Vue3中如何处理组件间的动画?
2024-11-17 04:54:49 +0800 CST
实现微信回调多域名的方法
2024-11-18 09:45:18 +0800 CST
在Rust项目中使用SQLite数据库
2024-11-19 08:48:00 +0800 CST
PHP中获取某个月份的天数
2024-11-18 11:28:47 +0800 CST
mysql int bigint 自增索引范围
2024-11-18 07:29:12 +0800 CST
服务器购买推荐
2024-11-18 23:48:02 +0800 CST
推荐几个前端常用的工具网站
2024-11-19 07:58:08 +0800 CST
联系我们
2024-11-19 02:17:12 +0800 CST
Vue 3 路由守卫详解与实战
2024-11-17 04:39:17 +0800 CST
Vue3中如何处理SEO优化?
2024-11-17 08:01:47 +0800 CST
程序员茄子在线接单