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

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

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

推荐文章

Claude:审美炸裂的网页生成工具
2024-11-19 09:38:41 +0800 CST
Nginx 防止IP伪造,绕过IP限制
2025-01-15 09:44:42 +0800 CST
纯CSS绘制iPhoneX的外观
2024-11-19 06:39:43 +0800 CST
deepcopy一个Go语言的深拷贝工具库
2024-11-18 18:17:40 +0800 CST
Python 基于 SSE 实现流式模式
2025-02-16 17:21:01 +0800 CST
解决python “No module named pip”
2024-11-18 11:49:18 +0800 CST
MySQL 1364 错误解决办法
2024-11-19 05:07:59 +0800 CST
linux设置开机自启动
2024-11-17 05:09:12 +0800 CST
网络数据抓取神器 Pipet
2024-11-19 05:43:20 +0800 CST
用 Rust 构建一个 WebSocket 服务器
2024-11-19 10:08:22 +0800 CST
程序员茄子在线接单