第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Apache POI:用 Java 寫入 excel:打開工作簿

Apache POI:用 Java 寫入 excel:打開工作簿

慕無忌1623718 2023-05-17 17:58:33
我有一個(gè)存儲(chǔ) Excel 工作簿的類:private Map<String, Workbook> workbooks = new HashMap();public Workbook createWorkbook(String name)  {    Workbook workbook = new XSSFWorkbook();    workbooks.put(name, workbook);    return workbook;}以及寫入具有指定名稱的工作簿的方法:public void write(List<ExcelData> data, String workbookName) {   Workbook workbook = workbooks.get(workbookName);   CreationHelper createHelper = workbook.getCreationHelper();   ... write stuff   FileOutputStream fileOut = new FileOutputStream(workbookName + ".xlsx");   workbook.write(fileOut);   fileOut.close();   workbook.close();}但是,當(dāng)我嘗試為同一個(gè)工作簿調(diào)用方法write兩次時(shí):testExcel.write(data, "Default");testExcel.write(data1, "Default");我得到Exception in thread "main" java.io.IOException: Cannot write data, document seems to have been closed already錯(cuò)誤。我知道我可以像這樣打開現(xiàn)有的 Excel 工作簿:FileInputStream inputStream = new FileInputStream(new File(excelFilePath));Workbook workbook = WorkbookFactory.create(inputStream);但我想知道是否有辦法通過存儲(chǔ)Workbook變量來繞過它。有什么內(nèi)在機(jī)制在起作用?workbook.write()調(diào)用后變量是否失效?
查看完整描述

2 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以將工作簿的名稱存儲(chǔ)在列表中,而不是存儲(chǔ)工作簿本身


private List<String> workbooks = new ArrayList();

重寫 createWorkbook 以僅存儲(chǔ) excel 工作表的名稱 重寫 write 方法,使其創(chuàng)建一個(gè)新工作簿,如下所示


public void write(List<ExcelData> data, String workbookName) {


Workbook workbook = new XSSFWorkbook();

CreationHelper createHelper = workbook.getCreationHelper();

... 寫東西


 FileOutputStream fileOut = new FileOutputStream(workbookName + ".xlsx");

 workbook.write(fileOut);

 fileOut.close();


 workbook.close();

}


查看完整回答
反對 回復(fù) 2023-05-17
?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊

這個(gè)錯(cuò)誤主要發(fā)生在我們試圖寫入已經(jīng)關(guān)閉的工作簿的內(nèi)容時(shí)。


public void writeToExcel(File file) throws IOException {

    LOGGER.debug("Writing chunks data to excel {}", file);

    try (FileOutputStream outputStream = new FileOutputStream(file)) {

        workbook.write(outputStream);

    } catch (IOException e) {

        LOGGER.error("Exception raised while writing chunks of items {}", e.getLocalizedMessage());

    } finally {

        // This line will take of closing XSSFWorkbook along with SXSSFWorkbook

        workbook.close();

    }

}

這段代碼是拋出以下異常的代碼


Exception thrown is:

Exception raised while writing chunks of items 

"Cannot write data, document seems to have been closed already" 

為什么我們得到這個(gè)例外?看看這段代碼


 private void instructionSheet(Resource resource) {

    try {

        InputStream in = resource.getInputStream();

        // This is try-with-resources block which is closing the XSSFWorkbook

        try (XSSFWorkbook xssfwb = new XSSFWorkbook(OPCPackage.open(in))) {

            workbook = new SXSSFWorkbook(xssfwb);

        }

    } catch (IOException | InvalidFormatException e) {

        LOGGER.error("The instruction sheet failed to create {}", e.getLocalizedMessage());

    }

}

您可以注意到第二個(gè) try 塊是 try-with-resources 塊并且正在關(guān)閉工作簿,因此我們得到了異常。


我們只是通過刪除第二個(gè) try 塊來解決它,即


private void instructionSheet(Resource resource) {

    try {

        workbook = new SXSSFWorkbook(new XSSFWorkbook(OPCPackage.open(resource.getInputStream())));

    } catch (IOException | InvalidFormatException e) {

        LOGGER.error("The instruction sheet failed to create {}", e.getLocalizedMessage());

    }


}

您可以在這個(gè)答案的第一個(gè)代碼塊中注意到,我們在將內(nèi)容寫入文件后關(guān)閉工作簿。


在 finally 塊中調(diào)用的close方法將負(fù)責(zé)關(guān)閉XSSFWorkbook實(shí)例以及SXSSFWorkbook。


查看完整回答
反對 回復(fù) 2023-05-17
  • 2 回答
  • 0 關(guān)注
  • 460 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)