Apache poi如何在word中的表格中插入表格,图片等操作

1.引入依赖

       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.6.0-beta1</version>
        </dependency>

2.在文档中插入表格

       //读取word
        XWPFDocument doc = new XWPFDocument(new FileInputStream(outfile));
      //创建段落
        XWPFParagraph p1 = doc.createParagraph();

        //添加表格,50行
        for (int i = 0; i < 50; i++) {
            table.createRow();
        }

        //固定表格,不然表格会出现不协调
        int[] COLUMN_WIDTHS = new int[]{5000, 5000, 5000, 5000, 5000, 5000};
        setTableGridCol(table, COLUMN_WIDTHS);
       

3.获取表格行中指定的位置,再插入表格

//在表格中的行添加段落   
 XWPFParagraph xwpfParagraph = table.getRow(j).getCell(1).addParagraph();
//获取光标的位置
 XmlCursor cursor = xwpfParagraph.getCTP().newCursor();
//在光标处插入表格
XWPFTable xwpfTable = table.getRow(j).getCell(1).insertNewTbl(cursor);

4.在表格中的表格中创建行

    XWPFTableRow xwpfTableRow = xwpfTable.createRow();

5.在行中创建自己想创建的列数

        //创建列
                XWPFTableCell xwpfTableCell = xwpfTableRow.createCell();

6.在列中写入想要写入的内容

 //在列中创建段落     
XWPFParagraph c1 = xwpfTableCell.addParagraph();
                XWPFRun rt1 = c1.createRun();
                rt1.setText("物证序号");
                rt1.setFontSize(10);
                rt1.setBold(true);
//移除段落,不然会出现一个空行
                xwpfTableCell.removeParagraph(0);

7.在表格中插入图片

  InputStream stream = new FileInputStream("C:\\Users\\16001\\Desktop\\001155.jpg");
                p1 = table.getRow(j).getCell(0).addParagraph();
                XWPFRun r1 = p1.createRun();
                r1.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, "Generated", Units.toEMU(pictureBase.getPictureWide() * 1.2), Units.toEMU(pictureBase.getPictureHigh() * 1.2));

固定表格宽高的函数

    /**
     * @Description: 设置列宽和垂直对齐方式
     */
    public void setCellWidthAndVAlign(XWPFTableCell cell, String width, STVerticalJc.Enum typeEnum, STJc.Enum vAlign) {
        CTTc cttc = cell.getCTTc();
        CTTcPr cellPr = cttc.addNewTcPr();
        cellPr.addNewVAlign().setVal(typeEnum);
        cttc.getPList().get(0).addNewPPr().addNewJc().setVal(vAlign);
        CTTblWidth tblWidth = cellPr.isSetTcW() ? cellPr.getTcW() : cellPr.addNewTcW();
        if (!width.isEmpty()) {
            tblWidth.setW(new BigInteger(width));
            tblWidth.setType(STTblWidth.DXA);
        }
    }

结果:

注:我在做的时候表格的线不能和边缘重合,我也不知道为什么,可能是样式的原因,欢迎大家交流

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alex_yuan666

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值