当前版本 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;
}