当前版本 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 setCellBackGround(XWPFTableCell cell , String color){
CTTc ctTc = cell.getCTTc();
CTTcPr tcPr = ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr();
CTShd ctShd = tcPr.isSetShd() ? tcPr.getShd() : tcPr.addNewShd();
ctShd.setVal(STShd.CLEAR);
ctShd.setFill(color);
}
- 设置单元格宽度
/**设置单元格宽度 单位 磅*/
public static void setCellW(XWPFTableCell cell , float w){
CTTc ctTc = cell.getCTTc();
CTTcPr tcPr = ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr();
CTTblWidth tcW = tcPr.isSetTcW() ? tcPr.getTcW() : tcPr.addNewTcW();
tcW.setType(STTblWidth.DXA);
tcW.setW(BigInteger.valueOf((int) (w * 20f)));
}
- 获取单元格宽度
/** 获取单元格宽度 单位 磅*/
public static int getCellW(XWPFTableCell cell){
try {
return Integer.parseInt(cell.getCTTc().getTcPr().getTcW().getW().toString()) / 20;
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
- 设置行高
/**设置表格行 的 高度 单位 磅 。 */
public static void setRowH(XWPFTableRow row , float h){
row.setHeight(Math.round(h * 20));
}
- 获取行高
/**获取表格行 的 高度 单位 磅 。 */
public static int getRowH(XWPFTableRow row){
return row.getHeight() / 20;
}
- 移除单元格
/** 移除单元格 */
public static void removeCell(XWPFTable table , int rows , int cols){
XWPFTableRow row = table.getRow(rows);
XWPFTableCell cell = row.getCell(cols);
List<XWPFParagraph> paragraphs = cell.getParagraphs();
for(int p = 0; p < paragraphs.size(); p++){
cell.removeParagraph(p);
}
row.removeCell(cols);
row.getCtRow().removeTc(cols);
}
- 复刻一个单元格
/** 复刻单元格 */
public static void copyCell(XWPFTableCell from , XWPFTableCell to){
to.getCTTc().getTcPr().set(from.getCTTc().getTcPr());
if(StringUtil.empty(to.getParagraphArray(0).getText())){
to.removeParagraph(0);
}
for(XWPFParagraph p : from.getParagraphs()){
to.addParagraph().getCTP().set(p.getCTP());
}
}
- 绘制单元格斜分割线
/**
* 对应表格 绘制表格,用于给单元格 左上-右下 斜线分割
*/
public static void setDrawBias(XWPFTableCell cell){
CTTcPr tcPr = getCttcpr(cell);
CTTcBorders ctTcBorders = tcPr.isSetTcBorders() ? tcPr.getTcBorders() : tcPr.addNewTcBorders();
CTBorder ctBorder = ctTcBorders.isSetTl2Br() ? ctTcBorders.getTl2Br() : ctTcBorders.addNewTl2Br();
ctBorder.setVal(STBorder.SINGLE);
ctBorder.setSz(BigInteger.valueOf(4));
ctBorder.setSpace(BigInteger.valueOf(0));
ctBorder.setColor("auto");
}
- 去除单元格边框线
/**移除单元格对应的边框线*/
public static void removeCellLeftBorder(XWPFTableCell cell){
cell.getCTTc().getTcPr().addNewTcBorders().addNewLeft().setVal(STBorder.NIL);
}
public static void removeCellRightBorder(XWPFTableCell cell){
cell.getCTTc().getTcPr().addNewTcBorders().addNewRight().setVal(STBorder.NIL);
}
public static void removeCellTopBorder(XWPFTableCell cell){
cell.getCTTc().getTcPr().addNewTcBorders().addNewTop().setVal(STBorder.NIL);
}
public static void removeCellBottomBorder(XWPFTableCell cell){
cell.getCTTc().getTcPr().addNewTcBorders().addNewBottom().setVal(STBorder.NIL);
}
- 移除单元格边框、设置单元格单框样式
/**移除单元格 左 边框线*/
public static void removeCellLeftBorder(XWPFTableCell cell){
setCellLeftBorder(cell , STBorder.NIL , 0 , 0 , "");
}
/**移除单元格 右 边框线*/
public static void removeCellRightBorder(XWPFTableCell cell){
setCellRightBorder(cell , STBorder.NIL , 0 , 0 , "");
}
/**移除单元格 上 边框线*/
public static void removeCellTopBorder(XWPFTableCell cell){
setCellTopBorder(cell , STBorder.NIL , 0 , 0 , "");
}
/**移除单元格 下 边框线*/
public static void removeCellBottomBorder(XWPFTableCell cell){
setCellBottomBorder(cell , STBorder.NIL , 0 , 0 , "");
}
/**
* @Description 设置单元格左边框样式
* @param cell 单元格
* @param borderType 边框类型
* @param borderW 边框宽度 磅
* @param space 左间隔
* @param color 边框颜色
*/
public static void setCellLeftBorder(XWPFTableCell cell , STBorder.Enum borderType , float borderW , float space , String color){
CTTcBorders ctTcBorders = getCttcBorders(cell);
CTBorder leftBorder = ctTcBorders.isSetLeft() ? ctTcBorders.getLeft() : ctTcBorders.addNewLeft();
leftBorder.setVal(borderType);
leftBorder.setSz(BigInteger.valueOf(Math.round(borderW * 8)));
leftBorder.setSpace(BigInteger.valueOf((int)space));
leftBorder.setColor(color);
}
/**
* @Description 设置单元格上边框样式
* @param cell 单元格
* @param borderType 边框类型
* @param borderW 边框宽度 磅
* @param space 左间隔
* @param color 边框颜色
*/
public static void setCellTopBorder(XWPFTableCell cell , STBorder.Enum borderType , float borderW , float space , String color){
CTTcBorders ctTcBorders = getCttcBorders(cell);
CTBorder topBorder = ctTcBorders.isSetTop() ? ctTcBorders.getTop() : ctTcBorders.addNewTop();
topBorder.setVal(borderType);
topBorder.setSz(BigInteger.valueOf(Math.round(borderW * 8)));
topBorder.setSpace(BigInteger.valueOf((int)space));
topBorder.setColor(color);
}
/**
* @Description 设置单元格右边框样式
* @param cell 单元格
* @param borderType 边框类型
* @param borderW 边框宽度 磅
* @param space 左间隔
* @param color 边框颜色
*/
public static void setCellRightBorder(XWPFTableCell cell , STBorder.Enum borderType , float borderW , float space , String color){
CTTcBorders ctTcBorders = getCttcBorders(cell);
CTBorder rightBorder = ctTcBorders.isSetRight() ? ctTcBorders.getRight() : ctTcBorders.addNewRight();
rightBorder.setVal(borderType);
rightBorder.setSz(BigInteger.valueOf(Math.round(borderW * 8)));
rightBorder.setSpace(BigInteger.valueOf((int)space));
rightBorder.setColor(color);
}
/**
* @Description 设置单元格下边框样式
* @param cell 单元格
* @param borderType 边框类型
* @param borderW 边框宽度 磅
* @param space 左间隔
* @param color 边框颜色
*/
public static void setCellBottomBorder(XWPFTableCell cell , STBorder.Enum borderType , float borderW , float space , String color){
CTTcBorders ctTcBorders = getCttcBorders(cell);
CTBorder bottomBorder = ctTcBorders.isSetBottom() ? ctTcBorders.getBottom() : ctTcBorders.addNewBottom();
bottomBorder.setVal(borderType);
bottomBorder.setSz(BigInteger.valueOf(Math.round(borderW * 8)));
bottomBorder.setSpace(BigInteger.valueOf((int)space));
bottomBorder.setColor(color);
}
- 是否允许 表格/行 跨页断行
/** 允许表格跨页断行,一个单元格里面的内容是否允许出现在两页上 */
public static void setTableSplit(XWPFTable table , boolean isSplit){
List<XWPFTableRow> rows = table.getRows();
for(XWPFTableRow row : rows){
setRowSplit(row , isSplit);
}
}
/** 是否允许该行 跨页断行 ,一个单元格里面的内容是否允许出现在两页上 */
public static void setRowSplit(XWPFTableRow row , boolean isSplit){
CTRow ctRow = row.getCtRow();
CTTrPr trPr = ctRow.isSetTrPr() ? ctRow.getTrPr() : ctRow.addNewTrPr();
if(isSplit){
if(trPr.sizeOfCantSplitArray() == 0){
trPr.addNewCantSplit();
}
}else{
for(int i = 0; i < trPr.sizeOfCantSplitArray(); i++){
trPr.removeCantSplit(i);
}
}
}
- 设置单元格边距
/**
* @Description 设置单元格边距
* @param cell 待设置的单元格
* @param left 左边距 磅
* @param top 上边距 磅
* @param right 右边距 磅
* @param bottom 下边距 磅
*/
public static void setTableCellMar(XWPFTableCell cell , float left , float top , float right , float bottom){
CTTcPr cttcpr = getCttcpr(cell);
CTTcMar ctTcMar = cttcpr.isSetTcMar() ? cttcpr.getTcMar() : cttcpr.addNewTcMar();
if(left >= 0){
(ctTcMar.isSetLeft() ? ctTcMar.getLeft() : ctTcMar.addNewLeft()).setW(Math.round(left * 20));
}
if(top >= 0){
(ctTcMar.isSetTop() ? ctTcMar.getTop() : ctTcMar.addNewTop()).setW(Math.round(top * 20));
}
if(right >= 0){
(ctTcMar.isSetRight() ? ctTcMar.getRight() : ctTcMar.addNewRight()).setW(Math.round(right * 20));
}
if(bottom >= 0){
(ctTcMar.isSetBottom() ? ctTcMar.getBottom() : ctTcMar.addNewBottom()).setW(Math.round(bottom * 20));
}
}
- 合并单元格(合并列、跨列合并)
/**
* 跨列合并,横着合并 , 都是下标值
* @param rowindex 要合并哪一行的列
* @param fromcell 从哪列开始合并(下标)
* @param endcell 合并到哪列结束(下标)
*/
public static void mergeCellsByCol(XWPFTable table, int rowindex, int fromcell, int endcell) {
//2013及之前的老版本自己合并自己会出乱版,这里防止自己合并自己
if(fromcell == endcell){
return;
}
for (int cellindex = fromcell; cellindex <= endcell; cellindex++) {
XWPFTableCell cell = table.getRow(rowindex).getCell(cellindex);
CTTc ctTc = cell.getCTTc();
CTTcPr tcPr = ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr();
CTHMerge hMerge = tcPr.isSetHMerge() ? tcPr.getHMerge() : tcPr.addNewHMerge();
if (cellindex == fromcell) {
//这个判断是为了处理 2 3 合并 之后又 3 4 合并,如果不做判断最终合并的只有3 4
if(!STMerge.CONTINUE.equals(hMerge.getVal())){
hMerge.setVal(STMerge.RESTART);
}
} else {
hMerge.setVal(STMerge.CONTINUE);
}
}
}
- 合并单元格(合并行、跨行合并)
/**
* 跨行合并 , 都是下标值
* @param cellindex 要合并哪一列的行
* @param fromrow 从哪行开始合并(下标)
* @param endrow 合并到哪行结束(下标)
*/
public static void mergeCellByRow(XWPFTable table, int cellindex, int fromrow, int endrow) {
//2013及之前的老版本自己合并自己会出乱版,这里防止自己合并自己
if(fromrow == endrow){
return;
}
for (int rowindex = fromrow; rowindex <= endrow; rowindex++) {
XWPFTableRow row = table.getRow(rowindex);
CTTc ctTc = row.getCell(cellindex).getCTTc();
CTTcPr tcPr = ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr();
CTVMerge vMerge = tcPr.isSetVMerge() ? tcPr.getVMerge() : tcPr.addNewVMerge();
if (rowindex == fromrow) {
if(!STMerge.CONTINUE.equals(vMerge.getVal())){
vMerge.setVal(STMerge.RESTART);
}
} else {
vMerge.setVal(STMerge.CONTINUE);
}
}
}
- 检查当前单元格是否被合并
/**
* 检查当前单元格是否是合并后的单元格
* @Return boolean
*/
public static boolean isMergedCell(XWPFTableCell cell){
CTTcPr cttcpr = getCttcpr(cell);
if(cttcpr.isSetVMerge()){
CTVMerge vMerge = cttcpr.getVMerge();
if(STMerge.CONTINUE.equals(vMerge.getVal())){
return true;
}
}
if(cttcpr.isSetHMerge()){
CTHMerge hMerge = cttcpr.getHMerge();
if(STMerge.CONTINUE.equals(hMerge.getVal())){
return true;
}
}
return false;
}
- 涉及子方法,上面的方法内若是报错看这里
/** 获取边框对象 */
private static CTTcBorders getCttcBorders(XWPFTableCell cell){
CTTcPr ctTcPr = getCttcpr(cell);
return ctTcPr.isSetTcBorders() ? ctTcPr.getTcBorders() : ctTcPr.addNewTcBorders();
}
private static CTTcPr getCttcpr(XWPFTableCell cell){
CTTc ctTc = cell.getCTTc();
return ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr();
}