簡體   English   中英

如何使用同一工作簿讀寫Excel文件(.xlsx)?

[英]How to use same workbook to read and write into Excel file (.xlsx)?

是否可以使用同一工作簿讀取和寫入Excel文件(.xlsx)?

我嘗試使用同一工作簿來提高性能。

確實,打開,關閉,打開,關閉...不是很有效。

因此,我將工作簿放在“全局變量”中。

this.FileInputStream = null
this.Workbook = null

public void before() {
     this.FileInputStream = new FileInputStream (new File('File.xlsx'))
     this.Workbook = new XSSFWorkbook(this.FileInputStream)
}

public void read() {
     XSSFSheet sheet = this.Workbook.getSheet(sheetName) 
     ...
}

public void write() {
     XSSFSheet sheet = this.Workbook.getSheet(sheetName) 
     ...
     FileOutputStream outFile = new FileOutputStream(new File('File.xlsx'))
     this.Workbook.write(outFile)
     outFile.close()
}    

public void after() {
     this.Workbook = null
     this.FileInputStream.close()
     this.FileInputStream = null 
}

錯誤

org.apache.xmlbeans.impl.values.XmlValueDisconnectedException

在線

this.Workbook.write(outFile)

你能幫我改善一下嗎?

先感謝您,

問候,

將我的答案從評論中移開:

我發現曾經有一個與多次編寫有關的錯誤: bz.apache.org/bugzilla/show_bug.cgi?id=49940 它可能已在3.10-FINAL(2014-02-08)版本中修復。

如果您使用的是較舊的版本,而無法獲得較新的版本,那么最好的選擇就是不要多次向同一工作簿寫入內容。 打開文件后立即進行所有更改並寫入一次,或者在每次寫入后重新打開文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM