国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java實現(xiàn)word文件轉(zhuǎn)html文件

java實現(xiàn)word文件轉(zhuǎn)html文件

2020-09-01 09:43littleFatty Java教程

這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)word文件轉(zhuǎn)html文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近在項目開發(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包

java實現(xiàn)word文件轉(zhuǎn)html文件

代碼

java" id="highlighter_800105">
?
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();
  }
}

文件目錄:

java實現(xiàn)word文件轉(zhuǎn)html文件

java實現(xiàn)word文件轉(zhuǎn)html文件

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四虎在线观看 | 国产成人精品一区二区三区四区 | av片在线观看 | 99精品电影| 曰韩一级鸥美一级 | 在线国产视频观看 | 午夜精品一区二区三区在线视频 | 亚洲欧洲tv | 日韩高清在线一区二区三区 | av一级久久 | 国产在线观看免费 | 国产精品成人国产乱一区 | 神马久久久久久久 | 国产中文字幕在线播放 | 国产成人黄色网址 | 日韩三级在线免费观看 | 99成人在线视频 | 99热这里有精品 | 欧美日韩中文在线观看 | 日韩中文字幕免费在线播放 | 女人夜夜春高潮爽av片 | 精品久久一区二区 | 欧美一级视频在线观看 | 色九九 | 国产精品久久天天躁 | www.色94色.com | 天天操一操 | 亚洲精品免费在线观看视频 | 国产这里只有精品 | 亚洲免费国产 | 久久久久久久久久久久免费 | 成人免费淫片aa视频免费 | 精品亚洲第一 | 中文字幕观看 | 亚洲a网 | 欧美日韩国产精品一区二区 | 91久久艹| 黄色影院| 九热精品 | 黄色视屏在线免费观看 | 久久99国产精一区二区三区 |