本文實(shí)例講述了java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法。分享給大家供大家參考,具體如下:
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
|
public static void main(string[] args) throws exception { string filepath = "c:/users/administrator/desktop/92個(gè)診療方案及臨床路徑/" ; file file = new file(filepath); file[] files = file.listfiles(); string name = null ; for (file file2 : files) { thread.sleep( 500 ); name = file2.getname().substring( 0 , file2.getname().lastindexof( "." )); system.out.println(file2.getname()); if (file2.getname().endswith( ".docx" ) || file2.getname().endswith( ".docx" )) { casehtm.docx(filepath ,file2.getname(),name + ".htm" ); } else { casehtm.dox(filepath ,file2.getname(),name + ".htm" ); } } } /** * 轉(zhuǎn)換docx * @param filepath * @param filename * @param htmlname * @throws exception */ public static void docx(string filepath ,string filename,string htmlname) throws exception{ final string file = filepath + filename; file f = new file(file); // ) 加載word文檔生成 xwpfdocument對(duì)象 inputstream in = new fileinputstream(f); xwpfdocument document = new xwpfdocument(in); // ) 解析 xhtml配置 (這里設(shè)置iuriresolver來設(shè)置圖片存放的目錄) file imagefolderfile = new file(filepath); xhtmloptions options = xhtmloptions.create().uriresolver( new fileuriresolver(imagefolderfile)); options.setextractor( new fileimageextractor(imagefolderfile)); options.setignorestylesifunused( false ); options.setfragment( true ); // ) 將 xwpfdocument轉(zhuǎn)換成xhtml outputstream out = new fileoutputstream( new file(filepath + htmlname)); xhtmlconverter.getinstance().convert(document, out, options); } /** * 轉(zhuǎn)換doc * @param filepath * @param filename * @param htmlname * @throws exception */ public static void dox(string filepath ,string filename,string htmlname) throws exception{ final string file = filepath + filename; inputstream input = new fileinputstream( new file(file)); hwpfdocument worddocument = new hwpfdocument(input); wordtohtmlconverter wordtohtmlconverter = new wordtohtmlconverter(documentbuilderfactory.newinstance().newdocumentbuilder().newdocument()); //解析word文檔 wordtohtmlconverter.processdocument(worddocument); document htmldocument = wordtohtmlconverter.getdocument(); file htmlfile = new file(filepath + htmlname); outputstream outstream = new fileoutputstream(htmlfile); domsource domsource = new domsource(htmldocument); streamresult streamresult = new streamresult(outstream); transformerfactory factory = transformerfactory.newinstance(); transformer serializer = factory.newtransformer(); serializer.setoutputproperty(outputkeys.encoding, "utf-8" ); serializer.setoutputproperty(outputkeys.indent, "yes" ); serializer.setoutputproperty(outputkeys.method, "html" ); serializer.transform(domsource, streamresult); outstream.close(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<dependency> <groupid>fr.opensagres.xdocreport</groupid> <artifactid>fr.opensagres.xdocreport.document</artifactid> <version> 1.0 . 5 </version> </dependency> <dependency> <groupid>fr.opensagres.xdocreport</groupid> <artifactid>org.apache.poi.xwpf.converter.xhtml</artifactid> <version> 1.0 . 5 </version> </dependency> <dependency> <groupid>org.apache.poi</groupid> <artifactid>poi</artifactid> <version> 3.12 </version> </dependency> <dependency> <groupid>org.apache.poi</groupid> <artifactid>poi-scratchpad</artifactid> <version> 3.12 </version> </dependency> |
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/tangyaliang11/article/details/79007873