我有一个大的XLSX文件(大约200k-300k行)。我需要用Java打开它,在几张纸上更改一些值然后关闭它。
我尝试使用Apache POI但是创建一个工作簿需要很长时间。所以我找到了SXSSF的决定,但它需要XSSFWorkbook作为模板,由于长时间创建工作簿而无法创建。然后我决定打开XLSX文件作为OPCPackage,获取工作表,更改它们并保存文件。我使用了此处的示例代码(Add Custom XML Part using Apache POI),但我不知道如何更改现有工作表中的数据。
这是我的代码:
String filename = "temp.xlsx";
OPCPackage pkg = null;
int sheetID = 3;
try
{
pkg = OPCPackage.open(filename);
PackagePart part = pkg.getPart(PackagingURIHelper.createPartName("/xl/worksheets/sheet" + sheetID + ".xml"));
InputStream is = part.getInputStream();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
NodeList nList = doc.getElementsByTagName("c");
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String cellType = eElement.getAttribute("t");
if(cellType.equals("s"))
{
eElement.getElementsByTagName("v").item(0).setTextContent("62");
}
}
}
DOMSource source = new DOMSource(doc);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StreamResult result = new StreamResult(bos);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
is.close();
//InputStream newIs = new ByteArrayInputStream(bos.toByteArray());
//bos.toByteArray()
//part.load(newIs);
byte[] arr = bos.toByteArray();
System.out.println(arr.length);
OutputStream os = part.getOutputStream();
os.write(arr);
os.close();
}
catch(Throwable ex)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
System.out.println(sw.toString());
}
finally {
try
{
pkg.close();
}
catch(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
System.out.println(sw.toString());
}
}
最后,我将文件作为zip存档打开,而sheet3.xml为空。我应该以某种方式保存更改吗?
任何帮助将不胜感激!