使用jacob 合并excel

本文介绍了一种使用Jacob库在Java中合并多个Excel文件的方法。通过创建Excel应用程序实例,并调用特定API,可以将多个Excel文件合并到一个主文件中。此过程包括打开主文件、准备临时文件以及通过参数数组传递所有要合并的文件。

使用jacob 合并excel

 

 

try {
           
            String tempDir = System.getProperty("java.io.tmpdir");
            ActiveXComponent excel = new ActiveXComponent("Excel.Application");
            Dispatch workbooks = excel.getProperty("Workbooks").toDispatch();
            Dispatch workbook = Dispatch.call(
                    workbooks,
                    "Open","master.xls" ).toDispatch();
            Variant[] vargs = new Variant[copyFromFilesArray.length + 2];
            vargs[0] = new Variant("Sheet1.CombineWorkbooks");
            SequenceUUID oGeneration = SequenceUUID.getInstance();
            String tempFile = tempDir + oGeneration.getUUID() + ".xls";
            vargs[1] = new Variant(tempFile);
            for (int i = 0; i < copyFromFilesArray.length; i++) {
                vargs[i + 2] = new Variant(copyFromFilesArray[i]);
            }
            Dispatch.call(excel, "Run", vargs);
            return tempFile;
        } finally {
            ComThread.Release();
        }

一个简单示例 初始化当前Java线程STA ComThread InitSTA ; ActiveXComponent xl new ActiveXComponent "Excel Application" ; try{ 打开现有的Excel xl setProperty "Visible" new Variant false ; Dispatch workbooks xl getProperty "Workbooks" toDispatch ; Dispatch workbook Dispatch invoke workbooks "Open" Dispatch Method new Object[]{excelPath new Variant false 是否只读 new Variant false } 是否显示打开 new int[1] toDispatch ; 得到所有sheet Dispatch sheets Dispatch get workbook "sheets" toDispatch ; System out println "sheet数量:"+Dispatch get sheets "count" toString ; 根据索引得到sheet Dispatch sheet Dispatch invoke sheets "Item" Dispatch Get new Object[]{i} new int[1] toDispatch ; 绘图 Dispatch chartobjects Dispatch call sheet "ChartObjects" toDispatch ; Dispatch achart Dispatch invoke chartobjects "Add" Dispatch Method new Object[] { new Double 12 yHeight 横坐标 纵坐标 new Double 943 new Double 293 宽度 高度 } new int[1] toDispatch ; Dispatch chart Dispatch get achart "chart" toDispatch ; Dispatch range Dispatch invoke sheet "Range" Dispatch Get new Object[] {rangeX rangeY} 列名位置 数据位置 new int[2] toDispatch ; Dispatch series Dispatch call chart "seriescollection" toDispatch ; Dispatch call series "add" range new Variant true ; 生成图形 Dispatch put chart "PlotBy" "2" ; 纵向读取数据绘制chart 设置线条气泡颜色 黄色 Dispatch seriesChartFillFormat Dispatch get series "Fill" toDispatch ; Dispatch seriesForeColor Dispatch get seriesChartFillFormat "ForeColor" toDispatch ; Dispatch put s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值