Browse Source

Bug 56957: Avoid error if Workbook with empty SharedStringTable is written

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1710521 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_3_14_BETA1
Dominik Stadler 8 years ago
parent
commit
bcaab496b6

+ 7
- 0
src/ooxml/java/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.java View File

@@ -38,6 +38,7 @@ import org.apache.poi.openxml4j.opc.internal.ZipHelper;
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;

@@ -63,6 +64,12 @@ public final class ZipPartMarshaller implements PartMarshaller {
// 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

+ 39
- 0
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java View File

@@ -40,10 +40,12 @@ import org.apache.poi.POIXMLProperties;
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;
@@ -1019,5 +1021,42 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
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());
}
}
}

BIN
test-data/spreadsheet/56957.xlsx View File


Loading…
Cancel
Save