Index: src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (revision 1377130) +++ src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (working copy) @@ -1364,4 +1364,12 @@ } return -1; } + + /** + * Deletes the temporary file that backed this sheet on disk. + * @return true if the file was deleted, false if it wasn't. + */ + boolean dispose() { + return _writer.dispose(); + } } Index: src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java (revision 1377130) +++ src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java (working copy) @@ -57,7 +57,6 @@ */ public File createTempFile()throws IOException { File fd = File.createTempFile("poi-sxssf-sheet", ".xml"); - fd.deleteOnExit(); return fd; } @@ -302,4 +301,12 @@ _out.write(chars, last, length - last); } } + + /** + * Deletes the temporary file that backed this sheet on disk. + * @return true if the file was deleted, false if it wasn't. + */ + boolean dispose() { + return _fd.delete(); + } } Index: src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java (revision 1377130) +++ src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java (working copy) @@ -760,15 +760,41 @@ //Save the template File tmplFile = File.createTempFile("poi-sxssf-template", ".xlsx"); - tmplFile.deleteOnExit(); - FileOutputStream os = new FileOutputStream(tmplFile); - _wb.write(os); - os.close(); + try + { + FileOutputStream os = new FileOutputStream(tmplFile); + try + { + _wb.write(os); + } + finally + { + os.close(); + } - //Substitute the template entries with the generated sheet data files - injectData(tmplFile, stream); - tmplFile.delete(); + //Substitute the template entries with the generated sheet data files + injectData(tmplFile, stream); + } + finally + { + tmplFile.delete(); + } } + + /** + * Dispose of temporary files backing this workbook on disk. + * Calling this method will render the workbook unusable. + * @return true if all temporary files were deleted successfully. + */ + public boolean dispose() + { + boolean success = true; + for (SXSSFSheet sheet : _sxFromXHash.keySet()) + { + success = sheet.dispose() && success; + } + return success; + } /** * @return the total number of defined names in this workbook Index: src/ooxml/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java (revision 1377130) +++ src/ooxml/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java (working copy) @@ -37,7 +37,6 @@ */ public File createTempFile()throws IOException { File fd = File.createTempFile("poi-sxssf-sheet-xml", ".gz"); - fd.deleteOnExit(); return fd; } Index: src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java =================================================================== --- src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java (revision 1377130) +++ src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java (working copy) @@ -204,5 +204,36 @@ } + + public void testWorkbookDispose() + { + SXSSFWorkbook wb = new SXSSFWorkbook(); + wb.setCompressTempFiles(true); + int rowNum = 1000; + int sheetNum = 5; + for(int i = 0; i < sheetNum; i++){ + Sheet sh = wb.createSheet("sheet" + i); + for(int j = 0; j < rowNum; j++){ + Row row = sh.createRow(j); + Cell cell1 = row.createCell(0); + cell1.setCellValue(new CellReference(cell1).formatAsString()); + + Cell cell2 = row.createCell(1); + cell2.setCellValue(i); + Cell cell3 = row.createCell(2); + cell3.setCellValue(j); + } + } + + for (SXSSFSheet sheet : wb._sxFromXHash.keySet()) { + assertTrue(sheet.getSheetDataWriter().getTempFile().exists()); + } + + wb.dispose(); + + for (SXSSFSheet sheet : wb._sxFromXHash.keySet()) { + assertFalse(sheet.getSheetDataWriter().getTempFile().exists()); + } + } }