POI-5.2.2 操作 word 【表格】

poi 3.17操作Word工具包,点击此处

当前版本 5.2.2 ,版本不同,写法略有差异
- poi-5.2.2 操作word 【段落】
- poi-5.2.2 操作word 【表格】
- poi-5.2.2 操作word【单元格、行、列】
- poi 5.2.2 操作word【页眉页脚】
- poi 5.2.2 操作word【纸张、边距】
- poi-5.2.2 操作word【图片操作相关】
- poi 5.2.2 操作word【目录】


本文所需依赖

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-full</artifactId>
            <version>5.2.2</version>
        </dependency>


- 是否允许表格自动重调单元格宽度

	/**
     * 是否允许表格自动重调单元格宽度 对应【表格属性-表格-选项-自动重调尺寸以适应内容】
     * */
    public static void setTableWidthFixed(XWPFTable table,boolean isFixed){
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTTblLayoutType tblLayout = tblPr.isSetTblLayout() ? tblPr.getTblLayout() : tblPr.addNewTblLayout();
        tblLayout.setType(STTblLayoutType.FIXED);
    }

- 设置表格高度

	/**
     * 设置表格高度
     */
    public static void setTableHight_pt(XWPFTable table, int hight) {
        List<XWPFTableRow> rows2 = table.getRows();
        for (XWPFTableRow xwpfTableRow : rows2) {
            setRowH(xwpfTableRow,hight/rows2.size());
        }
    }

- 设置表头重复

	/**
     *若表格位列两页,则被分开的表格都有一个头部行
     */
    public static void setHeaderRepetition(XWPFTableRow headerRow){
        headerRow.setRepeatHeader(true); // 表头重复
    }

- 设置表格位置(偏移)

	/**
     * 设置表格位置
     * @param tblpX  X方向右移的距离  磅
     * @param tblpY  Y方向下移的距离  磅
     */
    public static void setTableSite(XWPFTable table , float tblpX , float tblpY){
        CTTbl ctTbl = table.getCTTbl();
        CTTblPr tblPr = ctTbl.getTblPr();
        if(tblPr == null){
            tblPr = ctTbl.addNewTblPr();
        }
        CTTblPPr ctTblPPr = tblPr.isSetTblpPr() ? tblPr.getTblpPr() : tblPr.addNewTblpPr();
        ctTblPPr.setVertAnchor(STVAnchor.TEXT);             //定位:随文字移动
        ctTblPPr.setTblpX(Math.round(tblpX * 20));
        ctTblPPr.setTblpY(Math.round(tblpY * 20));
    }

- 去除表格边框

	/**
     * 抹除表格边框
     */
    public static void unsetTableBorder(XWPFTable table){
        table.setLeftBorder(XWPFTable.XWPFBorderType.NONE , 0 , 0 ,"");
        table.setRightBorder(XWPFTable.XWPFBorderType.NONE , 0 , 0 ,"");
        table.setTopBorder(XWPFTable.XWPFBorderType.NONE , 0 , 0 ,"");
        table.setBottomBorder(XWPFTable.XWPFBorderType.NONE , 0 , 0 ,"");
        table.setInsideHBorder(XWPFTable.XWPFBorderType.NONE , 0 , 0 , "");
        table.setInsideVBorder(XWPFTable.XWPFBorderType.NONE , 0 , 0 , "");
    }

- 恢复表格边框

	/**
     * 恢复所有表格边框 默认值
     */
    public static void setDefalutTableBorder(XWPFTable table){
        table.setLeftBorder(XWPFTable.XWPFBorderType.SINGLE , 0 , 0 ,"");
        table.setTopBorder(XWPFTable.XWPFBorderType.SINGLE , 0 , 0 ,"");
        table.setRightBorder(XWPFTable.XWPFBorderType.SINGLE , 0 , 0 ,"");
        table.setBottomBorder(XWPFTable.XWPFBorderType.SINGLE , 0 , 0 ,"");
        table.setInsideHBorder(XWPFTable.XWPFBorderType.SINGLE , 0 , 0 , "");            //单元格底边框
        table.setInsideVBorder(XWPFTable.XWPFBorderType.SINGLE , 0 , 0 , "");            //单元格右边框
    }

