Exception al escribir en el documento xlsx varias veces usando apache poi 3.7

Recibo la siguiente excepción al intentar escribir un.xlsx archivo usando Apache POI:org.apache.xmlbeans.impl.values.XmlValueDisconnectedException

Parece que el problema es usar el método write () por segunda vez. Cuando se trabaja con un HSSFWorkbook de este problema no surge.

Aquí está el Código:

public class SomeClass{

XSSFWorkbook workbook;

public SomeClass() throws IOException{
    File excelFile = new File("workbook.xlsx");

    InputStream inp = new FileInputStream(excelFile);
    workbook = new XSSFWorkbook(inp);
    inp.close();
}

void method(int i) throws InvalidFormatException, IOException {

    XSSFSheet sheet = workbook.getSheetAt(0);
    XSSFRow row = sheet.getRow(i);
    if (row == null) {
        row = sheet.createRow(i);
    }
    XSSFCell cell = row.getCell(i);
    if (cell == null)
        cell = row.createCell(i);
    cell.setCellType(Cell.CELL_TYPE_STRING);
    cell.setCellValue("a test");

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
    workbook.write(fileOut);
    fileOut.close();

}

public static void main(String[] args) throws Exception {
    SomeClass sc = new SomeClass();

    sc.method(1);
    sc.method(2);
}
}

Respuestas a la pregunta(7)

Su respuesta a la pregunta