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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With