- 设置table边框样式

	/**
     * 设置table边框线样式
     * @param  style {@link XWPFTable.XWPFBorderType#NONE}
     * @param  borderW 边框宽度  磅
     * @param  color   颜色值:如 ffffff ,注意不能带 # 号
     */
    public static void setTableBorder(XWPFTable table, XWPFTable.XWPFBorderType style , float borderW , String color) throws Exception {

        int w = Math.round(borderW * 8);
        table.setLeftBorder(style , w , 0 ,color);
        table.setTopBorder(style , w , 0 ,color);
        table.setRightBorder(style , w , 0 ,color);
        table.setBottomBorder(style , w , 0 ,color);
        //注意一下两个方法有限制范围
        table.setInsideHBorder(style , w , 0 , color);            //单元格底边框
        table.setInsideVBorder(style , w , 0 , color);            //单元格右边框
        //该效果的边框需要添加一定的单元格间距
        if(style == XWPFTable.XWPFBorderType.OUTSET || style == XWPFTable.XWPFBorderType.INSET){
            CTTblPr tblPr = table.getCTTbl().getTblPr();
            CTTblWidth ctTblWidth = tblPr.isSetTblCellSpacing() ? tblPr.getTblCellSpacing() : tblPr.addNewTblCellSpacing();
            ctTblWidth.setW(20);
            ctTblWidth.setType(STTblWidth.DXA);
        }
    }

- 表格各列宽度自动分割

	/**
     * 列宽自动分割
     * @param tableWidth 表格宽度 磅
     * @param fixedWidth 单元格宽度是否固定,固定则不会随单元格内文字多少而改变
     */
    public static void setTableWidth_pt(XWPFTable table, float tableWidth , boolean fixedWidth) {
        CTTblPr tblPr = getCTTblPr(table);
        CTTblWidth comTableWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
        comTableWidth.setType(STTblWidth.DXA);
        comTableWidth.setW(Math.round(tableWidth * 20));
        List<XWPFTableRow> rows2 = table.getRows();
        for (XWPFTableRow xwpfTableRow : rows2) {
            xwpfTableRow.setCantSplitRow(true);
            if(fixedWidth){
                float cellW = tableWidth / xwpfTableRow.getTableCells().size();
                for(XWPFTableCell cell : xwpfTableRow.getTableCells()){
                    setCellW(cell , cellW);
                }
            }
        }
    }

- 设置表格单元格为灰白交替样式

	/**
     * 设置表格样式为 黑白间隔样式
     * @param beginRowIndex              从哪行开始设置(下标值)
     * @param beginCellIndex             从哪列开始设置(下标值)
     */
    public static void setTableStyle_BW(XWPFTable table , int beginRowIndex , int beginCellIndex){
        List<XWPFTableRow> rows = table.getRows();
        for(int i = beginRowIndex + 1; i < rows.size(); i+=2){
            XWPFTableRow row = rows.get(i);
            List<XWPFTableCell> cells = row.getTableCells();
            for(int c = beginCellIndex; c < cells.size(); c++){
                XWPFTableCell cell = cells.get(c);
                if(StringUtil.empty(cell.getColor())){
                    cell.setColor("f1f0f3");
                }
            }
        }
    }

- 设置表格水平对齐方式

 	/** 设置表格水平对齐方式
     * @param align {@link STJcTable#CENTER}*/
    public static void setTableAlign(XWPFTable table , STJcTable.Enum align){
        //表格总体
        CTTblPr tblPr = getCTTblPr(table);
        CTJcTable ctJcTable = tblPr.isSetJc() ? tblPr.getJc() : tblPr.addNewJc();
        ctJcTable.setVal(align);

        //表格各行
        List<XWPFTableRow> rows = table.getRows();
        for(XWPFTableRow row : rows){
            CTRow ctRow = row.getCtRow();
            CTTrPr ctTrPr = ctRow.isSetTrPr() ? ctRow.getTrPr() : ctRow.addNewTrPr();
            ctTrPr.addNewJc().setVal(align);
        }
    }

