POI(一) org.apache.poi 编写word文档

org.apache.poi 编写word文档

    • 导入依赖
    • 使用poi生成段落
    • poi 在word中生成表格
      • 1.生成表格对象
      • 2.设置表格边框
      • 3. 设置表格内文本
      • 4.设置行高
        • 5.设置单元格背景色
        • 6.合并单元格

导入依赖

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.17</version>
        </dependency>

使用poi生成段落

           private XWPFRun setCustomizeOptionParagraph(Integer indentationLeft, ParagraphAlignment align) {
        XWPFParagraph nestOptionParagraph = document.createParagraph();
        // 缩进
        nestOptionParagraph.setIndentationLeft(indentationLeft);
        // 文本水平居中
        nestOptionParagraph.setAlignment(align);
        // 文本垂直居中
        nestOptionParagraph.setVerticalAlignment(TextAlignment.CENTER);
        XWPFRun nestOptionRun = nestOptionParagraph.createRun();
        nestOptionRun.setFontFamily("宋体");
        nestOptionRun.setColor("000000");
        nestOptionRun.setFontSize(9);
        // 字体加粗
         nestOptionRun.setBold(true);
        //回车换行
        nestOptionRun.addCarriageReturn();
        return nestOptionRun;
    }

poi 在word中生成表格

1.生成表格对象

    private XWPFTable setTableWidth(Integer width, Integer rows, Integer cols) {
        //基本信息表格
        XWPFTable infoTable = document.createTable(rows, cols);
        //设置表格宽度
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        // 设置为自定义表格宽度
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(width));
        return infoTable;
    }

2.设置表格边框

    private void setTableBorder(XWPFTable infoTable) {
     	CTTbl ctTbl = infoTable.getCTTbl();
        //设置边框
        CTTblBorders borders = ctTbl.getTblPr().addNewTblBorders();
        String borderColor = "000000";
        CTBorder lBorder = borders.addNewLeft();
        //线条类型
        lBorder.setVal(STBorder.DOT_DOT_DASH);
        // 线条大小
        lBorder.setSz(new BigInteger("1"));
        // 边框颜色
        lBorder.setColor(borderColor);

        CTBorder rBorder = borders.addNewRight();
        rBorder.setVal(STBorder.DOT_DOT_DASH);
        rBorder.setSz(new BigInteger("1"));
        rBorder.setColor(borderColor);

        CTBorder tBorder = borders.addNewTop();
        tBorder.setVal(STBorder.DOT_DOT_DASH);
        tBorder.setSz(new BigInteger("1"));
        tBorder.setColor(borderColor);

        CTBorder bBorder = borders.addNewBottom();
        bBorder.setVal(STBorder.DOT_DOT_DASH);
        bBorder.setSz(new BigInteger("1"));
        bBorder.setColor(borderColor);

    }

3. 设置表格内文本

 private void setCustomizeTableParagraph(XWPFTableCell cell, String text, TextAlignment valign, ParagraphAlignment align) {
        CTP ctp = CTP.Factory.newInstance();
        XWPFParagraph p = new XWPFParagraph(ctp, cell);
        p.setAlignment(align);
        p.setVerticalAlignment(valign);
        XWPFRun run = p.createRun();
        run.setText(text);
        run.setFontSize(9);
        run.setColor("000000");
        run.setFontFamily("宋体");
        cell.setParagraph(p);
    }

4.设置行高

infoTable.getRow(0).setHeight(2800);

5.设置单元格背景色

infoTableRow.getCell(0).setColor(gray);

6.合并单元格

    private void setMergeCell(int row, int cellIndex, XWPFTableCell cell) {
    // 合并列
        if (row == 0) {
            if (cellIndex == 1) {
                //设置合并开始点
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // 设置合并结束点
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }

        //合并行
        if (cellIndex == 0) {
            if (row == 0) {
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            }
            if (row == 1) {
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
            // 设置合并单元格后文本居中
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        }
    }

你可能感兴趣的:(Java语言,#,POI)