Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write metadata into Excel workbooks using Apache POI in Java

MetaData Excel
Expected File Properties

Need to write custom data into excel file using Apache POI.I am using POI 3.1.1 version jar. This is my code:

FileInputStream fis = new FileInputStream(sample);
workbook = new XSSFWorkbook(fis);
POIXMLProperties props = workbook.getProperties();

/* Let us set some core properties now*/
POIXMLProperties.CoreProperties coreProp = props.getCoreProperties();
coreProp.setCreator("Thinktibits"); //set document creator
coreProp.setDescription("set Metadata using Apache POI / Java");
coreProp.setCategory("Programming"); //category

/* Finally, we can set some custom Properies */
POIXMLProperties.CustomProperties custProp = props.getCustomProperties();
custProp.addProperty("Author", "Thinktibits");// String
custProp.addProperty("Year", 2014);     // Number Property
custProp.addProperty("Published", true); //Yes No Property
custProp.addProperty("Typist", "tika");
FileOutputStream fos = new FileOutputStream(sample);
workbook.write(fos);
fos.close();

Can anyone help me where it went wrong in my code to get the expected custom tab?

like image 830
ASHA Avatar asked May 05 '17 04:05

ASHA


1 Answers

Here this code works for me with Excel 2011 and XLSX (of course the xlsx needs to be not open in EXCEL):

public class ApachePOI {

    public static void main(final String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("C:\\Mappe1.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        POIXMLProperties props = workbook.getProperties();

        /* Let us set some core properties now */
        POIXMLProperties.CoreProperties coreProp = props.getCoreProperties();
        coreProp.setCreator("Thinktibits"); // set document creator
        coreProp.setDescription("set Metadata using Apache POI / Java");
        coreProp.setCategory("Programming"); // category

        /* Finally, we can set some custom Properies */
        POIXMLProperties.CustomProperties custProp = props.getCustomProperties();
        custProp.addProperty("Author", "Thinktibits");// String
        custProp.addProperty("Year", 2014); // Number Property
        custProp.addProperty("Published", true); // Yes No Property
        custProp.addProperty("Typist", "tika");
        FileOutputStream fos = new FileOutputStream("C:\\Mappe1.xlsx");
        workbook.write(fos);
        fos.close();
    }
}

As dependency I got:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency>
like image 91
kism3t Avatar answered Oct 26 '22 05:10

kism3t