- 设置表格缩进

	/** 设置表格缩进 对应【表格属性-表格-左缩进】
     * @param ind 左缩进值  磅 */
    public static void setTableInd(XWPFTable table , float ind){
        CTTblPr tblPr = getCTTblPr(table);
        CTTblWidth ctTblWidth = tblPr.isSetTblInd() ? tblPr.getTblInd() : tblPr.addNewTblInd();
        ctTblWidth.setW(BigInteger.valueOf((int) (ind * 20f)));
        ctTblWidth.setType(STTblWidth.DXA);
    }

- 设置表格内的所有单元格边距

	/**
     * @Description 设置表格内的所有单元格边距
     * @param table 待设置的表格
     * @param left  左边距 磅
     * @param top   上边距 磅
     * @param right 右边距 磅
     * @param bottom    下边距 磅
     */
    public static void setTableCellMar(XWPFTable table , float left , float top , float right , float bottom){
        CTTblPr ctTblPr = getCTTblPr(table);
        CTTblCellMar ctTblCellMar = ctTblPr.isSetTblCellMar() ? ctTblPr.getTblCellMar() : ctTblPr.addNewTblCellMar();
        if(left >= 0){
            (ctTblCellMar.isSetLeft() ? ctTblCellMar.getLeft() : ctTblCellMar.addNewLeft()).setW(Math.round(left * 20));
        }
        if(top >= 0){
            (ctTblCellMar.isSetTop() ? ctTblCellMar.getTop() : ctTblCellMar.addNewTop()).setW(Math.round(top * 20));
        }
        if(right >= 0){
            (ctTblCellMar.isSetRight() ? ctTblCellMar.getRight() : ctTblCellMar.addNewRight()).setW(Math.round(right * 20));
        }
        if(bottom >= 0){
            (ctTblCellMar.isSetBottom() ? ctTblCellMar.getBottom() : ctTblCellMar.addNewBottom()).setW(Math.round(bottom * 20));
        }
    }

- 涉及子方法,若有方法找不到看这里

private static CTTblPr getCTTblPr(XWPFTable table){
        CTTbl ctTbl = table.getCTTbl();
        CTTblPr tblPr = ctTbl.getTblPr();
        if(tblPr == null){
            tblPr = ctTbl.addNewTblPr();
        }
        return tblPr;
    }
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
poi-ooxml-5.2.2.jar是一个Java库,用于操作Microsoft Office Open XML(OOXML)格式的文档。它是Apache POI项目的一部分,POI是一个开源的Java库,用于读取、创建和修改各种格式的Office文档。 poi-ooxml-5.2.2.jar提供了一系列类和方法,用于处理OOXML格式的文档,包括.xlsx电子表格文件和.docx文档文件。使用这个库,我们可以读取和提取文档中的数据,修改和创建新的文档,添加、删除或修改单元格、行、列、图表、图像等元素。它还支持文本格式设置、样式、公式计算等功能。 使用poi-ooxml-5.2.2.jar,我们可以通过编程方式对OOXML文档进行自动化处理。例如,我们可以读取电子表格中的数据,并进行各种计算和分析,或者将数据导出到其他格式。我们还可以创建自定义的模板文件,填充数据并生成新的文档。 poi-ooxml-5.2.2.jar是在Apache许可证下发布的,所以我们可以免费使用它,并集成到我们的Java应用程序中。它是一个功能强大且广泛使用的库,被许多开发人员和企业用于处理Office文档。无论是处理大量数据还是进行高级文档操作poi-ooxml-5.2.2.jar都可以提供方便和高效的解决方案。 总之,poi-ooxml-5.2.2.jar是一个处理Microsoft Office Open XML格式文档的Java库,它可以帮助我们读取、创建和修改xlsx和docx文档,提供了丰富的功能和灵活性,能够满足各种对于OOXML文档的处理需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值