poi操作word工具类
当前poi版本:3.17,升级版本写法略有差异
新版poi操作方法:
- 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【目录】
涉及到的单位转换 :
1磅 = 12700 EMUs 1英寸 = 914400 EMUs, 1cm = 360000 EMUs 1磅 = 20缇
涉及到的依赖包 :
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>4.0.0</version>
</dependency>
- 备注:
- 文中提到的 WordUtil_Common 是当前工具类的类名
- 其中插入环绕图片的方法若使用过多参数调节,导出的word排版可能会乱(解决方法依赖第三方付费jar来格式化,aspose.words),目前3+的版本没有过多的维护了(请体谅),但在新版(5.2.2)工具包中解决了这个问题,不需要第三方jar
文章目录
- - 设置表格宽度,并根据宽度自动均分各单元格宽度
- - 给页眉添加图片,作为整个word 的边框
- - 设置页脚
- - 获取页眉页脚策略对象
- - 设置图片环绕模式【文字上方/文字下方】
- - 设置页眉页脚距离顶端/底端距离
- - 获取页眉高度
- - 获取页脚高度
- - 设置样式
- - 设置样式【下划线】
- - 设置文段内文本的位置和字符间距
- - 段落设置 - 间距
- - 段落设置 - 缩进
- - 设置页边距
- - 设置纸张大小
- - 获取正文宽度
- - 获取正文高度
- - 设置表格边框,上下左右
- - 去除table所有边框
- - 设置表格位置偏移量
- - 设置单元格背景色
- - 设置单元格宽度
- - 设置单元格高度
- - 获取word纸张宽度
- - 获取word纸张高度
- - 单位转换方法
- - 写入response
- - 下载失败
- 设置表格宽度,并根据宽度自动均分各单元格宽度
/**
* 列宽自动分割
* @param tableWidth 表格宽度 磅
*/
public static void setTableWidth_pt(XWPFTable table, int tableWidth) {
CTTbl ctTbl = table.getCTTbl();
CTTblPr tblPr = ctTbl.getTblPr();
if(tblPr == null){
tblPr = ctTbl.addNewTblPr();
}
CTTblWidth comTableWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
comTableWidth.setType(STTblWidth.DXA);
comTableWidth.setW(BigInteger.valueOf(tableWidth * 20));
List<XWPFTableRow> rows2 = table.getRows();
for (XWPFTableRow xwpfTableRow : rows2) {
xwpfTableRow.setCantSplitRow(true);
}
}
- 给页眉添加图片,作为整个word 的边框
/**
* 设置图片页眉
* @param w h 图片宽高单位 磅
* @param positionH 图片位置 左右方向 正文左边界处为0 ,单位 磅
* @param positionV 图片位置 上下方向 正文上边界处为0, 单位 磅
*/
public static void createHeader_useImage(XWPFDocument document , String imgPath , int w , int h , int positionH , int positionV) throws Exception {
XWPFHeaderFooterPolicy headerFooterPolicy = getHeaderFooterPolicy(document);
XWPFHeader header = headerFooterPolicy.createHeader(STHdrFtr.DEFAULT);
XWPFParagraph paragraph = header.createParagraph();
setParagraphSpacing(paragraph , 0 , 0 , 1);
XWPFRun run = paragraph.createRun();
setStyle(run , "" , 5 , false , "" , null); //页眉设置为0,还有一个文段的距离,这是设置为最小字体
FileInputStream fileInputStream = new FileInputStream(new File(imgPath));
addPicture_Anchor(run , fileInputStream , XWPFDocument.PICTURE_TYPE_PNG , "" ,
w , h , positionH,positionV, true);
fileInputStream.close();
}
- 设置页脚
/**
* 设置页脚文段
*/
public static void createFooter_simpleNumber(XWPFDocument document, String version, String footTitle) {
XWPFHeaderFooterPolicy headerFooterPolicy = getHeaderFooterPolicy(document);
XWPFFooter footer = headerFooterPolicy.createFooter(STHdrFtr.DEFAULT);
XWPFParagraph paragraph = footer.createParagraph();
//段落左侧文本设置
paragraph.setAlignment(ParagraphAlignment.LEFT);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun run = paragraph.createRun();
WordUtil_Common.setStyle(run, "微软雅黑", 6, false, footTitle , "000000");
//生成日期和版本号
if(!StringUtil.isEmpty(version)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String format = sdf.format(new Date());
XWPFRun versionRun = paragraph.createRun();
WordUtil_Common.setStyle(versionRun, "微软雅黑", 6, false,
"生成日期: " + format + " 版本: " + version, "000000");
versionRun.addTab(); //也可以使用制表符控制格式,这里用tab定死格式
versionRun.addTab();
versionRun.addTab();
versionRun.setText(" ");
}
//页码,最右边
WordUtil_Common.setStyle(paragraph.createRun(), "微软雅黑", 8, false, "第" , "000000");
CTFldChar fldChar = paragraph.createRun().getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
XWPFRun numberRun = paragraph.createRun();
CTText ctText = numberRun.getCTR().addNewInstrText();
ctText.setStringValue("PAGE \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
WordUtil_Common.setStyle(numberRun, "微软雅黑", 8, false, null , "000000");
fldChar = paragraph.createRun().getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
WordUtil_Common.setStyle(paragraph.createRun(), "微软雅黑", 8, false, "页" , "000000");
}
- 获取页眉页脚策略对象
/**
* 获取页眉页脚策略对象(创建页眉页脚的对象)
*/
public static XWPFHeaderFooterPolicy getHeaderFooterPolicy (XWPFDocument document){
CTBody body = document.getDocument().getBody();
CTSectPr ctSectPr = body.isSetSectPr() ? body.getSectPr() : body.addNewSectPr();
return new XWPFHeaderFooterPolicy(document , ctSectPr);
}
- 设置图片环绕模式【文字上方/文字下方】
/**
* 添加图片[环绕模式:文字上方/下方]
* @Auther lu zhifeng
* @Date 2020/11/12 10:37 下午
* @param pictureData 图片数据
* @param pictureType 图片类型,参考 ({@link XWPFDocument#PICTURE_TYPE_PNG})
* @param filename 文件名
* @param width 图片宽 单位 磅
* @param height 图片高 单位 磅
* @param behind 环绕模式:true 在文字下方 , false 在文字上方
* @param positionH 图片位置 左右方向 正文左边界处为0/段落开始位置为0 ,单位 磅
* @param positionV 图片位置 上下方向 正文上边界处为0/段落开始位置为0 , 单位 磅
* @param backGColor 图片染色
*/
public static void addPicture_Anchor(XWPFRun run ,InputStream pictureData, int pictureType, String filename,
int width, int height ,int positionH , int positionV , boolean behind , String backGColor) throws IOException {
try {
//poi提供的添加图片的方法,环绕模式是嵌入式(行内模式)(猜测是两部分,一部分是数据存放,一部分是模式设置),这里先添加图片,以便下面数据获取
run.addPicture(pictureData , pictureType , filename , ptToEMUs(width) , ptToEMUs(height));
//获取刚才添加的图片数据
CTDrawing drawing = run.getCTR().getDrawingArray(0);
CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
long id = drawing.getInlineArray(0).getDocPr().getId(); //由于计算id的方法是私有的,这里调用不了,这里使用原来的id
//设置一个浮动模式,替换掉默认的行内模式
CTAnchor ctAnchor = drawing.addNewAnchor();
String xml = "<a:graphic xmlns:a=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" + "<a:graphicData uri=\"" + org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture.type.getName().getNamespaceURI() + "\">" + "<pic:pic xmlns:pic=\"" + org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture.type.getName().getNamespaceURI() + "\" />" + "</a:graphicData>" + "</a:graphic>";
InputSource is = new InputSource(new StringReader(xml));
Document doc = DocumentHelper.readDocument(is);
ctAnchor.set(org.apache.xmlbeans.XmlToken.Factory.parse(doc.getDocumentElement(), POIXMLTypeLoader.DEFAULT_XML_OPTIONS));
ctAnchor.setDistT(0L);
ctAnchor.setDistR(0L);
ctAnchor.setDistB(0L);
ctAnchor.setDistL(0L);
ctAnchor.setBehindDoc(behind);
ctAnchor.addNewPositionH().setPosOffset(ptToEMUs(positionH));
ctAnchor.addNewPositionV().setPosOffset(ptToEMUs(positionV));
CTNonVisualDrawingProps docPr = ctAnchor.addNewDocPr();
docPr.setId(id);
docPr.setName("Drawing " + id);
docPr.setDescr(filename);
CTPositiveSize2D extent = ctAnchor.addNewExtent();
extent.setCx(ptToEMUs(width));
extent.setCy(ptToEMUs(height));
ctAnchor.setGraphic(graphicalobject);
drawing.setAnchorArray(new CTAnchor[]{ctAnchor});//添加浮动属性
drawing.removeInline(0);//删除行内属性
//给图片添加颜色
if(!StringUtil.empty(backGColor)){
String xmlColor = "<a:duotone>\n" +
"<a:prstClr val=\"black\"/>\n" +
"<a:srgbClr val=\"" + backGColor + "\">\n" +
"</a:srgbClr>\n" +
"</a:duotone>\n" +
"</a:blip>";
String string = ctAnchor.getGraphic().getGraphicData().xmlText();
String toReplaceStr = string.replaceAll(".*(<a:blip.*?/>).*", "$1");
String targetReplace = toReplaceStr.replaceAll("/>", ">") + xmlColor;
String result = string.replaceAll(toReplaceStr, targetReplace);
XmlObject parse = XmlObject.Factory.parse(result);
ctAnchor.getGraphic().getGraphicData().set(parse);
}
} catch (XmlException | SAXException | InvalidFormatException var31) {
throw new IllegalStateException(var31);
}
}
- 设置页眉页脚距离顶端/底端距离
/**
* 设置页眉页脚缩进
* @param headerMar 页眉距离顶端距离 单位 磅
* @param footerMar 页脚距离底部距离 单位 磅
*/
public static void setHeaderFooterMar(XWPFDocument document , int headerMar , int footerMar){
//一磅 = 20 缇
CTBody body = document.getDocument().getBody();
CTSectPr sectPr = body.isSetSectPr() ? body.getSectPr() : body.addNewSectPr();
CTPageMar pgMar = sectPr.isSetPgMar() ? sectPr.getPgMar() : sectPr.addNewPgMar();
pgMar.setHeader(BigInteger.valueOf(headerMar * 20));
pgMar.setFooter(BigInteger.valueOf(footerMar * 20));
}
- 获取页眉高度
/**
* 获取页眉高度 磅
*/
public static int getHeaderHeight(XWPFDocument document){
try {
return document.getDocument().getBody().getSectPr().getPgMar().getHeader().intValue() / 20;
}catch (Exception e){
return 0;
}
}
- 获取页脚高度
/**
* 获取页脚高度 磅
*/
public static int getFooterHeight(XWPFDocument document){
try {
return document.getDocument().getBody().getSectPr().getPgMar().getFooter().intValue() / 20;
}catch (Exception e){
return 0;
}
}
- 设置样式
/**
* 设置样式
*/
public static void setStyle(XWPFRun run, String fontFamily, int fontSize, boolean bold, String text, String color) {
run.setBold(bold);
run.setFontFamily(fontFamily);
run.setFontSize(fontSize);
if(!StringUtil.isEmpty(text)){
run.setText(text);
}
run.setColor(StringUtil.isEmpty(color) ? "000000" : color); //颜色默认黑色
}
- 设置样式【下划线】
/**
* 设置样式
* @param underType 下划线类型 参考:({@link UnderlinePatterns#SINGLE})
* @Return
*/
public static void setStyle(XWPFRun run, String fontFamily, int fontSize, boolean bold, String text, String color , UnderlinePatterns underType){
setStyle(run , fontFamily , fontSize , bold , text , color);
run.setUnderline(underType);
}
- 设置文段内文本的位置和字符间距
/**
* 设置文段Run位置【对应word字体 - 高级 - 字符间距】
* 注意:设置run的高低都会是段落变高,即位置变化皆在段落内
* @param positionValue 【上下位置调整正负】 单位 磅
* @param spacingValue 【字符间距调整 宽窄-正负】单位 磅
*/
public static void setPosition_run(XWPFRun run , int positionValue , int spacingValue){
CTRPr rPr = run.getCTR().getRPr();
if(rPr == null){
rPr = run.getCTR().addNewRPr();
}
CTSignedHpsMeasure position = rPr.getPosition();
if(position == null){
rPr.addNewPosition().setVal(BigInteger.valueOf(positionValue * 2));
}else{
position.setVal(BigInteger.valueOf(positionValue * 2));
}
CTSignedTwipsMeasure spacing = rPr.getSpacing();
if(spacing == null){
rPr.addNewSpacing().setVal(BigInteger.valueOf(spacingValue * 20));
}else{
spacing.setVal(BigInteger.valueOf(spacingValue * 20));
}
}
- 段落设置 - 间距
/**
* 调整段落间距【对应word段落设置-间距设置】
* @param before 段前 磅
* @param after 段后 磅
* @param multiple 几倍行距
*/
public static void setParagraphSpacing(XWPFParagraph paragraph , int before , int 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(BigInteger.valueOf(before * 20));
spacing.setAfter(BigInteger.valueOf(after * 20));
spacing.setLine(BigInteger.valueOf((int) (240 * multiple)));
spacing.setLineRule(STLineSpacingRule.AUTO);
}
- 段落设置 - 缩进
/**
* 设置段落缩进
* @param left 左缩进值 磅
* @param right 右缩进值 磅
*/
public static void setParagraphInd(XWPFParagraph paragraph , int left , int right){
CTPPr ppr = paragraph.getCTP().getPPr();
if (ppr == null) {
ppr = paragraph.getCTP().addNewPPr();
}
CTInd ctInd = ppr.isSetInd() ? ppr.getInd() : ppr.addNewInd();
ctInd.setLeft(BigInteger.valueOf(left * 20));
ctInd.setRight(BigInteger.valueOf(right * 20));
}
- 设置页边距
/**
* 设置页边距 单位 磅
*/
public static void setPageMargin(XWPFDocument document, int left, int top, int right, int bottom) {
//一磅 = 20缇
CTBody body = document.getDocument().getBody();
CTSectPr sectPr = body.isSetSectPr() ? body.getSectPr() : body.addNewSectPr();
CTPageMar ctpagemar = sectPr.isSetPgMar() ? sectPr.getPgMar() : sectPr.addNewPgMar();
ctpagemar.setLeft(new BigInteger((left*20) + ""));
ctpagemar.setTop(new BigInteger((top * 20) + ""));
ctpagemar.setRight(new BigInteger((right * 20) + ""));
ctpagemar.setBottom(new BigInteger((bottom * 20) + ""));
}
- 设置纸张大小
/**
* 设置纸张大小
*/
public static void setPaperSize(XWPFDocument document , String wordConstants_paperSize){
String[] split = wordConstants_paperSize.split("-");
CTBody body = document.getDocument().getBody();
CTSectPr sectPr = body.isSetSectPr() ? body.getSectPr() : body.addNewSectPr();
CTPageSz pageSize = sectPr.addNewPgSz();
pageSize.setW(BigInteger.valueOf(Integer.parseInt(split[0]) * 20));
pageSize.setH(BigInteger.valueOf(Integer.parseInt(split[1]) * 20));
pageSize.setOrient(STPageOrientation.PORTRAIT);
}
- 获取正文宽度
/**
* 获取word正文宽度 单位 磅 【获取不到,未设置 返回0】
* */
public static int getPagebodyWidth(XWPFDocument docxDocument){
try {
//下面这个单位(缇)是英寸 * 1440 得到的, 一英寸 = 914400 EMUs
CTSectPr sectPr = docxDocument.getDocument().getBody().getSectPr();
if(sectPr != null){
int pageSize = sectPr.getPgSz().getW().intValue(); //纸张大小
int leftMar = sectPr.getPgMar().getLeft().intValue(); //左缩进
int rightMar = sectPr.getPgMar().getRight().intValue(); //右缩进
return (pageSize - leftMar - rightMar) / 20;
}
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
- 获取正文高度
/**
* 获取word正文高度 单位 磅 【获取不到返回0】
* */
public static int getPagebodyHeight(XWPFDocument docxDocument){
try {
//下面这个单位(缇)是英寸 * 1440 得到的, 一英寸 = 914400 EMUs
CTSectPr sectPr = docxDocument.getDocument().getBody().getSectPr();
if(sectPr != null){
int pageSize = sectPr.getPgSz().getH().intValue(); //纸张大小
int topMar = sectPr.getPgMar().getTop().intValue(); //上缩进
int bottomMar = sectPr.getPgMar().getBottom().intValue(); //下缩进
return (pageSize - topMar - bottomMar) / 20;
}
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
- 设置表格边框,上下左右
/**
* 设置表格边框 上边框
* @param type 边框线类型({@link STBorder#SINGLE})
* @param size 边框线大小
* @param space 内边距
* @param color 颜色
*/
public static void setTopBorder(XWPFTable table , STBorder.Enum type , int size , int space , String color){
CTTblPr tblPr = table.getCTTbl().getTblPr();
if(tblPr == null){
tblPr = table.getCTTbl().addNewTblPr();
}
CTTblBorders borders = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders();
CTBorder top = borders.isSetTop() ? borders.getTop() : borders.addNewTop();
top.setVal(type);
top.setSz(BigInteger.valueOf(size));
top.setSpace(BigInteger.valueOf(space));
top.setColor(color);
}
/**
* 设置表格边框 左边框
* @param type 边框线类型({@link STBorder#SINGLE})
* @param size 边框线大小
* @param space 内边距
* @param color 颜色
*/
public static void setLeftBorder(XWPFTable table, STBorder.Enum type , int size , int space , String color){
CTTblPr tblPr = table.getCTTbl().getTblPr();
if(tblPr == null){
tblPr = table.getCTTbl().addNewTblPr();
}
CTTblBorders borders = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders();
CTBorder left = borders.isSetLeft() ? borders.getLeft() : borders.addNewLeft();
left.setVal(type);
left.setSz(BigInteger.valueOf(size));
left.setSpace(BigInteger.valueOf(space));
left.setColor(color);
}
/**
* 设置表格边框 右边框
* @param type 边框线类型({@link STBorder#SINGLE})
* @param size 边框线大小
* @param space 内边距
* @param color 颜色
*/
public static void setRightBorder(XWPFTable table, STBorder.Enum type , int size , int space , String color){
CTTblPr tblPr = table.getCTTbl().getTblPr();
if(tblPr == null){
tblPr = table.getCTTbl().addNewTblPr();
}
CTTblBorders borders = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders();
CTBorder right = borders.isSetRight() ? borders.getRight() : borders.addNewRight();
right.setVal(type);
right.setSz(BigInteger.valueOf(size));
right.setSpace(BigInteger.valueOf(space));
right.setColor(color);
}
/**
* 设置表格边框 下边框
* @param type 边框线类型({@link STBorder#SINGLE})
* @param size 边框线大小
* @param space 内边距
* @param color 颜色
*/
public static void setBottomBorder(XWPFTable table, STBorder.Enum type , int size , int space , String color){
CTTblPr tblPr = table.getCTTbl().getTblPr();
if(tblPr == null){
tblPr = table.getCTTbl().addNewTblPr();
}
CTTblBorders borders = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders();
CTBorder bottom = borders.isSetBottom() ? borders.getBottom() : borders.addNewBottom();
bottom.setVal(type);
bottom.setSz(BigInteger.valueOf(size));
bottom.setSpace(BigInteger.valueOf(space));
bottom.setColor(color);
}
- 去除table所有边框
/**
* 去除table所有边框,
*/
public static void unsetTableBorder(XWPFTable table){
WordUtil_Common.setLeftBorder(table , STBorder.NONE , 0 , 0 ,"");
WordUtil_Common.setTopBorder(table , STBorder.NONE , 0 , 0 ,"");
WordUtil_Common.setRightBorder(table , STBorder.NONE , 0 , 0 ,"");
WordUtil_Common.setBottomBorder(table , STBorder.NONE , 0 , 0 ,"");
table.setInsideHBorder(XWPFTable.XWPFBorderType.SINGLE , 0 , 0 , ""); //单元格底边框
table.setInsideVBorder(XWPFTable.XWPFBorderType.NONE , 0 , 0 , ""); //单元格右边框
}
- 设置表格位置偏移量
/**
* 设置表格位置
* @param tblpX X方向右移的距离 磅
* @param tblpY Y方向下移的距离 磅
* # 用于单元格内部表格位置调整,正文表格未测
*/
public static void setTableSite(XWPFTable table , int tblpX , int tblpY){
CTTbl ctTbl = table.getCTTbl();
CTTblPr tblPr = ctTbl.getTblPr();
if(tblPr == null){
tblPr = ctTbl.addNewTblPr();
}
CTTblPPr ctTblPPr = tblPr.isSetTblpPr() ? tblPr.getTblpPr() : tblPr.addNewTblpPr();
ctTblPPr.setTblpX(BigInteger.valueOf(tblpX * 20));
ctTblPPr.setTblpY(BigInteger.valueOf(tblpY * 20));
}
- 设置单元格背景色
/**
* 设置单元格背景色。
*/
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 , int 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(w * 20));
}
- 设置单元格高度
/**设置表格行 的 高度 单位 磅 。 */
public static void setRowH(XWPFTableRow row , int h){
row.setHeight( h * 20);
}
- 获取word纸张宽度
/**
* 获取word纸张宽度 单位 磅 【获取不到返回0】
* */
public static int getPaperSizeW(XWPFDocument docxDocument){
try {
return docxDocument.getDocument().getBody().getSectPr().getPgSz().getW().intValue() / 20;
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
- 获取word纸张高度
/**
* 获取word纸张宽度 单位 磅 【获取不到返回0】
* */
public static int getPaperSizeH(XWPFDocument docxDocument){
try {
return docxDocument.getDocument().getBody().getSectPr().getPgSz().getH().intValue() / 20;
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
- 单位转换方法
/**
* 磅 转 EMUs
*/
public static int ptToEMUs (int pt){
return pt * 12700;
}
/**
* 缇 转 磅 一磅 = 20缇
*/
public static int tiTopt(int ti){
return ti / 20;
}
/**
* EMUs 转 磅
*/
public static int EMUsToPt(long EMUs){
return (int) (EMUs / 12700);
}
- 写入response
/**
* 写入response
* @param fileName 文件名
*/
public static void writeResponse(HttpServletResponse response , String fileName , XWPFDocument document) throws Exception {
if (response != null) {
response.setContentType("application/binary;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
ServletOutputStream out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8") + ".docx");
try {
document.write(out);
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
out.close();
}
document.close();
}
- 下载失败
/**下载失败*/
public static void downloadFail(HttpServletResponse response) throws Exception{
response.setContentType("application/binary;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
String str = "下载失败!";
out.write(str.getBytes());
out.flush();
out.close();
}
更多推荐
poi操作word常用操作方法,word工具包
发布评论