使用jxl修改现有的Excel [英] Modifying existing excel using jxl

查看:227
本文介绍了使用jxl修改现有的Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用jxl编辑现有的Excel工作表。
它总是会创建一个新的。
任何人都可以帮我解决这个问题。
请提供一个小样本代码。

I m not able to edit the existing excel sheet using jxl. It always creates a new one. Can anyone please help me out with it. Please give a small sample code.

推荐答案

jxl旨在提高读取效率(因为这是主要用途)的API)。为了提高性能,在读取电子表格时不会解释与输出信息相关的数据(例如,所有格式信息,如字体),因为在查询原始数据值时这是多余的。

jxl is designed for increased read efficiency (since this is the primary use of the API). In order to improve performance, data which relates to output information (eg. all the formatting information such as fonts) is not interpreted when the spreadsheet is read, since this is superfluous when interrogating the raw data values.

但是,如果我们需要修改此电子表格,则需要使用各种写入接口的句柄,这可以使用复制方法获得。

However, if we need to modify this spreadsheet a handle to the various write interfaces is needed, which can be obtained using the copy method.

Workbook workbook = Workbook.getWorkbook(new File("myfile.xls"));
WritableWorkbook copy = Workbook.createWorkbook(new File("temp.xls"), workbook);

这会复制已读入的信息以及执行额外处理以解释字段这是编写电子表格所必需的。这种读优化策略的缺点是我们在内存中保存了两个电子表而不是一个,从而使内存需求翻倍。

This copies the information that has already been read in as well as performing the additional processing to interpret the fields that are necessary to for writing spreadsheets. The disadvantage of this read-optimized strategy is that we have two spreadsheets held in memory rather than just one, thus doubling the memory requirements.

但在此之后,你可以做任何你想要的。喜欢:

But after this, you can do whatever you want. Like:

WritableSheet sheet2 = copy.getSheet(1); 
WritableCell cell = sheet2.getWritableCell(1, 2); 

if (cell.getType() == CellType.LABEL) 
{ 
  Label l = (Label) cell; 
  l.setString("modified cell"); 
}
copy.write(); 
copy.close();
workbook.close();

注意:这直接取自 Andy Khan的教程页面

这篇关于使用jxl修改现有的Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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