Search in sources :

Example 1 with POIXMLProperties

use of org.apache.poi.POIXMLProperties in project poi by apache.

the class WorkbookProperties method main.

public static void main(String[] args) throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    workbook.createSheet("Workbook Properties");
    POIXMLProperties props = workbook.getProperties();
    /**
         * Extended properties are a predefined set of metadata properties
         * that are specifically applicable to Office Open XML documents.
         * Extended properties consist of 24 simple properties and 3 complex properties stored in the
         *  part targeted by the relationship of type
         */
    POIXMLProperties.ExtendedProperties ext = props.getExtendedProperties();
    ext.getUnderlyingProperties().setCompany("Apache Software Foundation");
    ext.getUnderlyingProperties().setTemplate("XSSF");
    /**
         * Custom properties enable users to define custom metadata properties.
         */
    POIXMLProperties.CustomProperties cust = props.getCustomProperties();
    cust.addProperty("Author", "John Smith");
    cust.addProperty("Year", 2009);
    cust.addProperty("Price", 45.50);
    cust.addProperty("Available", true);
    FileOutputStream out = new FileOutputStream("workbook.xlsx");
    workbook.write(out);
    out.close();
    workbook.close();
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 2 with POIXMLProperties

use of org.apache.poi.POIXMLProperties in project poi by apache.

the class TestXWPFDocument method testZeroLengthLibreOfficeDocumentWithWaterMarkHeader.

@Test
public void testZeroLengthLibreOfficeDocumentWithWaterMarkHeader() throws IOException {
    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("zero-length.docx");
    POIXMLProperties properties = doc.getProperties();
    assertNotNull(properties.getCoreProperties());
    XWPFHeader headerArray = doc.getHeaderArray(0);
    assertEquals(1, headerArray.getAllPictures().size());
    assertEquals("image1.png", headerArray.pictures.get(0).getFileName());
    assertEquals("", headerArray.getText());
    POIXMLProperties.ExtendedProperties extendedProperties = properties.getExtendedProperties();
    assertNotNull(extendedProperties);
    assertEquals(0, extendedProperties.getUnderlyingProperties().getCharacters());
    doc.close();
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) Test(org.junit.Test)

Example 3 with POIXMLProperties

use of org.apache.poi.POIXMLProperties in project poi by apache.

the class TestXSSFWorkbook method workbookProperties.

/**
     *  Test setting of core properties such as Title and Author
     * @throws IOException
     */
@Test
public void workbookProperties() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    try {
        POIXMLProperties props = workbook.getProperties();
        assertNotNull(props);
        //the Application property must be set for new workbooks, see Bugzilla #47559
        assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication());
        PackagePropertiesPart opcProps = props.getCoreProperties().getUnderlyingProperties();
        assertNotNull(opcProps);
        opcProps.setTitleProperty("Testing Bugzilla #47460");
        assertEquals("Apache POI", opcProps.getCreatorProperty().getValue());
        opcProps.setCreatorProperty("poi-dev@poi.apache.org");
        XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(workbook);
        assertEquals("Apache POI", wbBack.getProperties().getExtendedProperties().getUnderlyingProperties().getApplication());
        opcProps = wbBack.getProperties().getCoreProperties().getUnderlyingProperties();
        assertEquals("Testing Bugzilla #47460", opcProps.getTitleProperty().getValue());
        assertEquals("poi-dev@poi.apache.org", opcProps.getCreatorProperty().getValue());
        wbBack.close();
    } finally {
        workbook.close();
    }
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) PackagePropertiesPart(org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart) Test(org.junit.Test)

Example 4 with POIXMLProperties

use of org.apache.poi.POIXMLProperties in project poi by apache.

the class TestXWPFDocument method testWorkbookProperties.

@Test
public void testWorkbookProperties() throws Exception {
    XWPFDocument doc = new XWPFDocument();
    POIXMLProperties props = doc.getProperties();
    assertNotNull(props);
    assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication());
    doc.close();
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) Test(org.junit.Test)

Example 5 with POIXMLProperties

use of org.apache.poi.POIXMLProperties in project poi by apache.

the class TestXSSFBugs method bug54764.

@Test
public void bug54764() throws IOException, OpenXML4JException, XmlException {
    OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("54764.xlsx");
    // Check the core properties - will be found but empty, due
    //  to the expansion being too much to be considered valid
    POIXMLProperties props = new POIXMLProperties(pkg);
    assertEquals(null, props.getCoreProperties().getTitle());
    assertEquals(null, props.getCoreProperties().getSubject());
    assertEquals(null, props.getCoreProperties().getDescription());
    // Now check the spreadsheet itself
    try {
        new XSSFWorkbook(pkg).close();
        fail("Should fail as too much expansion occurs");
    } catch (POIXMLException e) {
    // Expected
    }
    pkg.close();
    // Try with one with the entities in the Content Types
    try {
        XSSFTestDataSamples.openSamplePackage("54764-2.xlsx").close();
        fail("Should fail as too much expansion occurs");
    } catch (Exception e) {
    // Expected
    }
    // Check we can still parse valid files after all that
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
    assertEquals(3, wb.getNumberOfSheets());
    wb.close();
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) POIXMLException(org.apache.poi.POIXMLException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) POIXMLException(org.apache.poi.POIXMLException) XLSBUnsupportedException(org.apache.poi.xssf.XLSBUnsupportedException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Aggregations

POIXMLProperties (org.apache.poi.POIXMLProperties)5 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 POIXMLException (org.apache.poi.POIXMLException)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 PackagePropertiesPart (org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart)1 XLSBUnsupportedException (org.apache.poi.xssf.XLSBUnsupportedException)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1 XmlException (org.apache.xmlbeans.XmlException)1