我有一种情况,我需要使用页眉,表脚和表格创建word文档。并且表数据将动态地来自前端。有时可能不得不根据输入内容创建更多表。有人可以帮我吗。提前致谢
有许多库可以做到这一点,Apache Poi就是其中之一。
样例代码
public static void main(String[] args) throws IOException {
XWPFDocument document = new XWPFDocument();
XWPFTable tableOne = document.createTable();
XWPFTableRow tableOneRowOne = tableOne.getRow(0);
tableOneRowOne.getCell(0).setText("Header1");
tableOneRowOne.addNewTableCell().setText("header2");
XWPFTableRow tableOneRowTwo = tableOne.createRow();
tableOneRowTwo.getCell(0).setText("Data1");
tableOneRowTwo.getCell(1).setText("Data2");
FileOutputStream outStream = new FileOutputStream("test.doc");
document.write(outStream);
outStream.close();
}
Pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
进口货
导入java.io.FileOutputStream; 导入java.io.IOException;
导入org.apache.poi.xwpf.usermodel.XWPFDocument; 导入org.apache.poi.xwpf.usermodel.XWPFTable; 导入org.apache.poi.xwpf.usermodel.XWPFTableRow;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句