说明

使用 POI 解析 word,word 内容已调整好大纲级别等格式

Maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!-- 引入poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
@Override
public Iterable<Law> add(MultipartFile file, String type) {
InputStream is = null;
XWPFDocument doc = null;
OutputStream out = null;

int i=0;
try {
XWPFDocument createDoc = new XWPFDocument();

is = file.getInputStream();
doc = new XWPFDocument(is);
//获取段落
List<XWPFParagraph> paras=doc.getParagraphs();

List<LawDto> lawList = Lists.newArrayList();
LawDto law = null;
String lastLevel = null;
for (XWPFParagraph para : paras){
String styleID = para.getStyleID();
String content = para.getParagraphText();
// System.out.println("styleid:" + styleID);//段落级别
// System.out.println("text:" + content);//段落内容
if(!StringUtil.isNullOrEmpty(styleID)) {
switch (styleID) {
case Consts.WORKCONTENT_LEVEL_NAME: {
if(lastLevel != null) {
if(!styleID.equals(lastLevel)) {
//开始一个新的对象,上个对象结束
lawList.add(law);
law = new LawDto();
law.setId(IdUtil.fastSimpleUUID());
law.setLawtype(type);
law.setName(content);
} else {
law.setName(law.getName()+"\n"+content);
}
} else {
law = new LawDto();
law.setId(IdUtil.fastSimpleUUID());
law.setLawtype(type);
law.setName(content);
}
break;
}
case Consts.WORKCONTENT_LEVEL_INTRO: {
law.setIntro((law.getIntro()==null?"\n":law.getIntro())+content);
break;
}
case Consts.WORKCONTENT_LEVEL_ZHANG: {
law.addZhang(content);
break;
}
case Consts.WORKCONTENT_LEVEL_JIE: {
law.addTiao(content);
break;
}
case Consts.WORKCONTENT_LEVEL_TIAO: {
law.addTiao(content);
break;
}
case Consts.WORKCONTENT_LEVEL_KUAN: {
law.addKuan(content);
break;
}
case Consts.WORKCONTENT_LEVEL_XIANG: {
law.addXiang(content);
break;
}
case Consts.WORKCONTENT_LEVEL_MU: {
law.addMu(content);
break;
}
}
lastLevel = styleID;
}
}
if(law != null) {
lawList.add(law);
}

Iterable<Law> all = repo.findAll();
long count = StreamSupport.stream(all.spliterator(), false).count();
List<Law> convert = LawDto.convert(count, lawList);
return repo.saveAll(convert);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(null!=out){
out.close();
}
if(null!=is){
is.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
return null;
}