改造了一个word导出功能,用到了之前没用到过的冷门方法,我当时百度找方法的时候也是很绝望,就是很冷门,有的博客看到关键地方,还就要钱了,所以我要在这里记录一下,省的以后我忘了,重蹈覆辙,另外也是希望能够帮助到大家!
1.首先用XWPFDocument导出,需要在pom里先引入依赖,下边的依赖都是相关的,有没用到的,我也没管了,先导入,方法管用就行,最开始我在网上粘贴的依赖放进去,是有版本号报错的,百度说是版本太高,不兼容了,现在这个版本号是我挨个试出来的,如果粘过去你们那不管用,就换了试试别的版本号,另外还有一种特殊情况,比如说这个依赖我导入,但依然是报红的状态,但方法能引用到,也不妨碍启动,就很无奈,反正就多试试呗。
<!-- Hutool超级工具类 http://hutool.mydoc.io/ -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.3.2</version>
</dependency>
<!-- 操作Excel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 操作压缩文件 -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.6.4</version>
</dependency>
<!--导出Word相关依赖-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
<exclusions>
<exclusion>
<artifactId>xmlbeans</artifactId>
<groupId>org.apache.xmlbeans</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
<version>3.17</version>
</dependency>
2. 好,下边列举一下我用到的冷门方法
初始化doc ,设置上sectPr和pgSz,其实设置的这些属性像是html的标签,使用属性就要先设置属性,所以才在使用前先判断是否已经设置了属性
XWPFDocument doc = new XWPFDocument();
CTBody body = doc.getDocument().getBody();
if (!body.isSetSectPr()) {
body.addNewSectPr();
}
CTSectPr section = body.getSectPr();
if (!section.isSetPgSz()) {
section.addNewPgSz();
}
//设置分栏
if (!section.isSetCols()) {
section.addNewCols();
}
CTColumns cols = section.getCols();
//分栏间距
cols.setSpace(BigInteger.valueOf(3000));
//分几栏
cols.setNum(BigInteger.valueOf(2));
//将纸张设置为A3大小的
CTPageSz pageSize = section.getPgSz();
pageSize.setW(BigInteger.valueOf(23800));
pageSize.setH(BigInteger.valueOf(16840));
//横向 页面排版
pageSize.setOrient(STPageOrientation.PORTRAIT);
//下边标题 考试介绍 和考试时间总分的设置 都一类型的,初始化段落,然后各种属性设置
// 标题(段落)
XWPFParagraph title = doc.createParagraph();
title.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleRun = title.createRun();
titleRun.setText(exam.getName());
titleRun.setBold(true);
titleRun.setFontSize(35);
titleRun.addBreak();
// 考试介绍(段落)
XWPFParagraph desc = doc.createParagraph();
desc.setAlignment(ParagraphAlignment.LEFT);
XWPFRun descRun = desc.createRun();
descRun.setText(exam.getExamDesc());
descRun.setBold(true);
descRun.setFontSize(15);
descRun.addBreak();
// 考试时间和总分
XWPFParagraph score = doc.createParagraph();
score.setAlignment(ParagraphAlignment.LEFT);
XWPFRun scoreRun = score.createRun();
scoreRun.setText("考试时间:" + exam.getTotalTime() + " 分钟, 总分:" +
exam.getTotalScore());
scoreRun.setBold(true);
scoreRun.setFontSize(15);
scoreRun.addBreak();
return doc;
我的这些设置出来的效果是这样的

大家看吧,说不定呢就哪个方法用到了。

这篇博客介绍了如何使用XWPFDocument进行Word导出,特别强调了一些不常用的方法,如设置分栏、纸张大小和页面排版。作者在实践中遇到版本不兼容问题,通过尝试找到了合适的依赖版本,并提醒读者遇到类似问题时可以尝试更换版本。文章还分享了初始化doc并设置sectPr和pgSz等属性的步骤,展示了实际效果。
1万+

被折叠的 条评论
为什么被折叠?



