use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class DataExtraction method main.
public static void main(String[] args) throws IOException, OpenXML4JException {
PrintStream out = System.out;
if (args.length == 0) {
out.println("Input file is required");
return;
}
FileInputStream is = new FileInputStream(args[0]);
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
// Get the document's embedded files.
for (PackagePart p : ppt.getAllEmbedds()) {
String type = p.getContentType();
// typically file name
String name = p.getPartName().getName();
out.println("Embedded file (" + type + "): " + name);
InputStream pIs = p.getInputStream();
// make sense of the part data
pIs.close();
}
// Get the document's embedded files.
for (XSLFPictureData data : ppt.getPictureData()) {
String type = data.getContentType();
String name = data.getFileName();
out.println("Picture (" + type + "): " + name);
InputStream pIs = data.getInputStream();
// make sense of the image data
pIs.close();
}
// size of the canvas in points
Dimension pageSize = ppt.getPageSize();
out.println("Pagesize: " + pageSize);
for (XSLFSlide slide : ppt.getSlides()) {
for (XSLFShape shape : slide) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape txShape = (XSLFTextShape) shape;
out.println(txShape.getText());
} else if (shape instanceof XSLFPictureShape) {
XSLFPictureShape pShape = (XSLFPictureShape) shape;
XSLFPictureData pData = pShape.getPictureData();
out.println(pData.getFileName());
} else {
out.println("Process me: " + shape.getClass());
}
}
}
ppt.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XSLFChart method commit.
@Override
protected void commit() throws IOException {
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
chartSpace.save(out, xmlOptions);
out.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();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestXSSFWorkbook method saveLoadNew.
/**
* Tests that we can save, and then re-load a new document
*/
@Test
public void saveLoadNew() throws IOException, InvalidFormatException {
XSSFWorkbook wb1 = new XSSFWorkbook();
//check that the default date system is set to 1900
CTWorkbookPr pr = wb1.getCTWorkbook().getWorkbookPr();
assertNotNull(pr);
assertTrue(pr.isSetDate1904());
assertFalse("XSSF must use the 1900 date system", pr.getDate1904());
Sheet sheet1 = wb1.createSheet("sheet1");
Sheet sheet2 = wb1.createSheet("sheet2");
wb1.createSheet("sheet3");
RichTextString rts = wb1.getCreationHelper().createRichTextString("hello world");
sheet1.createRow(0).createCell((short) 0).setCellValue(1.2);
sheet1.createRow(1).createCell((short) 0).setCellValue(rts);
sheet2.createRow(0);
assertEquals(0, wb1.getSheetAt(0).getFirstRowNum());
assertEquals(1, wb1.getSheetAt(0).getLastRowNum());
assertEquals(0, wb1.getSheetAt(1).getFirstRowNum());
assertEquals(0, wb1.getSheetAt(1).getLastRowNum());
assertEquals(0, wb1.getSheetAt(2).getFirstRowNum());
assertEquals(0, wb1.getSheetAt(2).getLastRowNum());
File file = TempFile.createTempFile("poi-", ".xlsx");
OutputStream out = new FileOutputStream(file);
wb1.write(out);
out.close();
// Check the package contains what we'd expect it to
OPCPackage pkg = OPCPackage.open(file.toString());
PackagePart wbRelPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));
assertNotNull(wbRelPart);
assertTrue(wbRelPart.isRelationshipPart());
assertEquals(ContentTypes.RELATIONSHIPS_PART, wbRelPart.getContentType());
PackagePart wbPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
// Links to the three sheets, shared strings and styles
assertTrue(wbPart.hasRelationships());
assertEquals(5, wbPart.getRelationships().size());
wb1.close();
// Load back the XSSFWorkbook
@SuppressWarnings("resource") XSSFWorkbook wb2 = new XSSFWorkbook(pkg);
assertEquals(3, wb2.getNumberOfSheets());
assertNotNull(wb2.getSheetAt(0));
assertNotNull(wb2.getSheetAt(1));
assertNotNull(wb2.getSheetAt(2));
assertNotNull(wb2.getSharedStringSource());
assertNotNull(wb2.getStylesSource());
assertEquals(0, wb2.getSheetAt(0).getFirstRowNum());
assertEquals(1, wb2.getSheetAt(0).getLastRowNum());
assertEquals(0, wb2.getSheetAt(1).getFirstRowNum());
assertEquals(0, wb2.getSheetAt(1).getLastRowNum());
assertEquals(0, wb2.getSheetAt(2).getFirstRowNum());
assertEquals(0, wb2.getSheetAt(2).getLastRowNum());
sheet1 = wb2.getSheetAt(0);
assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
pkg.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XSSFWorkbook method setVBAProject.
/**
* Adds a vbaProject.bin file to the workbook. This will change the workbook
* type if necessary.
*
* @throws IOException
*/
public void setVBAProject(InputStream vbaProjectStream) throws IOException {
if (!isMacroEnabled()) {
setWorkbookType(XSSFWorkbookType.XLSM);
}
PackagePartName ppName;
try {
ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
} catch (InvalidFormatException e) {
throw new POIXMLException(e);
}
OPCPackage opc = getPackage();
OutputStream outputStream;
if (!opc.containPart(ppName)) {
POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
outputStream = relationship.getPackagePart().getOutputStream();
} else {
PackagePart part = opc.getPart(ppName);
outputStream = part.getOutputStream();
}
try {
IOUtils.copy(vbaProjectStream, outputStream);
} finally {
IOUtils.closeQuietly(outputStream);
}
}
Aggregations