最近在項目開發(fā)中用戶提出要在電腦上沒有裝office時在瀏覽器中打開html">word文件,最后確定的邏輯:用戶選擇想要查看的文件,頁面js判斷文件是否為word。不是執(zhí)行下載,是后端根據(jù)word文件后綴訪問對應(yīng)轉(zhuǎn)換方法。文件已存在對應(yīng)html文件直接返回html文件地址,不存在先生成對應(yīng)html文件再返回地址。js直接通過open()打開新的頁簽,展示word文件內(nèi)容。新人一枚,如果代碼中存在錯誤或有更好的實現(xiàn)萬望指正!
相關(guān)jar包
代碼
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import javax.xml.transform.outputkeys; import javax.xml.transform.transformer; import javax.xml.transform.transformerexception; import javax.xml.transform.transformerfactory; import javax.xml.transform.dom.domsource; import javax.xml.transform.stream.streamresult; import org.apache.poi.hwpf.hwpfdocument; import org.apache.poi.hwpf.converter.picturesmanager; import org.apache.poi.hwpf.converter.wordtohtmlconverter; import org.apache.poi.hwpf.usermodel.picturetype; import org.apache.poi.xwpf.converter.core.basicuriresolver; import org.apache.poi.xwpf.converter.core.fileimageextractor; import org.apache.poi.xwpf.converter.core.fileuriresolver; import org.apache.poi.xwpf.converter.xhtml.xhtmlconverter; import org.apache.poi.xwpf.converter.xhtml.xhtmloptions; import org.apache.poi.xwpf.usermodel.xwpfdocument; import org.w3c.dom.document; /** * word 轉(zhuǎn)換成html 2017-2-27 */ public class wordtohtml { /** * 將word2003轉(zhuǎn)換為html文件 2017-2-27 * @param wordpath word文件路徑 * @param wordname word文件名稱無后綴 * @param suffix word文件后綴 * @throws ioexception * @throws transformerexception * @throws parserconfigurationexception */ public string word2003tohtml(string wordpath,string wordname,string suffix) throws ioexception, transformerexception, parserconfigurationexception { string htmlpath = wordpath + file.separator + wordname + "_show" + file.separator; string htmlname = wordname + ".html" ; final string imagepath = htmlpath + "image" + file.separator; //判斷html文件是否存在 file htmlfile = new file(htmlpath + htmlname); if (htmlfile.exists()){ return htmlfile.getabsolutepath(); } //原word文檔 final string file = wordpath + file.separator + wordname + suffix; inputstream input = new fileinputstream( new file(file)); hwpfdocument worddocument = new hwpfdocument(input); wordtohtmlconverter wordtohtmlconverter = new wordtohtmlconverter(documentbuilderfactory.newinstance().newdocumentbuilder().newdocument()); //設(shè)置圖片存放的位置 wordtohtmlconverter.setpicturesmanager( new picturesmanager() { public string savepicture( byte [] content, picturetype picturetype, string suggestedname, float widthinches, float heightinches) { file imgpath = new file(imagepath); if (!imgpath.exists()){ //圖片目錄不存在則創(chuàng)建 imgpath.mkdirs(); } file file = new file(imagepath + suggestedname); try { outputstream os = new fileoutputstream(file); os.write(content); os.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } //圖片在html文件上的路徑 相對路徑 return "image/" + suggestedname; } }); //解析word文檔 wordtohtmlconverter.processdocument(worddocument); document htmldocument = wordtohtmlconverter.getdocument(); //生成html文件上級文件夾 file folder = new file(htmlpath); if (!folder.exists()){ folder.mkdirs(); } //生成html文件地址 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(); return htmlfile.getabsolutepath(); } /** * 2007版本word轉(zhuǎn)換成html 2017-2-27 * @param wordpath word文件路徑 * @param wordname word文件名稱無后綴 * @param suffix word文件后綴 * @return * @throws ioexception */ public string word2007tohtml(string wordpath,string wordname,string suffix) throws ioexception { string htmlpath = wordpath + file.separator + wordname + "_show" + file.separator; string htmlname = wordname + ".html" ; string imagepath = htmlpath + "image" + file.separator; //判斷html文件是否存在 file htmlfile = new file(htmlpath + htmlname); if (htmlfile.exists()){ return htmlfile.getabsolutepath(); } //word文件 file wordfile = new file(wordpath + file.separator + wordname + suffix); // 1) 加載word文檔生成 xwpfdocument對象 inputstream in = new fileinputstream(wordfile); xwpfdocument document = new xwpfdocument(in); // 2) 解析 xhtml配置 (這里設(shè)置iuriresolver來設(shè)置圖片存放的目錄) file imgfolder = new file(imagepath); xhtmloptions options = xhtmloptions.create(); options.setextractor( new fileimageextractor(imgfolder)); //html中圖片的路徑 相對路徑 options.uriresolver( new basicuriresolver( "image" )); options.setignorestylesifunused( false ); options.setfragment( true ); // 3) 將 xwpfdocument轉(zhuǎn)換成xhtml //生成html文件上級文件夾 file folder = new file(htmlpath); if (!folder.exists()){ folder.mkdirs(); } outputstream out = new fileoutputstream(htmlfile); xhtmlconverter.getinstance().convert(document, out, options); return htmlfile.getabsolutepath(); } } |
文件目錄:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。