Java,修改一个文件excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12580519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java, modify a file excel
提问by user1696875
I would like to know if there is a way in java to edit an excel file. For example: if I have an excel sheet populated, I can change the value of certain cells while leaving the value of others? Thank you in advance
我想知道java中是否有编辑excel文件的方法。例如:如果我填充了一个 Excel 工作表,我可以更改某些单元格的值,同时保留其他单元格的值吗?先感谢您
回答by Bart Friederichs
I have some decent results with the Apache POI library: http://poi.apache.org/
我对 Apache POI 库有一些不错的结果:http: //poi.apache.org/
It even works on Android.
它甚至适用于Android。
回答by Kumar Vivek Mitra
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.
JXL
旨在提高读取效率(因为这是 API 的主要用途)。为了提高性能,在读取电子表格时不解释与输出信息相关的数据(例如所有格式信息,如字体),因为在询问原始数据值时这是多余的。
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();
回答by duffymo
回答by Jayy
Yes, you can use Apache POIAPI to edit an excel sheet
是的,您可以使用Apache POIAPI 来编辑 Excel 表格
回答by Mukul Goel
use can use JXL to do this
使用可以使用 JXL 来做到这一点
the JXL doesnt provide a direct way of reading and writing to the same file (might seem inconvenient to you but the JXL says this is because reading is main function(mostly used) and this improves performance , I though would have preferred a readwrite streat on a workbook myself)
JXL 不提供读取和写入同一个文件的直接方式(对您来说可能看起来不方便,但 JXL 说这是因为读取是主要功能(主要使用)并且这提高了性能,虽然我更喜欢读写 streat自己的工作簿)
example
例子
import java.io.File;
import java.util.Date;
import jxl.*;
import jxl.write.*;
//open read to your workbook (readonly)
Workbook workbook = Workbook.getWorkbook(new File("myfile.xls"));
//create a copy workbook on which you will write
WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"), workbook);
//modify existing cell
WritableSheet sheet2 = copy.getSheet(1);
WritableCell cell = sheet2.getWritableCell(2, 4);
NumberFormat fivedps = new NumberFormat("#.#####");
WritableCellFormat cellFormat = new WritableCellFormat(fivedps);
cell.setFormat(cellFormat);
//add cells
Label label = new Label(0, 2, "New label record");
sheet2.addCell(label);
Number number = new Number(3, 4, 3.1459);
sheet2.addCell(number);
回答by Kos Petoussis
You'll have to write some utility to do this, however it is possible to do in JXL.
您必须编写一些实用程序来执行此操作,但是可以在 JXL 中执行此操作。
Please note there is a bug in SheetCopier.java, L996+997: wrong:
请注意 SheetCopier.java 中有一个错误,L996+997:错误:
if (c.getCellFeatures() != null &
c.getCellFeatures().hasDataValidation())
should be:
应该:
if (c.getCellFeatures() != null &&
c.getCellFeatures().hasDataValidation())
Since i dont have Andy Khans email I cannot contact him, so rebuild the sources too.
因为我没有 Andy Khans 的电子邮件,所以我无法联系他,所以也要重建资源。
As a side note, Apache poi is far too complicated for mortals, and the class names are ridiculous, and in my experience apache code is unfriendly, unhelpful, and not robust. So I avoid apache when possible.
附带说明一下,Apache poi 对于凡人来说太复杂了,而且类名很荒谬,而且根据我的经验,Apache 代码不友好、无用且不健壮。所以我尽可能避免使用 apache。