阿帕奇兴趣点.复印工作表 [英] Apache POI. Copying sheets

查看:31
本文介绍了阿帕奇兴趣点.复印工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 apache poi 创建一个 excel 文档.要在工作簿中创建新工作表,我编写了下一个代码:

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();

此代码创建工作表并将其添加到工作簿.但我想以前创建工作表,然后将其添加到工作簿中.像这样:

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);

我需要这样的东西,因为我想将数据从一个工作簿的一张纸复制到另一张工作簿的另一张纸上(工作簿接口有方法Sheet cloneSheet(int)).但是 Workbook 接口没有像 addSheet(Sheet 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?

推荐答案

您不能只从一个 Workbook 中获取 Sheet 对象,然后将其添加到另一个 Workbook.

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.

这篇关于阿帕奇兴趣点.复印工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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