import org.apache.poi.util.DocumentHelper;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
// Normally should happen only in developement phase, so just throw
// exception
}
+
+ // check if there is anything to save for some parts. We don't do this for all parts as some code
+ // might depend on empty parts being saved, e.g. some unit tests verify this currently.
+ if(part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) {
+ return true;
+ }
ZipOutputStream zos = (ZipOutputStream) os;
ZipEntry partEntry = new ZipEntry(ZipHelper
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.openxml4j.opc.ContentTypes;
import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.openxml4j.opc.internal.FileHelper;
import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.ss.SpreadsheetVersion;
finally {
IOUtils.closeQuietly(wb);
}
+ }
+
+ @Test
+ public void testBug56957CloseWorkbook() throws Exception {
+ File file = TempFile.createTempFile("TestBug56957_", ".xlsx");
+
+ try {
+ // as the file is written to, we make a copy before actually working on it
+ FileHelper.copyFile(new File("test-data/spreadsheet/56957.xlsx"), file);
+
+ assertTrue(file.exists());
+
+ // read-only mode works!
+ Workbook workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
+ System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
+ workbook.close();
+ workbook = null;
+
+ workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
+ System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
+ workbook.close();
+ workbook = null;
+
+ // now check read/write mode
+ workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
+ System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
+ workbook.close();
+ workbook = null;
+
+ workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
+ System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
+ workbook.close();
+ workbook = null;
+ } finally {
+ assertTrue(file.exists());
+ assertTrue(file.delete());
+ }
}
}