当前版本 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 XWPFParagraph setPageBreak(XWPFDocument document){
XWPFParagraph pageParagraph = document.createParagraph();
pageParagraph.setPageBreak(true);
return pageParagraph;
}
- 插入段落换行符
public static void setParagraphBreak(XWPFParagraph paragraph){
paragraph.createRun().addBreak();
}
- 设置段落内文本上下对齐方式【段落-中文板式-文本对齐方式】
/**
* 设置段落内文本上下对齐方式【段落-中文板式-文本对齐方式】
* @param textAlignment {@link STTextAlignment#CENTER}
*/
public static void setTextAlignment(XWPFParagraph paragraph , STTextAlignment.Enum textAlignment){
CTP ctp = paragraph.getCTP();
CTPPr ctpPr = ctp.isSetPPr() ? ctp.getPPr() : ctp.addNewPPr();
CTTextAlignment ctTextAlignment = CTTextAlignment.Factory.newInstance();
ctTextAlignment.setVal(textAlignment);
ctpPr.setTextAlignment(ctTextAlignment);
}
- 调整段落间距【段落-间距】
/**
* 调整段落间距【对应word段落设置-间距设置】
* @param before 段前 磅
* @param after 段后 磅
* @param multiple 几倍行距
*/
public static void setParagraphSpacing(XWPFParagraph paragraph , float before , float after , float multiple) {
CTPPr ppr = paragraph.getCTP().getPPr();
if (ppr == null) {
ppr = paragraph.getCTP().addNewPPr();
}
CTSpacing spacing = ppr.isSetSpacing()? ppr.getSpacing() : ppr.addNewSpacing();
spacing.setBefore(Math.round(before * 20));
spacing.setAfter(Math.round(after * 20));
spacing.setLine(Math.round(240 * multiple));
spacing.setLineRule(STLineSpacingRule.AUTO);
}
- 设置固定行高
/** 固定行高
* @param h 行高 磅*/
public static void setParagraphHight(XWPFParagraph paragraph , float h){
CTPPr ppr = paragraph.getCTP().getPPr();
if (ppr == null) {
ppr = paragraph.getCTP().addNewPPr();
}
CTSpacing spacing = ppr.isSetSpacing()? ppr.getSpacing() : ppr.addNewSpacing();
spacing.setBefore(BigInteger.valueOf(0));
spacing.setAfter(BigInteger.valueOf(0));
spacing.setLine(BigInteger.valueOf((long) (20 * h)));
spacing.setLineRule(STLineSpacingRule.EXACT);
}
- 是否允许西文在单词中间换行
/**
* 段落设置-中文板式 : 允许西文在单词中间换行
* */
public static void setParagraphWrap(XWPFParagraph paragraph , boolean allow){
//解释: 允许后(勾选) word展示效果
// | 这是一个段落, | |带来了珍贵的礼物—— |
// |paragraph | | 五彩缤纷的落叶。 |
// 不允许(取消勾选) word展示效果
// | 这是一个段落,par| |带来了珍贵的礼物——五彩缤 |
// |agraph | |纷的落叶。 |
CTP ctp = paragraph.getCTP();
CTPPr ctpPr = ctp.isSetPPr() ? ctp.getPPr() : ctp.addNewPPr();
CTOnOff ctOnOff = ctpPr.isSetWordWrap() ? ctpPr.getWordWrap() : ctpPr.addNewWordWrap();
if(allow){ //允许
ctOnOff.setVal(STOnOff.EQUAL);
}else{ //不允许
ctpPr.unsetWordWrap();
}
}
- 设置段落缩进
/**
* 设置段落缩进
* @param left 左缩进值 磅
* @param right 右缩进值 磅
*/
public static void setParagraphInd(XWPFParagraph paragraph , float left , float right){
CTPPr ppr = paragraph.getCTP().getPPr();
if (ppr == null) {
ppr = paragraph.getCTP().addNewPPr();
}
CTInd ctInd = ppr.isSetInd() ? ppr.getInd() : ppr.addNewInd();
if(left >= 0){
ctInd.setLeft(Math.round(left * 20));
}
if(right >= 0){
ctInd.setRight(Math.round(right * 20));
}
}
- 设置 段落内 文本位置【字符间距】
/**
* 设置 段落内 文本位置【字符间距】
* @param positionValue 【上下位置调整 正 上移 负 下移】 单位 磅
* @param spacingValue 【字符间距调整 宽窄-正负】单位 磅
*/
public static void setPosition_paragraph(XWPFParagraph paragraph , int positionValue, int spacingValue){
List<XWPFRun> runs = paragraph.getRuns();
for(XWPFRun run : runs){
setPosition_run(run , positionValue , spacingValue);
}
}
- 设置文段Run位置【对应word字体 - 高级 - 字符间距】
/**
* 设置文段Run位置【对应word字体 - 高级 - 字符间距】
* @param positionValue 【上下位置调整正负】 单位 磅
* @param spacingValue 【字符间距调整 宽窄-正负】单位 磅
*/
public static void setPosition_run(XWPFRun run , float positionValue , float spacingValue){
CTRPr rPr = run.getCTR().getRPr();
if(rPr == null){
rPr = run.getCTR().addNewRPr();
}
CTSignedHpsMeasure position = rPr.sizeOfPositionArray() > 0 ? rPr.getPositionArray(0) : null;
if(position == null){
rPr.addNewPosition().setVal(positionValue * 2);
}else{
position.setVal(positionValue * 2);
}
CTSignedTwipsMeasure spacing = rPr.sizeOfSpacingArray() > 0 ? rPr.getSpacingArray(0) : null;
if(spacing == null){
rPr.addNewSpacing().setVal(spacingValue * 20);
}else{
spacing.setVal(spacingValue * 20);
}
}
- 设置整个段落内文本样式
/**
* 设置整个段落内样式
* @param fontFamily 字体
* @param fontSize 字体大小 磅
* @param bold 是否加粗
* @param color 字体颜色
*/
public static void setStyle(XWPFParagraph paragraph, String fontFamily, int fontSize, boolean bold, String color) {
CTP ctp1 = paragraph.getCTP();
CTPPr pPr = ctp1.isSetPPr() ? ctp1.getPPr() : ctp1.addNewPPr();
CTParaRPr rPr = pPr.isSetRPr() ? pPr.getRPr() : pPr.addNewRPr();
CTFonts ctFonts = rPr.sizeOfRFontsArray() > 0 ? rPr.getRFontsArray(0) : rPr.addNewRFonts();
ctFonts.setAscii(fontFamily);
ctFonts.setEastAsia(fontFamily);
ctFonts.setHAnsi(fontFamily);
CTHpsMeasure sz = rPr.sizeOfSzArray() > 0 ? rPr.getSzArray(0) : rPr.addNewSz();
sz.setVal(BigInteger.valueOf(fontSize * 2L));
if(bold){
rPr.addNewB();
}else{
if(rPr.sizeOfBArray() > 0){
rPr.removeB(0);
}
}
if(!StringUtil.empty(color)){
CTColor ctColor = rPr.sizeOfColorArray() > 0 ? rPr.getColorArray(0) : rPr.addNewColor();
ctColor.setVal(color);
}
}
- 设置单个 run 的样式
/**
* 设置样式
*/
public static void setStyle(XWPFRun run, String fontFamily, float fontSize, boolean bold, String text, String color) {
setStyle(run , fontFamily , fontSize , bold , text , color , false , false);
}
public static void setStyle(XWPFRun run, String fontFamily, float fontSize, boolean bold, String text, String color,boolean em , boolean i) {
run.setBold(bold);
run.setFontFamily(fontFamily);
run.setFontSize(fontSize);
if(text != null){
run.setText(text);
}
run.setColor(StringUtil.empty(color) ? "000000" : color); //颜色默认黑色
//着重号
if(em){
run.getCTR().getRPr().addNewEm().setVal(STEm.UNDER_DOT);
}
if(i){
run.setItalic(true);
}
}
public static void setStyle(XWPFRun run, String fontFamily, float fontSize, boolean bold, String text, String color , STUnderline.Enum underType , String underColor){
setStyle(run , fontFamily , fontSize , bold , text , color , false , false , underType , underColor);
}
/**
* 设置单个run的样式
* @param run
* @param fontFamily 字体
* @param fontSize 文字大小 磅
* @param bold 是否加粗
* @param text run内文本
* @param color 文本颜色
* @param em 是否添加着重号
* @param i 是否倾斜
* @param underType 下划线类型{@link STUnderline#SINGLE}
* @param underColor 下划线颜色
*
*/
public static void setStyle(XWPFRun run, String fontFamily, float fontSize, boolean bold, String text, String color ,boolean em , boolean i , STUnderline.Enum underType , String underColor){
setStyle(run , fontFamily , fontSize , bold , text , color , em , i);
CTR ctr = run.getCTR();
CTRPr ctrPr = ctr.isSetRPr() ? ctr.getRPr() : ctr.addNewRPr();
CTUnderline ctUnderline = ctrPr.sizeOfUArray() > 0 ? ctrPr.getUArray(0) : ctrPr.addNewU();
ctUnderline.setVal(underType);
if(!StringUtil.empty(underColor)){
ctUnderline.setColor(underColor);
}
}
- 设置文本填充色
/**
* 设置文本填充色
*/
public static void setFillColor(XWPFRun run , String color){
CTR ctr = run.getCTR();
CTRPr rPr = ctr.isSetRPr() ? ctr.getRPr() : ctr.addNewRPr();
CTShd ctShd = rPr.sizeOfShdArray() > 0 ? rPr.getShdArray(0) : rPr.addNewShd();
ctShd.setFill(color);
}
- 移除一个段落或表格
/** 移除一个段落或者表格 */
public static void removeIBodyElement(XWPFDocument document , IBodyElement iBodyElement){
if(iBodyElement != null){
if(iBodyElement.getElementType() == BodyElementType.PARAGRAPH){ //段落
removeParagraph(document , ((XWPFParagraph) iBodyElement));
}else if(iBodyElement.getElementType() == BodyElementType.TABLE){ //表格
removeTable(document , (XWPFTable) iBodyElement);
}
}
}