package com.isstech;
import java.io.IOException;
import java.math.BigInteger;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar;
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.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
public class POIUtils {
//标题中文
public static String TITLE_1 = "标题 1";
public static String TITLE_2 = "标题 2";
public static String TITLE_3 = "标题 3";
//大纲级别
public static int TITLE_LEVEL_1 = 1;
public static int TITLE_LEVEL_2 = 2;
public static int TITLE_LEVEL_3 = 3;
//字体大小
public static int FONT_SIZE_10 = 10;
public static int FONT_SIZE_12 = 12;
public static int FONT_SIZE_14 = 14;
public static int FONT_SIZE_20 = 20;
//银白色
public static String CELL_COLOR_SILVER = "C0C0C0";
//灰色
public static String CELL_COLOR_GRAY = "808080";
/**
* 设置标题
* @param docxDocument
* @param styleId
* @param text 标题名称
* @param fontSize 字体大小
*/
public static void setTitle(XWPFDocument document,String styleId,String text,int fontSize) {
//添加标题
XWPFParagraph titleParagraph = document.createParagraph();
titleParagraph.setStyle(styleId);
//标题居左
titleParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun titleParagraphRun = titleParagraph.createRun();
titleParagraphRun.setText(text);
titleParagraphRun.setFontSize(fontSize);
}
/**
* 设置word页眉信息
* @param document
* @param text
* @param fontSize
* @throws IOException
*/
public static void setHeaderParagraph(XWPFDocument document,String text,int fontSize) throws IOException {
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
//添加页眉
CTP ctpHeader = CTP.Factory.newInstance();
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
//设置为右对齐
headerParagraph.setAlignment(ParagraphAlignment.LEFT);
//设置页眉下边线
headerParagraph.setBorderBottom(Borders.THICK);
XWPFRun headerRun = headerParagraph.createRun();
headerRun.setText(text);
headerRun.setFontSize(fontSize);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
}
/**
* 设置word页脚信息 页脚显示页码信息
* @param document
* @param fontSize
*/
public static void setFooterParagraph(XWPFDocument document,int fontSize) throws IOException {
/*
* 页脚中间对齐
*/
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
XWPFFooter footer = headerFooterPolicy.createFooter(STHdrFtr.DEFAULT);
XWPFParagraph paragraph = footer.getParagraphArray(0);
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
run.setText("第");
run.setFontSize(fontSize);
run = paragraph.createRun();
CTFldChar fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
run = paragraph.createRun();
CTText ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("PAGE \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
run = paragraph.createRun();
run.setText("页 总共");
run.setFontSize(fontSize);
run = paragraph.createRun();
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
run = paragraph.createRun();
ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("NUMPAGES \\* MERGEFORMAT ");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
run = paragraph.createRun();
run.setText("页");
run.setFontSize(fontSize);
}
/**
* 设置标题及大纲级别
* @param docxDocument
* @param strStyleId 中文标题级别(如:标题 1)
* @param headingLevel 大纲级别
*/
public static void setTitleStytle(XWPFDocument document, String strStyleId, int headingLevel) {
CTStyle ctStyle = CTStyle.Factory.newInstance();
ctStyle.setStyleId(strStyleId);
//设置中文的标题级别
CTString styleName = CTString.Factory.newInstance();
styleName.setVal(strStyleId);
ctStyle.setName(styleName);
CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
//设置数字的标题级别
indentNumber.setVal(BigInteger.valueOf(headingLevel));
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
ctStyle.setQFormat(onoffnull);
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setOutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
XWPFStyles styles = document.createStyles();
style.setType(STStyleType.PARAGRAPH);
styles.addStyle(style);
}
/**
* 换行处理
* @param document
*/
public static void changeLine(XWPFDocument document) {
//换行
XWPFParagraph paragraph1 = document.createParagraph();
XWPFRun paragraphRun1 = paragraph1.createRun();
paragraphRun1.setText("\r");
}
/**
* 横向合并单元格
* @param table
* @param row
* @param fromCell
* @param toCell
*/
public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if (cellIndex ==