use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XSSFWorkbook method commit.
@Override
protected void commit() throws IOException {
saveNamedRanges();
saveCalculationChain();
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
workbook.save(out, xmlOptions);
out.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestPackagingURIHelper method testCreatePartNameRelativeString.
/**
* Test createPartName(String, y)
*/
public void testCreatePartNameRelativeString() throws InvalidFormatException {
PackagePartName partNameToValid = PackagingURIHelper.createPartName("/word/media/image1.gif");
OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
// Base part
PackagePartName nameBase = PackagingURIHelper.createPartName("/word/document.xml");
PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
// Relative part name
PackagePartName relativeName = PackagingURIHelper.createPartName("media/image1.gif", partBase);
assertTrue("The part name must be equal to " + partNameToValid.getName(), partNameToValid.equals(relativeName));
pkg.revert();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestXSSFBSharedStringsTable method testBasic.
@Test
public void testBasic() throws Exception {
OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("51519.xlsb"));
List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.bin"));
assertEquals(1, parts.size());
XSSFBSharedStringsTable rtbl = new XSSFBSharedStringsTable(parts.get(0));
List<String> strings = rtbl.getItems();
assertEquals(49, strings.size());
assertEquals("コメント", rtbl.getEntryAt(0));
assertEquals("日本オラクル", rtbl.getEntryAt(3));
assertEquals(55, rtbl.getCount());
assertEquals(49, rtbl.getUniqueCount());
//TODO: add in tests for phonetic runs
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestXSSFBugs method bug45431.
/**
* We should carry vba macros over after save
*/
@Test
public void bug45431() throws IOException, InvalidFormatException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm");
OPCPackage pkg1 = wb1.getPackage();
assertTrue(wb1.isMacroEnabled());
// Check the various macro related bits can be found
PackagePart vba = pkg1.getPart(PackagingURIHelper.createPartName("/xl/vbaProject.bin"));
assertNotNull(vba);
// And the drawing bit
PackagePart drw = pkg1.getPart(PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml"));
assertNotNull(drw);
// Save and re-open, both still there
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
pkg1.close();
wb1.close();
OPCPackage pkg2 = wb2.getPackage();
assertTrue(wb2.isMacroEnabled());
vba = pkg2.getPart(PackagingURIHelper.createPartName("/xl/vbaProject.bin"));
assertNotNull(vba);
drw = pkg2.getPart(PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml"));
assertNotNull(drw);
// And again, just to be sure
XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
pkg2.close();
wb2.close();
OPCPackage pkg3 = wb3.getPackage();
assertTrue(wb3.isMacroEnabled());
vba = pkg3.getPart(PackagingURIHelper.createPartName("/xl/vbaProject.bin"));
assertNotNull(vba);
drw = pkg3.getPart(PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml"));
assertNotNull(drw);
pkg3.close();
wb3.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestXSSFWorkbook method testSetVBAProject.
/**
* Tests that we can save a workbook with macros and reload it.
*/
@Test
public void testSetVBAProject() throws Exception {
File file;
final byte[] allBytes = new byte[256];
for (int i = 0; i < 256; i++) {
allBytes[i] = (byte) (i - 128);
}
XSSFWorkbook wb1 = new XSSFWorkbook();
wb1.createSheet();
wb1.setVBAProject(new ByteArrayInputStream(allBytes));
file = TempFile.createTempFile("poi-", ".xlsm");
OutputStream out = new FileOutputStream(file);
wb1.write(out);
out.close();
wb1.close();
// Check the package contains what we'd expect it to
OPCPackage pkg = OPCPackage.open(file.toString());
PackagePart wbPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
assertTrue(wbPart.hasRelationships());
final PackageRelationshipCollection relationships = wbPart.getRelationships().getRelationships(XSSFRelation.VBA_MACROS.getRelation());
assertEquals(1, relationships.size());
assertEquals(XSSFRelation.VBA_MACROS.getDefaultFileName(), relationships.getRelationship(0).getTargetURI().toString());
PackagePart vbaPart = pkg.getPart(PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName()));
assertNotNull(vbaPart);
assertFalse(vbaPart.isRelationshipPart());
assertEquals(XSSFRelation.VBA_MACROS.getContentType(), vbaPart.getContentType());
final byte[] fromFile = IOUtils.toByteArray(vbaPart.getInputStream());
assertArrayEquals(allBytes, fromFile);
// Load back the XSSFWorkbook just to check nothing explodes
@SuppressWarnings("resource") XSSFWorkbook wb2 = new XSSFWorkbook(pkg);
assertEquals(1, wb2.getNumberOfSheets());
assertEquals(XSSFWorkbookType.XLSM, wb2.getWorkbookType());
pkg.close();
}
Aggregations