poi操作word 2007 常用方法总结
更新时间:2023-11-18 05:51:01 阅读量: 教育文库 文档下载
- poi操作word模板推荐度:
- 相关推荐
poi操作word 2007 常用方法总结
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.BodyElementType;
import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBackground;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEm;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHighlight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTInd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTMarkupRange;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTParaRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSignedTwipsMeasure;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTextScale;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STEm;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STPageOrientation;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
public class POI_Word2007_工具类_S3_Test {
// 替换 复制 表格 段落
/*------------------------------------Word 插入书签--------------------------------------------------- */
/**
* @Description: 添加书签
*/
public void addParagraphContentBookmarkBasicStyle(XWPFParagraph p,
String content, BigInteger markId, String bookMarkName,
boolean isInsert, boolean isNewLine, String fontFamily,
String fontSize, String colorVal, boolean isBlod,
boolean isUnderLine, String underLineColor,
STUnderline.Enum underStyle, boolean isItalic, boolean isStrike) {
CTBookmark bookStart = p.getCTP().addNewBookmarkStart();
bookStart.setId(markId);
bookStart.setName(bookMarkName);
XWPFRun pRun = getOrAddParagraphFirstRun(p, isInsert, isNewLine);
setParagraphRunFontInfo(p, pRun, content, fontFamily, fontSize);
setParagraphTextStyleInfo(p, pRun, colorVal, isBlod, isUnderLine,
underLineColor, underStyle, isItalic, isStrike, false, false,
false, false, false, false, false, null, false, null, false,
null, null, null, 0, 0, 0);
CTMarkupRange bookEnd = p.getCTP().addNewBookmarkEnd();
bookEnd.setId(markId);
}
/**
* @Description: 添加书签
*/
public void addParagraphContentBookmark(XWPFParagraph p, String content,
BigInteger markId, String bookMarkName, boolean isInsert,
boolean isNewLine, String fontFamily, String fontSize,
String colorVal, boolean isBlod, boolean isUnderLine,
String underLineColor, STUnderline.Enum underStyle,
boolean isItalic, boolean isStrike, boolean isDStrike,
boolean isShadow, boolean isVanish, boolean isEmboss,
boolean isImprint, boolean isOutline, boolean isEm,
STEm.Enum emType, boolean isHightLight,
STHighlightColor.Enum hightStyle, boolean isShd,
STShd.Enum shdStyle, String shdColor, VerticalAlign verticalAlign,
int position, int spacingValue, int indent) {
CTBookmark bookStart = p.getCTP().addNewBookmarkStart();
bookStart.setId(markId);
bookStart.setName(bookMarkName);
XWPFRun pRun = getOrAddParagraphFirstRun(p, isInsert, isNewLine);
setParagraphRunFontInfo(p, pRun, content, fontFamily, fontSize);
setParagraphTextStyleInfo(p, pRun, colorVal, isBlod, isUnderLine,
underLineColor, underStyle, isItalic, isStrike, isDStrike,
isShadow, isVanish, isEmboss, isImprint, isOutline, isEm,
emType, isHightLight, hightStyle, isShd, shdStyle, shdColor,
verticalAlign, position, spacingValue, indent);
CTMarkupRange bookEnd = p.getCTP().addNewBookmarkEnd();
bookEnd.setId(markId);
}
/*------------------------------------Word 插入超链接--------------------------------------------------- */
/**
* @Description: 默认的超链接样式
*/
public void addParagraphTextHyperlinkBasicStyle(XWPFParagraph paragraph,
String url, String text, String fontFamily, String fontSize,
String colorVal, boolean isBlod, boolean isItalic, boolean isStrike) {
addParagraphTextHyperlink(paragraph, url, text, fontFamily, fontSize,
colorVal, isBlod, true, \, STUnderline.SINGLE, isItalic,
isStrike, false, false, false, false, false, false, false,
null, false, null, false, null, null, null, 0, 0, 0);
}
/**
* @Description: 设置超链接样式
*/
public void addParagraphTextHyperlink(XWPFParagraph paragraph, String url,
String text, String fontFamily, String fontSize, String colorVal,
boolean isBlod, boolean isUnderLine, String underLineColor,
STUnderline.Enum underStyle, boolean isItalic, boolean isStrike,
boolean isDStrike, boolean isShadow, boolean isVanish,
boolean isEmboss, boolean isImprint, boolean isOutline,
boolean isEm, STEm.Enum emType, boolean isHightLight,
STHighlightColor.Enum hightStyle, boolean isShd,
STShd.Enum shdStyle, String shdColor,
STVerticalAlignRun.Enum verticalAlign, int position,
int spacingValue, int indent) {
// Add the link as External relationship
String id = paragraph
.getDocument()
.getPackagePart()
.addExternalRelationship(url,
XWPFRelation.HYPERLINK.getRelation()).getId();
// Append the link and bind it to the relationship
CTHyperlink cLink = paragraph.getCTP().addNewHyperlink();
cLink.setId(id);
// Create the linked text
CTText ctText = CTText.Factory.newInstance();
ctText.setStringValue(text);
CTR ctr = CTR.Factory.newInstance();
CTRPr rpr = ctr.addNewRPr();
if (StringUtils.isNotBlank(fontFamily)) {
// 设置字体
CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr
.addNewRFonts();
fonts.setAscii(fontFamily);
fonts.setEastAsia(fontFamily);
fonts.setHAnsi(fontFamily);
}
if (StringUtils.isNotBlank(fontSize)) {
// 设置字体大小
CTHpsMeasure sz = rpr.isSetSz() ? rpr.getSz() : rpr.addNewSz();
sz.setVal(new BigInteger(fontSize));
CTHpsMeasure szCs = rpr.isSetSzCs() ? rpr.getSzCs() : rpr
.addNewSzCs();
szCs.setVal(new BigInteger(fontSize));
}
// 设置超链接样式
// 字体颜色
if (StringUtils.isNotBlank(colorVal)) {
CTColor color = CTColor.Factory.newInstance();
color.setVal(colorVal);
rpr.setColor(color);
}
// 加粗
if (isBlod) {
CTOnOff bCtOnOff = rpr.addNewB();
bCtOnOff.setVal(STOnOff.TRUE);
}
// 下划线
if (isUnderLine) {
CTUnderline udLine = rpr.addNewU();
udLine.setVal(underStyle);
udLine.setColor(underLineColor);
}
// 倾斜
if (isItalic) {
CTOnOff iCtOnOff = rpr.addNewI();
iCtOnOff.setVal(STOnOff.TRUE);
}
// 删除线
if (isStrike) {
CTOnOff sCtOnOff = rpr.addNewStrike();
sCtOnOff.setVal(STOnOff.TRUE);
}
// 双删除线
if (isDStrike) {
CTOnOff dsCtOnOff = rpr.addNewDstrike();
dsCtOnOff.setVal(STOnOff.TRUE);
}
// 阴影
if (isShadow) {
CTOnOff shadowCtOnOff = rpr.addNewShadow();
shadowCtOnOff.setVal(STOnOff.TRUE);
}
// 隐藏
if (isVanish) {
CTOnOff vanishCtOnOff = rpr.addNewVanish();
vanishCtOnOff.setVal(STOnOff.TRUE);
}
// 阳文
if (isEmboss) {
CTOnOff embossCtOnOff = rpr.addNewEmboss();
embossCtOnOff.setVal(STOnOff.TRUE);
}
// 阴文
if (isImprint) {
CTOnOff isImprintCtOnOff = rpr.addNewImprint();
isImprintCtOnOff.setVal(STOnOff.TRUE);
}
// 空心
if (isOutline) {
CTOnOff isOutlineCtOnOff = rpr.addNewOutline();
isOutlineCtOnOff.setVal(STOnOff.TRUE);
}
// 着重号
if (isEm) {
CTEm em = rpr.addNewEm();
em.setVal(emType);
}
// 突出显示文本
if (isHightLight) {
if (hightStyle != null) {
CTHighlight hightLight = rpr.addNewHighlight();
hightLight.setVal(hightStyle);
}
}
if (isShd) {
// 设置底纹
CTShd shd = rpr.addNewShd();
if (shdStyle != null) {
shd.setVal(shdStyle);
}
if (shdColor != null) {
shd.setColor(shdColor);
}
}
// 上标下标
if (verticalAlign != null) {
rpr.addNewVertAlign().setVal(verticalAlign);
}
// 设置文本位置
rpr.addNewPosition().setVal(new BigInteger(String.valueOf(position)));
if (spacingValue != 0) {
// 设置字符间距信息
CTSignedTwipsMeasure ctSTwipsMeasure = rpr.addNewSpacing();
ctSTwipsMeasure
.setVal(new BigInteger(String.valueOf(spacingValue)));
}
// 设置字符间距缩进
if (indent > 0) {
CTTextScale paramCTTextScale = rpr.addNewW();
paramCTTextScale.setVal(indent);
}
ctr.setTArray(new CTText[] { ctText });
cLink.setRArray(new CTR[] { ctr });
}
/*------------------------------------Word 页眉页脚相关--------------------------------------------------- */
/**
* @Description: 页脚:显示页码信息
*/
public void simpleNumberFooter(XWPFDocument document) throws Exception {
CTP ctp = CTP.Factory.newInstance();
XWPFParagraph codePara = new XWPFParagraph(ctp, document);
XWPFRun r1 = codePara.createRun();
r1.setText(\第\);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
r1 = codePara.createRun();
CTFldChar fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.BEGIN);
r1 = codePara.createRun();
CTText ctText = r1.getCTR().addNewInstrText();
ctText.setStringValue(\);
ctText.setSpace(SpaceAttribute.Space.PRESERVE);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.END);
r1 = codePara.createRun();
r1.setText(\页 总共\);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
r1 = codePara.createRun();
fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.BEGIN);
r1 = codePara.createRun();
ctText = r1.getCTR().addNewInstrText();
ctText.setStringValue(\);
ctText.setSpace(SpaceAttribute.Space.PRESERVE);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.END);
r1 = codePara.createRun();
r1.setText(\页\);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
setParagraphAlignInfo(codePara, ParagraphAlignment.CENTER,
TextAlignment.CENTER);
codePara.setBorderTop(Borders.THICK);
XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
newparagraphs[0] = codePara;
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(
document, sectPr);
headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, newparagraphs);
}
/**
* @Description: 页眉:显示时间信息
*/
public void simpleDateHeader(XWPFDocument document) throws Exception {
CTP ctp = CTP.Factory.newInstance();
for (int rowIndex = 0, rowLen = table.getNumberOfRows(); rowIndex < rowLen; rowIndex++) {
XWPFTableRow row = table.getRow(rowIndex);
List
for (int colIndex = 0, colLen = row.getTableCells().size(); colIndex < colLen; colIndex++) {
XWPFTableCell cell = row.getCell(colIndex);
CTTc ctTc = cell.getCTTc();
if (ctTc.isSetTcPr()) {
CTTcPr tcPr = ctTc.getTcPr();
if (tcPr.isSetHMerge()) {
CTHMerge hMerge = tcPr.getHMerge();
if (STMerge.RESTART.equals(hMerge.getVal())) {
cellContentList.add(getTableCellContent(cell));
}
} else if (tcPr.isSetVMerge()) {
CTVMerge vMerge = tcPr.getVMerge();
if (STMerge.RESTART.equals(vMerge.getVal())) {
cellContentList.add(getTableCellContent(cell));
}
} else {
cellContentList.add(getTableCellContent(cell));
}
}
}
tableContentList.add(cellContentList);
}
return tableContentList;
}
/**
* @Description: 得到表格内容,合并后的单元格视为一个单元格
*/
public List> getTableContent(XWPFTable table) {
List> tableContentList = new ArrayList
>();
for (int rowIndex = 0, rowLen = table.getNumberOfRows(); rowIndex < rowLen; rowIndex++) {
XWPFTableRow row = table.getRow(rowIndex);
List
for (int colIndex = 0, colLen = row.getTableCells().size(); colIndex < colLen; colIndex++) {
XWPFTableCell cell = row.getCell(colIndex);
cellContentList.add(getTableCellContent(cell));
}
tableContentList.add(cellContentList);
}
return tableContentList;
}
public String getTableCellContent(XWPFTableCell cell) {
StringBuffer sb = new StringBuffer();
List
if (cellPList != null && cellPList.size() > 0) {
for (XWPFParagraph xwpfPr : cellPList) {
List
if (runs != null && runs.size() > 0) {
for (XWPFRun xwpfRun : runs) {
sb.append(xwpfRun.getText(0));
}
}
}
}
return sb.toString();
}
/**
* @Description: 创建表格,创建后表格至少有1行1列,设置列宽
*/
public XWPFTable createTable(XWPFDocument xdoc, int rowSize, int cellSize,
boolean isSetColWidth, int[] colWidths) {
XWPFTable table = xdoc.createTable(rowSize, cellSize);
if (isSetColWidth) {
CTTbl ttbl = table.getCTTbl();
CTTblGrid tblGrid = ttbl.addNewTblGrid();
for (int j = 0, len = Math.min(cellSize, colWidths.length); j < len; j++) {
CTTblGridCol gridCol = tblGrid.addNewGridCol();
gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));
}
}
return table;
}
/**
* @Description: 设置表格总宽度与水平对齐方式
*/
public void setTableWidthAndHAlign(XWPFTable table, String width,
STJc.Enum enumValue) {
CTTblPr tblPr = getTableCTTblPr(table);
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr
.addNewTblW();
if (enumValue != null) {
CTJc cTJc = tblPr.addNewJc();
cTJc.setVal(enumValue);
}
tblWidth.setW(new BigInteger(width));
tblWidth.setType(STTblWidth.DXA);
}
XWPFParagraph codePara = new XWPFParagraph(ctp, document);
XWPFRun r1 = codePara.createRun();
CTFldChar fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.BEGIN);
r1 = codePara.createRun();
CTText ctText = r1.getCTR().addNewInstrText();
ctText.setStringValue(\);
ctText.setSpace(SpaceAttribute.Space.PRESERVE);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.END);
r1 = codePara.createRun();
r1.setText(\年\);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
r1 = codePara.createRun();
fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.BEGIN);
r1 = codePara.createRun();
ctText = r1.getCTR().addNewInstrText();
ctText.setStringValue(\);
ctText.setSpace(SpaceAttribute.Space.PRESERVE);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.END);
r1 = codePara.createRun();
r1.setText(\月\);
r1.setFontSize(11);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
r1 = codePara.createRun();
fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.BEGIN);
r1 = codePara.createRun();
ctText = r1.getCTR().addNewInstrText();
ctText.setStringValue(\);
ctText.setSpace(SpaceAttribute.Space.PRESERVE);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
fldChar = r1.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.END);
r1 = codePara.createRun();
r1.setText(\日\);
r1.setFontSize(11);
setParagraphRunFontInfo(codePara, r1, null, \微软雅黑\, \);
setParagraphAlignInfo(codePara, ParagraphAlignment.CENTER,
TextAlignment.CENTER);
codePara.setBorderBottom(Borders.THICK);
XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
newparagraphs[0] = codePara;
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(
document, sectPr);
headerFooterPolicy.createHeader(STHdrFtr.DEFAULT, newparagraphs);
}
/*------------------------------------Word 段落相关--------------------------------------------------- */
/**
* @Description: 得到段落CTPPr
*/
public CTPPr getParagraphCTPPr(XWPFParagraph p) {
CTPPr pPPr = null;
if (p.getCTP() != null) {
if (p.getCTP().getPPr() != null) {
pPPr = p.getCTP().getPPr();
} else {
pPPr = p.getCTP().addNewPPr();
}
}
return pPPr;
}
/**
* @Description: 得到XWPFRun的CTRPr
*/
public CTRPr getRunCTRPr(XWPFParagraph p, XWPFRun pRun) {
CTRPr pRpr = null;
if (pRun.getCTR() != null) {
pRpr = pRun.getCTR().getRPr();
if (pRpr == null) {
pRpr = pRun.getCTR().addNewRPr();
}
} else {
pRpr = p.getCTP().addNewR().addNewRPr();
}
return pRpr;
}
public XWPFRun getOrAddParagraphFirstRun(XWPFParagraph p, boolean isInsert,
boolean isNewLine) {
XWPFRun pRun = null;
if (isInsert) {
pRun = p.createRun();
} else {
if (p.getRuns() != null && p.getRuns().size() > 0) {
pRun = p.getRuns().get(0);
} else {
pRun = p.createRun();
}
}
if (isNewLine) {
pRun.addBreak();
}
return pRun;
}
public void setParagraphTextFontInfo(XWPFParagraph p, boolean isInsert,
boolean isNewLine, String content, String fontFamily,
String fontSize) {
XWPFRun pRun = getOrAddParagraphFirstRun(p, isInsert, isNewLine);
setParagraphRunFontInfo(p, pRun, content, fontFamily, fontSize);
}
/**
* @Description 设置字体信息
*/
public void setParagraphRunFontInfo(XWPFParagraph p, XWPFRun pRun,
String content, String fontFamily, String fontSize) {
CTRPr pRpr = getRunCTRPr(p, pRun);
if (StringUtils.isNotBlank(content)) {
pRun.setText(content);
}
// 设置字体
CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr
.addNewRFonts();
fonts.setAscii(fontFamily);
fonts.setEastAsia(fontFamily);
fonts.setHAnsi(fontFamily);
// 设置字体大小
CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz();
sz.setVal(new BigInteger(fontSize));
CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr
.addNewSzCs();
szCs.setVal(new BigInteger(fontSize));
}
/**
* @Description: 设置段落基本样式
*/
public void setParagraphTextBasicStyleInfo(XWPFParagraph p, XWPFRun pRun,
String colorVal, boolean isBlod, boolean isUnderLine,
String underLineColor, STUnderline.Enum underStyle,
boolean isItalic, boolean isStrike, boolean isHightLight,
STHighlightColor.Enum hightStyle, boolean isShd,
STShd.Enum shdStyle, String shdColor) {
setParagraphTextStyleInfo(p, pRun, colorVal, isBlod, isUnderLine,
underLineColor, underStyle, isItalic, isStrike, false, false,
false, false, false, false, false, null, isHightLight,
hightStyle, isShd, shdStyle, shdColor, null, 0, 0, 0);
}
/**
* @Description: 设置段落文本样式(高亮与底纹显示效果不同)设置字符间距信息(CTSignedTwipsMeasure)
* @param verticalAlign
* : SUPERSCRIPT上标 SUBSCRIPT下标
* @param position
* :字符间距位置:>0提升 <0降低=磅值*2 如3磅=6
* @param spacingValue
* :字符间距间距 >0加宽 <0紧缩 =磅值*20 如2磅=40
* @param indent
* :字符间距缩进 <100 缩
*/
public void setParagraphTextSimpleStyleInfo(XWPFParagraph p, XWPFRun pRun,
String colorVal, boolean isBlod, boolean isUnderLine,
String underLineColor, STUnderline.Enum underStyle,
boolean isItalic, boolean isStrike, boolean isHightLight,
STHighlightColor.Enum hightStyle, boolean isShd,
STShd.Enum shdStyle, String shdColor, VerticalAlign verticalAlign,
int position, int spacingValue, int indent) {
setParagraphTextStyleInfo(p, pRun, colorVal, isBlod, isUnderLine,
underLineColor, underStyle, isItalic, isStrike, false, false,
false, false, false, false, false, null, isHightLight,
hightStyle, isShd, shdStyle, shdColor, verticalAlign, position,
spacingValue, indent);
}
/**
* @Description: 设置段落文本样式(高亮与底纹显示效果不同)设置字符间距信息(CTSignedTwipsMeasure)
* @param verticalAlign
* : SUPERSCRIPT上标 SUBSCRIPT下标
* @param position
* :字符间距位置:>0提升 <0降低=磅值*2 如3磅=6
* @param spacingValue
* :字符间距间距 >0加宽 <0紧缩 =磅值*20 如2磅=40
* @param indent
* :字符间距缩进 <100 缩
*/
public void setParagraphTextStyleInfo(XWPFParagraph p, XWPFRun pRun,
String colorVal, boolean isBlod, boolean isUnderLine,
String underLineColor, STUnderline.Enum underStyle,
boolean isItalic, boolean isStrike, boolean isDStrike,
boolean isShadow, boolean isVanish, boolean isEmboss,
boolean isImprint, boolean isOutline, boolean isEm,
STEm.Enum emType, boolean isHightLight,
STHighlightColor.Enum hightStyle, boolean isShd,
STShd.Enum shdStyle, String shdColor, VerticalAlign verticalAlign,
int position, int spacingValue, int indent) {
if (pRun == null) {
return;
}
CTRPr pRpr = getRunCTRPr(p, pRun);
if (colorVal != null) {
pRun.setColor(colorVal);
}
// 设置字体样式
// 加粗
if (isBlod) {
pRun.setBold(isBlod);
}
// 倾斜
if (isItalic) {
pRun.setItalic(isItalic);
}
// 删除线
if (isStrike) {
pRun.setStrike(isStrike);
}
// 双删除线
if (isDStrike) {
CTOnOff dsCtOnOff = pRpr.isSetDstrike() ? pRpr.getDstrike() : pRpr
.addNewDstrike();
dsCtOnOff.setVal(STOnOff.TRUE);
}
// 阴影
if (isShadow) {
CTOnOff shadowCtOnOff = pRpr.isSetShadow() ? pRpr.getShadow()
: pRpr.addNewShadow();
shadowCtOnOff.setVal(STOnOff.TRUE);
}
// 隐藏
if (isVanish) {
CTOnOff vanishCtOnOff = pRpr.isSetVanish() ? pRpr.getVanish()
: pRpr.addNewVanish();
vanishCtOnOff.setVal(STOnOff.TRUE);
}
// 阳文
if (isEmboss) {
CTOnOff embossCtOnOff = pRpr.isSetEmboss() ? pRpr.getEmboss()
: pRpr.addNewEmboss();
embossCtOnOff.setVal(STOnOff.TRUE);
}
// 阴文
if (isImprint) {
CTOnOff isImprintCtOnOff = pRpr.isSetImprint() ? pRpr.getImprint()
: pRpr.addNewImprint();
isImprintCtOnOff.setVal(STOnOff.TRUE);
}
// 空心
if (isOutline) {
CTOnOff isOutlineCtOnOff = pRpr.isSetOutline() ? pRpr.getOutline()
: pRpr.addNewOutline();
isOutlineCtOnOff.setVal(STOnOff.TRUE);
}
// 着重号
if (isEm) {
CTEm em = pRpr.isSetEm() ? pRpr.getEm() : pRpr.addNewEm();
em.setVal(emType);
}
// 设置下划线样式
if (isUnderLine) {
CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU();
if (underStyle != null) {
u.setVal(underStyle);
}
if (underLineColor != null) {
u.setColor(underLineColor);
}
}
// 设置突出显示文本
if (isHightLight) {
if (hightStyle != null) {
CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr
.getHighlight() : pRpr.addNewHighlight();
hightLight.setVal(hightStyle);
}
}
if (isShd) {
// 设置底纹
CTShd shd = pRpr.isSetShd() ? pRpr.getShd() : pRpr.addNewShd();
if (shdStyle != null) {
shd.setVal(shdStyle);
}
if (shdColor != null) {
shd.setColor(shdColor);
}
}
// 上标下标
if (verticalAlign != null) {
pRun.setSubscript(verticalAlign);
}
// 设置文本位置
pRun.setTextPosition(position);
if (spacingValue > 0) {
// 设置字符间距信息
CTSignedTwipsMeasure ctSTwipsMeasure = pRpr.isSetSpacing() ? pRpr
.getSpacing() : pRpr.addNewSpacing();
ctSTwipsMeasure
.setVal(new BigInteger(String.valueOf(spacingValue)));
}
if (indent > 0) {
CTTextScale paramCTTextScale = pRpr.isSetW() ? pRpr.getW() : pRpr
.addNewW();
paramCTTextScale.setVal(indent);
}
}
/**
* @Description: 设置段落底纹(对整段文字起作用)
*/
public void setParagraphShdStyle(XWPFParagraph p, boolean isShd,
STShd.Enum shdStyle, String shdColor) {
CTPPr pPpr = getParagraphCTPPr(p);
CTShd shd = pPpr.isSetShd() ? pPpr.getShd() : pPpr.addNewShd();
if (shdStyle != null) {
shd.setVal(shdStyle);
}
if (shdColor != null) {
shd.setColor(shdColor);
}
}
/**
* @Description: 设置段落间距信息,一行=100 一磅=20
*/
public void setParagraphSpacingInfo(XWPFParagraph p, boolean isSpace,
String before, String after, String beforeLines, String afterLines,
boolean isLine, String line, STLineSpacingRule.Enum lineValue) {
CTPPr pPPr = getParagraphCTPPr(p);
CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing()
: pPPr.addNewSpacing();
if (isSpace) {
// 段前磅数
if (before != null) {
pSpacing.setBefore(new BigInteger(before));
}
// 段后磅数
if (after != null) {
pSpacing.setAfter(new BigInteger(after));
}
// 段前行数
if (beforeLines != null) {
pSpacing.setBeforeLines(new BigInteger(beforeLines));
}
// 段后行数
if (afterLines != null) {
pSpacing.setAfterLines(new BigInteger(afterLines));
}
}
// 间距
if (isLine) {
if (line != null) {
pSpacing.setLine(new BigInteger(line));
}
if (lineValue != null) {
pSpacing.setLineRule(lineValue);
}
}
}
// 设置段落缩进信息 1厘米≈567
public void setParagraphIndInfo(XWPFParagraph p, String firstLine,
String firstLineChar, String hanging, String hangingChar,
String right, String rigthChar, String left, String leftChar) {
CTPPr pPPr = getParagraphCTPPr(p);
CTInd pInd = pPPr.getInd() != null ? pPPr.getInd() : pPPr.addNewInd();
if (firstLine != null) {
pInd.setFirstLine(new BigInteger(firstLine));
}
if (firstLineChar != null) {
pInd.setFirstLineChars(new BigInteger(firstLineChar));
}
if (hanging != null) {
pInd.setHanging(new BigInteger(hanging));
}
if (hangingChar != null) {
pInd.setHangingChars(new BigInteger(hangingChar));
}
if (left != null) {
pInd.setLeft(new BigInteger(left));
}
if (leftChar != null) {
pInd.setLeftChars(new BigInteger(leftChar));
}
if (right != null) {
pInd.setRight(new BigInteger(right));
}
if (rigthChar != null) {
pInd.setRightChars(new BigInteger(rigthChar));
}
}
// 设置段落边框
public void setParagraphBorders(XWPFParagraph p, Borders lborder,
Borders tBorders, Borders rBorders, Borders bBorders,
Borders btborders) {
if (lborder != null) {
p.setBorderLeft(lborder);
}
if (tBorders != null) {
p.setBorderTop(tBorders);
}
if (rBorders != null) {
p.setBorderRight(rBorders);
}
if (bBorders != null) {
p.setBorderBottom(bBorders);
}
if (btborders != null) {
p.setBorderBetween(btborders);
}
}
/**
* @Description: 设置段落对齐
*/
public void setParagraphAlignInfo(XWPFParagraph p,
ParagraphAlignment pAlign, TextAlignment valign) {
if (pAlign != null) {
p.setAlignment(pAlign);
}
if (valign != null) {
p.setVerticalAlignment(valign);
}
}
/*------------------------------------Word 表格相关--------------------------------------------------- */
/**
* @Description:删除指定位置的表格,被删除表格后的索引位置--
*/
public void deleteTableByIndex(XWPFDocument xdoc, int pos) {
Iterator
int eIndex = 0, tableIndex = -1;
while (bodyElement.hasNext()) {
IBodyElement element = bodyElement.next();
BodyElementType elementType = element.getElementType();
if (elementType == BodyElementType.TABLE) {
tableIndex++;
if (tableIndex == pos) {
break;
}
}
eIndex++;
}
xdoc.removeBodyElement(eIndex);
}
public XWPFTable getTableByIndex(XWPFDocument xdoc, int index) {
List
if (tablesList == null || index < 0 || index > tablesList.size()) {
return null;
}
return tablesList.get(index);
}
public List
return xdoc.getTables();
}
/**
* @Description: 得到表格内容(第一次跨行单元格视为一个,第二次跳过跨行合并的单元格)
*/
public List> getTableRContent(XWPFTable table) {
List> tableContentList = new ArrayList
>();
正在阅读:
《象吉通书》、《崇正辟谬永吉通书》择日12-06
延边朝鲜族自治州龙井市八年级上学期语文第8课《列夫&183;托尔斯泰》同步练习07-19
孝老爱亲道德模范事迹材料(多篇)03-08
网页设计心得体会08-14
畅游书世界作文500字06-17
北航计算机组成原理-实验报告 - 图文10-13
自考高级财务会计核算题05-03
桩基桩长及内力的计算 - 图文04-08
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 常用
- 操作
- 总结
- 方法
- word
- 2007
- poi
- 软件工程课设管理系统 - 详细设计说明书
- 1安全评价师模拟题 - 图文
- 6933 - 2605 - 1612 - 阜阳师范学院学生社团成立申请表(1) - 图文
- FLAC3D 实例命令流1
- 分数四则混合运算题库
- 电力系统分析试题及参考答案修改1
- 示波器的使用实验报告
- 挖掘机安全操作规程
- 关键岗位人员安全生产责任制和各工种的操作规程
- 防雷规范学习心得
- 中国劳动力供求状况现状与展望
- 教师师德师风学习心得体会2015
- 《操作系统》第4章教材习题解答
- 大连交通大学2013-2014学年本科学生先进集体、先进个人拟入选名单
- 七宝中学2014年5月高三数学理科模拟试题
- 2019年201X年1月预备党员思想汇报范文-word范文(2页)
- 语音信号采集与处理 - 图文
- 河大版小学信息技术六年级 - 图文
- 基于鲁棒水印的图像认证技术研究
- 材料力学习题(7)第十二章 哈工业大材料力学本科生试卷和课后题目