如何使用Java在Word文档中创建动态表

苏格哈卡(JgSudhakar)

我有一种情况,我需要使用页眉,表脚和表格创建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();
    }

示例教程http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi-part-2-creating-tables/

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章