相信大家在工作学习中遇到一些奇奇怪怪的任务,比如什么解析word文档并存入数据库,也就是直接读取word录入数据。
开发环境
- JDK8
- Springboot2.3
- Mybatis-plus
- Maven
运用工具
经百度发现,可以操作word的工具包有很多,各有利弊。这里主要讲用到的是Apache提供的POI,提供了一系列的APi对Microsoft Office格式档案读和写的功能。话不多说直接开始。
1.导入依赖
<!-- POI-word文件处理需要 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
2.主要方法介绍
getParagraphs()
该方法返回所有段落的List,List其中的对象是段落paragraph对象
getParagraphText()
该方法返回段落对象paragraph对象的文本内容,也就是paragraph.getParagraphText()
思路整理:
1.通过file对象生成对应的XWPFDocument对象或者是WordExtractor对象。
XWPFDocument对应的是2007及以后版本的word,也就是以.docx结尾的word文档
WordExtractor对应的是2003版本的word,也就是以.doc结尾的word文档
2.获取所有段落对象的List,然后遍历拿到每个段落的文本内容
注意:以上内容只可获取段落中的文本内容,不包括图片或者其他的东西!!!
3.直接上工具类,一看就懂了
/**
* 获取正文文件内容,docx方法
*
* @param file
* @return
*/
public static Map<String, String> getContentDocx(File file) {
Map<String, String> map = new HashMap();
StringBuffer content = new StringBuffer("");
String result = "0"; // 0表示获取正常,1表示获取异常
InputStream is = null;
Logger logger = null;
try {
//根据需求入参也可以改为文件路径,对应的输入流部分改为new File(路径)即可
is = new FileInputStream(file);
// 2007版本的word
XWPFDocument xwpf = new XWPFDocument(is); // 2007版本,仅支持docx文件处理
List<XWPFParagraph> paragraphs = xwpf.getParagraphs();
if (paragraphs != null && paragraphs.size() > 0) {
for (XWPFParagraph paragraph : paragraphs) {
if (!paragraph.getParagraphText().startsWith(" ")) {
content.append(paragraph.getParagraphText().trim()).append("\r\n");
} else {
content.append(paragraph.getParagraphText());
}
}
}
} catch (Exception e) {
logger.error("docx解析正文异常:" + e);
result = "1"; // 出现异常
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
logger.error("" + e);
}
}
map.put("result", result);
map.put("content", String.valueOf(content));
}
return map;
}
/**
* 获取正文文件内容,doc方法
*
* @param path
* @return
*/
public static Map<String, String> getContentDoc(String path) {
Map<String, String> map = new HashMap();
StringBuffer content = new StringBuffer("");
String result = "0"; // 0表示获取正常,1表示获取异常
InputStream is = null;
Logger logger = null;
try {
is = new FileInputStream(new File(path));
// 2003版本的word
WordExtractor extractor = new WordExtractor(is); // 2003版本 仅doc格式文件可处理,docx文件不可处理
String[] paragraphText = extractor.getParagraphText(); // 获取段落,段落缩进无法获取,可以在前添加空格填充
if (paragraphText != null && paragraphText.length > 0) {
for (String paragraph : paragraphText) {
if (!paragraph.startsWith(" ")) {
content.append(paragraph.trim()).append("\r\n");
} else {
content.append(paragraph);
}
}
}
} catch (Exception e) {
logger.error("doc解析正文异常:" + e);
result = "1"; // 出现异常
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
logger.error("" + e);
}
}
map.put("result", result);
map.put("content", content.toString());
}
return map;
}
/**
* 获取正文文件内容,wps方法
*
* @param path
* @return
*/
public static Map<String, String> getContentWps(String path) {
Map<String, String> map = new HashMap();
StringBuffer content = new StringBuffer("");
String result = "0"; // 0表示获取正常,1表示获取异常
InputStream is = null;
Logger logger = null;
try {
is = new FileInputStream(new File(path));
// wps版本word
HWPFDocument hwpf = new HWPFDocument(is);
WordExtractor wordExtractor = new WordExtractor(hwpf);
// 文档文本内容
String[] paragraphText1 = wordExtractor.getParagraphText();
if (paragraphText1 != null && paragraphText1.length > 0) {
for (String paragraph : paragraphText1) {
if (!paragraph.startsWith(" ")) {
content.append(paragraph.trim()).append("\r\n");
} else {
content.append(paragraph);
}
}
}
} catch (Exception e) {
logger.error("wps解析正文异常:" + e);
result = "1"; // 出现异常
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
logger.error("" + e);
}
}
map.put("result", result);
map.put("content", content.toString());
}
return map;
}