如何使用Java中的Apache POI将元数据写入Excel工作簿

元数据Excel
预期的文件属性

需要使用Apache POI将自定义数据写入excel文件。我使用的是POI 3.1.1版本的jar。 这是我的代码:

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(); 

任何人都可以帮我在我的代码中出现错误,以获得预期的自定义选项卡?

在这里,这个代码适用于Excel 2011和XLSX(当然,xlsx需要在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(); } } 

作为依赖我得到:

 <!-- 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>