Apache的POI。复印页数 [英] Apache POI. Copying sheets

查看:300
本文介绍了Apache的POI。复印页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Apache POI来创建一个Excel文件。要在工作簿中我写的下一个code新的工作表:

I'm using apache poi to create an excel document. To create new sheet in workbook I write next code:

Workbook wb = new HSSFWorkbook(); 
Sheet sh = wb.createSheet();

这code创建并添加表来工作簿。但我想以前创建表,然后将其添加到工作簿。水木清华这样的:

this code create and add sheet to workbook. But I want to create sheet formerly and then add it to workbook. Smth like this:

Sheet sh = new HSSFSheet();
wb.addSheet(sh);

我需要这样的东西,因为我想将数据从一个工作簿中的一个表复制到另一个工作簿的另一个工作表(工作簿接口有方法表cloneSheet(INT)) 。但是,工作簿接口没有像addSheet(纸SH)方法。
另外HSSFWorkbook是final类,所以我不能扩展它来实现add方法
我怎样才能做到这一点?

I need such thing, because I want to copy data from one sheet of one workbook to another sheet of another workbook(Workbook interface has method Sheet cloneSheet(int)). But Workbook interface doesn't have method like addSheet(Sheet sh). Also HSSFWorkbook is final class so I can't extend it to implement add method How can I do this?

推荐答案

您不能只取一表对象从一个工作簿,并将其添加到不同的工作簿。

You can't just take a Sheet object from one Workbook, and add it to a different Workbook.

什么你需要做的就是打开工作簿老,并在同一时间将新的工作簿,并创建表中的新工作簿。接下来,您克隆老片所使用到新一个所有样式(HSSFCellStyle有从一个工作簿克隆式的另一个方法)。最后,遍历所有的细胞,并复制了他们。

What you'll need to do is to open the old workbook and the new workbooks at the same time, and create the sheet in the new workbook. Next, clone all the styles you used in the old sheet onto the new one (HSSFCellStyle has a method for cloning a style from one workbook to another). Finally, iterate over all the cells and copy them over.

这篇关于Apache的POI。复印页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