如何利用openoffice读取word文档,并输出在页面上(HTML)

2022-03-15 科技 111阅读
将Word转Html的原理是这样的:
1、客户上传Word文档到服务器
2、服务器调用OpenOffice程序打开上传的Word文档
3、OpenOffice将Word文档另存为Html格式
4、Over
至此可见,这要求服务器端安装OpenOffice软件,其实也可以是MSOffice,不过OpenOffice的优势是跨平台,你懂的。恩,说明一下,本文的测试基于MSWin7UltimateX64系统。
下面就是规规矩矩的实现。
1、下载OpenOffice,
2、下载Jodconverter这是一个开启OpenOffice进行格式转化的第三方jar包。
3、泡杯热茶,等待下载。

4、安装OpenOffice,安装结束后,调用cmd,启动OpenOffice的一项服务:C:\ProgramFiles(x86)\OpenOffice.org3\program>soffice-headless-accept="socket,port=8100;urp;"

5、打开eclipse
6、喝杯热茶,等待eclipse打开。
7、新建eclipse项目,导入Jodconverter/lib下得jar包。

*commons-io
*jodconverter
*juh
*jurt
*ridl
*slf4j-api
*slf4j-jdk14
*unoil
*xstream

8、Coding...

查看代码

packagecom.mzule.doc2html.util;

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.ConnectException;
importjava.util.Date;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;

importcom.artofsolving.jodconverter.DocumentConverter;
importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;


publicclassDoc2Html{

publicstaticvoidmain(String[]args){
System.out
.println(toHtmlString(newFile("C:/test/test.doc"),"C:/test"));
}


publicstaticFileconvert(FiledocFile,Stringfilepath){
//创建保存html的文件
FilehtmlFile=newFile(filepath+"/"+newDate().getTime()
+".html");
//创建Openoffice连接
OpenOfficeConnectioncon=newSocketOpenOfficeConnection(8100);
try{
//连接
con.connect();
}catch(ConnectExceptione){
System.out.println("获取OpenOffice连接失败...");
e.printStackTrace();
}
//创建转换器
DocumentConverterconverter=newOpenOfficeDocumentConverter(con);
//转换文档问html
converter.convert(docFile,htmlFile);
//关闭openoffice连接
con.disconnect();
returnhtmlFile;
}


publicstaticStringtoHtmlString(FiledocFile,Stringfilepath){
//转换word文档
FilehtmlFile=convert(docFile,filepath);
//获取html文件流
StringBufferhtmlSb=newStringBuffer();
try{
BufferedReaderbr=newBufferedReader(newInputStreamReader(
newFileInputStream(htmlFile)));
while(br.ready()){
htmlSb.append(br.readLine());
}
br.close();
//删除临时文件
htmlFile.delete();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
//HTML文件字符串
StringhtmlStr=htmlSb.toString();
//返回经过清洁的html文本
returnclearFormat(htmlStr,filepath);
}


protectedstaticStringclearFormat(StringhtmlStr,StringdocImgPath){
//获取body内容的正则
StringbodyReg="";
PatternbodyPattern=Pattern.compile(bodyReg);
MatcherbodyMatcher=bodyPattern.matcher(htmlStr);
if(bodyMatcher.find()){
//获取BODY内容,并转化BODY标签为DIV
htmlStr=bodyMatcher.group().replaceFirst("
.replaceAll("","");
}
//调整图片地址
htmlStr=htmlStr.replaceAll("+"/");
//把转换成保留样式
//content=content.replaceAll("(]*>.*?)(<&>)",
//"
");
//把转换成
并删除样式
htmlStr=htmlStr.replaceAll("(]*)(>.*?)(<&>)","");
//删除不需要的标签
htmlStr=htmlStr
.replaceAll(
"]*?>",
"");
//删除不需要的属性
htmlStr=htmlStr
.replaceAll(
"]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>",
"");
returnhtmlStr;
}

}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com