简体   繁体   English

Apache POI SXSSF - Workbook.close() 清除先前保存的文件中的第二个(最后一个)工作表数据

[英]Apache POI SXSSF - Workbook.close() clears the second (the last) sheet data in previously saved file

I'm trying to modify existing document and fill large amount (~ 1 million rows) of data to a new sheet.我正在尝试修改现有文档并将大量(约 100 万行)数据填充到新工作表中。 I use SXSSF (Streaming Usermodel API) for this purpose.为此,我使用 SXSSF(流式用户模型 API)。 When I call workbook.close() it clears the added (the last) sheet data but still adds a new empty sheet.当我调用 workbook.close() 时,它会清除添加的(最后一个)工作表数据,但仍会添加一个新的空工作表。 Did I miss something in code?我错过了代码中的某些内容吗? Thanks a lot for the answers.非常感谢您的回答。 Example with 1000 rows only:仅包含 1000 行的示例:

package com.company.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class TestPoiStreaming {
   public static void main(String[] args) throws IOException {
      int rowNumber = 0;
      int cellNumber = 0;
      String outputFileName = "c:/temp/test1.xlsx";
      File outFile = new File(outputFileName);
      Workbook workbook = new SXSSFWorkbook(100);
      try {
         Sheet sheet = workbook.createSheet("Sheet1");
         Row row = null;
         for (int i = 0; i < 1000; i++) {
            row = sheet.createRow(rowNumber++);
            cellNumber = 0;
            for (int j = 1; j <= 10; j++) {
               Cell cell = row.createCell(cellNumber++);
               cell.setCellValue(String.valueOf(j));
            }
         }
         try (FileOutputStream out = new FileOutputStream(outFile)) {
            workbook.write(out);
         }
      } finally {
         workbook.close();
         ((SXSSFWorkbook) workbook).dispose();
      }

      rowNumber = 0;
      cellNumber = 0;
      workbook = new SXSSFWorkbook((XSSFWorkbook) WorkbookFactory.create(outFile), 100);
      try {
         Sheet sheet = workbook.createSheet("Sheet2");
         Row row = null;
         for (int i = 0; i < 1000; i++) {
            row = sheet.createRow(rowNumber++);
            cellNumber = 0;
            for (int j = 1; j <= 10; j++) {
               Cell cell = row.createCell(cellNumber++);
               cell.setCellValue(String.valueOf(j));
            }
         }
         Files.delete(Paths.get(outFile.getPath()));
         try (FileOutputStream out = new FileOutputStream(outFile)) {
            workbook.write(out);
         }
      } finally {
         workbook.close();
         ((SXSSFWorkbook) workbook).dispose();
      }
   }
}

You did not reset rowNumber to zero in the second block.您没有在第二个块中将 rowNumber 重置为零。 The sheet "Sheet2" is not empty.工作表“Sheet2”不为空。 Simply scroll down to line 1001 and you will see the contents.只需向下滚动到第 1001 行,您就会看到内容。

Answer for the edited code: I had problems executing it before you were reading the same file several times and also trying to delete it at the same time.对已编辑代码的回答:在您多次读取同一个文件并同时尝试删除它之前,我在执行它时遇到了问题。 Why don't you just try reading and writing once, instead of writing the first sheet and then reopening in order to add the second?你为什么不试着读和写一次,而不是写第一张然后重新打开以添加第二张? Here is the full code with several lines deleted:这是删除了几行的完整代码:

public static void main(String[] args) throws IOException, InvalidFormatException {
    int rowNumber = 0;
    int cellNumber = 0;
    String outputFileName = "c:/temp/test1.xlsx";
    File outFile = new File(outputFileName);
    Workbook workbook = new SXSSFWorkbook(100);
    Sheet sheet = workbook.createSheet("Sheet1");
    Row row = null;
    for (int i = 0; i < 1000; i++) {
      row = sheet.createRow(rowNumber++);
      cellNumber = 0;
      for (int j = 1; j <= 10; j++) {
        Cell cell = row.createCell(cellNumber++);
        cell.setCellValue(String.valueOf(j));
      }
    }
    rowNumber = 0;
    cellNumber = 0;
    try {
      sheet = workbook.createSheet("Sheet2");
      row = null;
      for (int i = 0; i < 1000; i++) {
        row = sheet.createRow(rowNumber++);
        cellNumber = 0;
        for (int j = 1; j <= 10; j++) {
          Cell cell = row.createCell(cellNumber++);
          cell.setCellValue(String.valueOf(j));
        }
      }
      try (FileOutputStream out = new FileOutputStream(outFile)) {
        workbook.write(out);
      }
    } finally {
      workbook.close();
      ((SXSSFWorkbook) workbook).dispose();
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM