import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
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.PackageRelationship;
elIdMap.put(p.getPackageRelationship().getId(), (ExternalLinksTable)p);
}
}
+ boolean packageReadOnly = (getPackage().getPackageAccess() == PackageAccess.READ);
if (stylesSource == null) {
// Create Styles if it is missing
- stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
+ if (packageReadOnly) {
+ stylesSource = new StylesTable();
+ } else {
+ stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
+ }
}
stylesSource.setTheme(theme);
if (sharedStringSource == null) {
// Create SST if it is missing
- sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
+ if (packageReadOnly) {
+ sharedStringSource = new SharedStringsTable();
+ } else {
+ sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
+ }
}
// Load individual sheets. The order of sheets is defined by the order
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart;
* A .xlsx file with no Shared Strings table should open fine
* in read-only mode
*/
- @Ignore
@Test
public void bug57482() throws Exception {
for (PackageAccess access : new PackageAccess[] {
File file = HSSFTestDataSamples.getSampleFile("57482-OnlyNumeric.xlsx");
OPCPackage pkg = OPCPackage.open(file, access);
try {
+ // Try to open it and read the contents
XSSFWorkbook wb = new XSSFWorkbook(pkg);
assertNotNull(wb.getSharedStringSource());
assertEquals(0, wb.getSharedStringSource().getCount());
assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
+
+ // Add a text cell
+ s.getRow(0).createCell(3).setCellValue("Testing");
+ assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
+
+ // Try to write-out and read again, should only work
+ // in read-write mode, not read-only mode
+ try {
+ wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ if (access == PackageAccess.READ)
+ fail("Shouln't be able to write from read-only mode");
+ } catch (InvalidOperationException e) {
+ if (access == PackageAccess.READ) {
+ // Expected
+ } else {
+ // Shouldn't occur in write-mode
+ throw e;
+ }
+ }
+
+ // Check again
+ s = wb.getSheetAt(0);
+ assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
+ assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
+ assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
+ assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
} finally {
- pkg.close();
+ pkg.revert();
}
}
}