Poi实现根据word模板导出-表格篇

往期系列传送门:

Poi实现根据word模板导出-文本段落篇

Poi实现根据word模板导出-图表篇

(需要完整代码的直接看最后位置!!!)

前言:

word中表格数据填充是非常简单的,非常类似excel。如果你是从之前系列看过来的,应该能想到思路应该也是通过poi获取表格对象,然后通过表格对象操作里面的内容,套路都是一样的。

话不多说,直接看代码吧,这部分很简单:

@RequestMapping("/export")
public void exportWord() throws Exception {
    //获取word模板
    InputStream is = WordController.class.getResourceAsStream("/template/wordChartTemplate.docx");

    try {
        ZipSecureFile.setMinInflateRatio(-1.0d);
        // 获取docx解析对象
        XWPFDocument document = new XWPFDocument(is);
        // 填充表格数据
        changeTable(document);
        // 输出新文件
        FileOutputStream fos = new FileOutputStream("D:\\test\\test.docx");
        document.write(fos);
        document.close();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void changeTable(XWPFDocument document) {
    // 获取文档第一个表格(有几个就按顺序取)
    XWPFTable table = document.getTables().get(0);
    // 填充表格数据
    // 获取表格行数
    int numberOfRows = table.getNumberOfRows();
    for (int i = 0; i < numberOfRows; i++) {
        // 获取当前行
        XWPFTableRow row = table.getRow(i);
        // 变量循环当前行,获取单元格
        for (int j = 0; j < row.getTableCells().size(); j++) {
            // 获取当前单元格
            XWPFTableCell cell = row.getCell(j);
            if (null == cell) {
                cell = row.createCell();
            }
            // 填充单元格数据
            cell.setText("A" + i + "," + j);
        }
    }
}

效果实现:每个表格都有自己的坐标,和Excel基本一样。行列坐标都是从0开始

补充:

整个poi操作word都是基于以下maven依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<!-- 根据模板导出word -->
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.12.0</version>
    <exclusions>
        <!-- 移除,解决版本冲突,再引入poi5.2.2 -->
        <exclusion>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </exclusion>
    </exclusions>
</dependency>

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!您可以使用poi-tl库来导出带有合并列的Word表格,并将其下载。下面是一个简单的示例代码: ```java import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign; import java.io.FileOutputStream; import java.io.IOException; public class WordTableMergeColumnsExample { public static void main(String[] args) { // 创建一个新的文档 XWPFDocument document = new XWPFDocument(); // 创建一个表格 XWPFTable table = document.createTable(3, 3); // 合并第一行的前两列 table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); table.getRow(0).getCell(0).setText("Merged Cells"); table.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); // 设置表格中其他单元格的内容 table.getRow(1).getCell(0).setText("Cell 1"); table.getRow(1).getCell(1).setText("Cell 2"); table.getRow(1).getCell(2).setText("Cell 3"); table.getRow(2).getCell(0).setText("Cell 4"); table.getRow(2).getCell(1).setText("Cell 5"); table.getRow(2).getCell(2).setText("Cell 6"); // 设置表格样式 CTTblPr tblPr = table.getCTTbl().getTblPr(); CTTblWidth tblWidth = tblPr.getTblW(); tblWidth.setW(BigInteger.valueOf(5000)); // 调整表格中所有单元格的垂直对齐方式 for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { cell.setVerticalAlignment(XWPFVertAlign.CENTER); } } // 将文档保存到文件中 try (FileOutputStream out = new FileOutputStream("merged_table.docx")) { document.write(out); System.out.println("表格导出成功!"); } catch (IOException e) { System.out.println("表格导出失败:" + e.getMessage()); } } } ``` 这个示例代码创建了一个3x3的表格,并将第一行的前两列进行了合并。您可以根据自己的需求修改表格内容和样式。最后,将文档保存到名为 "merged_table.docx" 的文件中。 希望对您有所帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1个凡夫俗子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值