服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Java实现将word转换为html的方法示例【doc与docx格式】

Java实现将word转换为html的方法示例【doc与docx格式】

2021-07-19 09:11只为学习与记录 Java教程

这篇文章主要介绍了Java实现将word转换为html的方法,结合实例形式分析了java针对doc与docx格式文件的相关转换操作技巧,需要的朋友可以参考下

本文实例讲述了java实现将word转换为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个诊疗方案及临床路径/";
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");
 }
 
   }
}
/**
* 转换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对象
inputstream in = new fileinputstream(f);
xwpfdocument document = new xwpfdocument(in);
// ) 解析 xhtml配置 (这里设置iuriresolver来设置图片存放的目录)
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转换成xhtml
outputstream out = new fileoutputstream(new file(filepath + htmlname));
xhtmlconverter.getinstance().convert(document, out, options);
}
/**
* 转换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>

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/tangyaliang11/article/details/79007873

延伸 · 阅读

精彩推荐