gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:01:58 26 4
gpt4 key购买 nike

MetaData Excel
Expected File Properties

需要使用 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>

关于java - 如何使用 Java 中的 Apache POI 将元数据写入 Excel 工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43796819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com