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 ==
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
aipgetway.zip (33个子文件)
aipgetway
.project 909B
WebContent
WEB-INF
lib
poi-ooxml-3.15.jar 1.28MB
poi-3.15.jar 2.46MB
xmlbeans-2.3.0.jar 2.54MB
ooxml-schemas-1.1.jar 14.15MB
web.xml 652B
index.html 305B
zhenzhao.html 310B
META-INF
MANIFEST.MF 39B
list.html 1021B
src
com
isstech
POIUtils.java 9KB
Html.java 872B
Organ.java 371B
Export.java 6KB
Resources.java 473B
GetUserInfo.java 943B
ExportInfo.java 2KB
Data.java 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 479B
.jsdtscope 567B
build
classes
com
isstech
POIUtils.class 12KB
Resources.class 899B
ExportInfo.class 2KB
Export.class 6KB
GetUserInfo.class 1KB
Html.class 1KB
Organ.class 720B
Data.class 2KB
.classpath 1KB
共 33 条
- 1
资源评论
- ym15733252018-12-03不是我想要的读取word中复杂的页眉页脚,这个是先自己设置简单的页眉页脚再读取,,,,,
limeng_zxj
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功