怎样使用Java读取openoffice 文档转换文档

web如何直接打开一个excel文档并可以使用_百度知道1772人阅读
Java(41)
1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件
2.通过swfTools将pdf文件转换成swf格式的文件
3.通过FlexPaper文档组件在页面上进行展示
2.安装包下载
1.openoffice是Apache下的一个开放免费的文字处理软件
& &下载地址:
2.SWFTools是一组用来处理Flash的swf文件的工具包,我们使用它将pdf文件转成swf文件!
& &下载地址:
3.FlexPaper是一个开源轻量级的在浏览器上显示各种文档的组件
& &下载地址:
4.JODConverter一个Java的OpenDocument 文件转换器,在此我们只用到它的jar包
& &下载地址:
3.安装文件
1.将所下载的文件(JODConverter除外)进行安装,盘符可随自己设定!需要注意的是在openoffice安装完成后,当我们使用它时,需将它的服务打开。在次我们需要以命令的方式打开:
打开dos窗口,进入openoffice安装盘符,输入以下代码来启动服务:
soffice -headless -accept=&socket,host=127.0.0.1,port=8100;& -nofirststartwizard
注意最后一个命令前边的‘—’,可不要写错!服务起不来,项目可是继续不下去的哦.
官网启动服务截图如下:
本地截图:
3.开发过程
1.新建项目,将flexpaper 文件中的js文件夹(包含了flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,这三个js 文件主要是预览swf文件的插件)拷贝至网站根目录;将FlexPaperViewer.swf拷贝至网站根目录下(该文件主要是用在网页中播放swf文
件的播放器),目录结构如下图:
注:需创建upload文件夹
2.创建fileUpload.jsp
&%@ page language=&java& contentType=&text/ charset=UTF-8&
pageEncoding=&UTF-8&%&
&!DOCTYPE html PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN& &http://www.w3.org/TR/html4/loose.dtd&&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&title&文档在线预览系统&/title&
body {margin-top:100background:#font-family: Verdana, T}
a {color:#CE4614;}
#msg-box {color: #CE4614; font-size:0.9text-align:}
#msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20padding-bottom:10}
#msg-box .title {font-size:1.4font-weight:margin:0 0 30px 0;}
#msg-box .nav {margin-top:20}
&div id=&msg-box&&
&form name=&form1&
method=&post& enctype=&multipart/form-data& action=&docUploadConvertAction.jsp&&
&div class=&title&&
请上传要处理的文件,过程可能需要几分钟,请稍候片刻。
&input name=&file1& type=&file&&
&input type=&submit& name=&Submit& value=&上传&&
3.创建转换页docUploadConvertAction.jsp
&%@ page language=&java& contentType=&text/ charset=UTF-8& pageEncoding=&UTF-8&%&
&%@page import=&java.io.*&%&
&%@page import=&java.util.Enumeration&%&
&%@page import=&com.oreilly.servlet.MultipartRequest&%&
&%@page import=&com.oreilly.servlet.multipart.DefaultFileRenamePolicy&%&
&%@page import=&com.cectsims.util.DocConverter&%&
//文件上传采用cos组件上传,可更换为commons-fileupload上传,文件上传后,保存在upload文件夹
//获取文件上传路径
String saveDirectory =application.getRealPath(&/&)+&upload&;
//打印上传路径信息
System.out.println(saveDirectory);
//每个文件最大50m
int maxPostSize = 50 * 1024 * 1024 ;
//采用cos缺省的命名策略,重名后加1,2,3...如果不加dfp重名将覆盖
DefaultFileRenamePolicy dfp = new DefaultFileRenamePolicy();
//response的编码为&UTF-8&,同时采用缺省的文件名冲突解决策略,实现上传,如果不加dfp重名将覆盖
MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,&UTF-8&,dfp);
//MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,&UTF-8&);
//输出反馈信息
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
System.err.println(&ccc&);
String name = (String)files.nextElement();
File f = multi.getFile(name);
if(f!=null){
String fileName = multi.getFilesystemName(name);
//获取上传文件的扩展名
String extName=fileName.substring(fileName.lastIndexOf(&.&)+1);
//文件全路径
String lastFileName= saveDirectory+&\\& + fileN
//获取需要转换的文件名,将路径名中的'\'替换为'/'
String converfilename = saveDirectory.replaceAll(&\\\\&, &/&)+&/&+fileN
System.out.println(converfilename);
//调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法
DocConverter d = new DocConverter(converfilename);
//调用conver方法开始转换,先执行doc2pdf()将office文件转换为再执行pdf2swf()将pdf转换为
d.conver();
//调用getswfPath()方法,打印转换后的swf文件路径
System.out.println(d.getswfPath());
//生成swf相对路径,以便传递给flexpaper播放器
String swfpath = &upload&+d.getswfPath().substring(d.getswfPath().lastIndexOf(&/&));
System.out.println(swfpath);
//将相对路径放入sessio中保存
session.setAttribute(&swfpath&, swfpath);
out.println(&上传的文件:&+lastFileName);
out.println(&文件类型&+extName);
out.println(&&hr&&);
&!DOCTYPE html PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN& &http://www.w3.org/TR/html4/loose.dtd&&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&title&Insert title here&/title&
body {margin-top:100background:#font-family: Verdana, T}
a {color:#CE4614;}
#msg-box {color: #CE4614; font-size:0.9text-align:}
#msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20padding-bottom:10}
#msg-box .title {font-size:1.4font-weight:margin:0 0 30px 0;}
#msg-box .nav {margin-top:20}
&form name=&viewForm& id=&form_swf& action=&documentView.jsp& method=&POST&&
&input type='submit' value='预览' class='BUTTON SUBMIT'/&
4.创建查看页documentView.jsp
&%@ page language=&java& contentType=&text/ charset=UTF-8& pageEncoding=&UTF-8&%&
String swfFilePath=session.getAttribute(&swfpath&).toString();
&!DOCTYPE html PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN& &http://www.w3.org/TR/html4/loose.dtd&&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&script type=&text/javascript& src=&js/jquery.js&&&/script&
&script type=&text/javascript& src=&js/flexpaper_flash.js&&&/script&
&script type=&text/javascript& src=&js/flexpaper_flash_debug.js&&&/script&
&style type=&text/css& media=&screen&&
html, body
{ height:100%; }
body { margin:0; padding:0; overflow: }
#flashContent { display: }
&title&文档在线预览系统&/title&
&div style=&position:left:50top:10&&
&a id=&viewerPlaceHolder& style=&width:820height:650display:block&&&/a&
&script type=&text/javascript&&
var fp = new FlexPaperViewer(
'FlexPaperViewer',
'viewerPlaceHolder', { config : {
SwfFile : escape('&%=swfFilePath%&'),
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'SinglePage',
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'en_US'
5.创建转换类DocConverter.java
package com.cectsims.
import java.io.BufferedInputS
import java.io.F
import java.io.IOE
import java.io.InputS
import com.artofsolving.jodconverter.DocumentC
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeC
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeC
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentC
* doc docx格式转换
public class DocConverter {
private static final int environment = 1;// 环境 1:windows 2:linux
private String fileS// (只涉及pdf2swf路径问题)
private String outputPath = &&;// 输入路径 ,如果不设置就输出在默认的位置
private String fileN
private File pdfF
private File swfF
private File docF
public DocConverter(String fileString) {
ini(fileString);
* 重新设置file
* @param fileString
public void setFile(String fileString) {
ini(fileString);
* @param fileString
private void ini(String fileString) {
this.fileString = fileS
fileName = fileString.substring(0, fileString.lastIndexOf(&.&));
docFile = new File(fileString);
pdfFile = new File(fileName + &.pdf&);
swfFile = new File(fileName + &.swf&);
* @param file
private void doc2pdf() throws Exception {
if (docFile.exists()) {
if (!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
// close the connection
connection.disconnect();
System.out.println(&****pdf转换成功,PDF输出:& + pdfFile.getPath()+ &****&);
} catch (java.net.ConnectException e) {
e.printStackTrace();
System.out.println(&****swf转换器异常,openoffice服务未启动!****&);
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace();
System.out.println(&****swf转换器异常,读取转换文件失败****&);
} catch (Exception e) {
e.printStackTrace();
System.out.println(&****已经转换为pdf,不需要再进行转化****&);
System.out.println(&****swf转换器异常,需要转换的文档不存在,无法转换****&);
* 转换成 swf
@SuppressWarnings(&unused&)
private void pdf2swf() throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1) {// windows环境处理
Process p = r.exec(&D:/Program Files/SWFTools/pdf2swf.exe &+ pdfFile.getPath() + & -o &+ swfFile.getPath() + & -T 9&);
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println(&****swf转换成功,文件输出:&
+ swfFile.getPath() + &****&);
if (pdfFile.exists()) {
pdfFile.delete();
} catch (IOException e) {
e.printStackTrace();
} else if (environment == 2) {// linux环境处理
Process p = r.exec(&pdf2swf & + pdfFile.getPath()
+ & -o & + swfFile.getPath() + & -T 9&);
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println(&****swf转换成功,文件输出:&
+ swfFile.getPath() + &****&);
if (pdfFile.exists()) {
pdfFile.delete();
} catch (Exception e) {
e.printStackTrace();
System.out.println(&****pdf不存在,无法转换****&);
System.out.println(&****swf已经存在不需要转换****&);
static String loadStream(InputStream in) throws IOException {
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ((ptr = in.read()) != -1) {
buffer.append((char) ptr);
return buffer.toString();
* 转换主方法
@SuppressWarnings(&unused&)
public boolean conver() {
if (swfFile.exists()) {
System.out.println(&****swf转换器开始工作,该文件已经转换为swf****&);
if (environment == 1) {
System.out.println(&****swf转换器开始工作,当前设置运行环境windows****&);
System.out.println(&****swf转换器开始工作,当前设置运行环境linux****&);
doc2pdf();
pdf2swf();
} catch (Exception e) {
e.printStackTrace();
if (swfFile.exists()) {
* 返回文件路径
* @param s
public String getswfPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll(&\\\\&, &/&);
return tempS
return &&;
* 设置输出路径
public void setOutputPath(String outputPath) {
this.outputPath = outputP
if (!outputPath.equals(&&)) {
String realName = fileName.substring(fileName.lastIndexOf(&/&),
fileName.lastIndexOf(&.&));
if (outputPath.charAt(outputPath.length()) == '/') {
swfFile = new File(outputPath + realName + &.swf&);
swfFile = new File(outputPath + realName + &.swf&);
6.部署发布
启动tomcat,部署当前web应用
地址栏输入http://localhost:8080/ctcesims/documentUpload.jsp 如下图:
单击选择文件,选择您要上传的文档,然后单击上传,处理完成后,打印如下信息,如下图所示:
单击预览按钮,就会生成预览界面,如下图:
4.常见问题
若出现swf无法预览,请访问
将生成swf的文件夹设置为信任文件位置。
bug:预览txt文档,txt转pdf,会出现乱码!
针对该问题,网上有不少解决方案,在此只提供自己测试好的一种解决方案:先将txt转换成odt文件,再将odt文件转换为pdf
1.修改DocConverter.java类中的初始化方法
* @author lf
* @param fileString
private void ini(String fileString) {
this.fileString = fileS
fileName = fileString.substring(0, fileString.lastIndexOf(&/&));
docFile = new File(fileString);
String s = fileString.substring(fileString.lastIndexOf(&/&) + 1,fileString.lastIndexOf(&.&));
fileName = fileName + &/& +
// 用于处理TXT文档转化为PDF格式乱码,获取上传文件的名称(不需要后面的格式)
String txtName = fileString.substring(fileString.lastIndexOf(&.&));
// 判断上传的文件是否是TXT文件
if (txtName.equalsIgnoreCase(&.txt&)) {
// 定义相应的ODT格式文件名称
odtFile = new File(fileName + &.odt&);
// 将上传的文档重新copy一份,并且修改为ODT格式,然后有ODT格式转化为PDF格式
this.copyFile(docFile, odtFile);
pdfFile = new File(fileName + &.pdf&); // 用于处理PDF文档
} else if (txtName.equals(&.pdf&) || txtName.equals(&.PDF&)) {
pdfFile = new File(fileName + &.pdf&);
this.copyFile(docFile, pdfFile);
pdfFile = new File(fileName + &.pdf&);
swfFile = new File(fileName + &.swf&);
} catch (IOException e) {
e.printStackTrace();
ok!有问题请留言!
源码已上传,下载链接:
参考资料:
http://blog.csdn.net/iamduoluo/article/details/6727031
/star-studio/archive//2282411.html#2497889
本文转载自:/xpws/p/3623022.html
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:51914次
积分:1082
积分:1082
排名:千里之外
原创:51篇
转载:59篇
(2)(2)(7)(2)(2)(10)(10)(3)(7)(30)(25)(6)(4)(1)(2)为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl+V。看起来也不麻烦,但是,如果文档中包含大量图片呢?尴尬的事是图片都需要重新上传吧?
如果可以将已经编写好的word文档上传到服务器就可以在相应页面进行展示,将会是一件非常惬意的事情,最起码信息发布人员会很开心。程序员可能就不会这么想了,囧。
将Word转Html的原理是这样的:
1、客户上传Word文档到服务器
2、服务器调用OpenOffice程序打开上传的Word文档
3、OpenOffice将Word文档另存为Html格式
至此可见,这要求服务器端安装OpenOffice软件,其实也可以是MS Office,不过OpenOffice的优势是跨平台,你懂的。恩,说明一下,本文的测试基于 MS Win7 Ultimate X64 系统。
下面就是规规矩矩的实现。
1、下载OpenOffice,&So easy...
2、下载Jodconverter &这是一个开启OpenOffice进行格式转化的第三方jar包。
3、泡杯热茶,等待下载。
4、安装OpenOffice,安装结束后,调用cmd,启动OpenOffice的一项服务:C:\Program Files (x86)\OpenOffice.org 3\program&soffice -headless -accept="socket,port=8100;"
5、打开eclipse
6、喝杯热茶,等待eclipse打开。
7、新建eclipse项目,导入Jodconverter/lib 下得jar包。
8、Coding...
package com.mzule.doc2html.import java.io.BufferedRimport java.io.Fimport java.io.FileInputSimport java.io.FileNotFoundEimport java.io.IOEimport java.io.InputStreamRimport java.net.ConnectEimport java.util.Dimport java.util.regex.Mimport java.util.regex.Pimport com.artofsolving.jodconverter.DocumentCimport com.artofsolving.jodconverter.openoffice.connection.OpenOfficeCimport com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeCimport com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentC/** * 将Word文档转换成html字符串的工具类 *
* @author MZULE *
*/public class Doc2Html {
public static void main(String[] args) {
System.out
.println(toHtmlString(new File("C:/test/test.doc"), "C:/test"));
* 将word文档转换成html文档
* @param docFile
需要转换的word文档
* @param filepath
转换之后html的存放路径
* @return 转换之后的html文件
public static File convert(File docFile, String filepath) {
// 创建保存html的文件
File htmlFile = new File(filepath + "/" + new Date().getTime()
+ ".html");
// 创建Openoffice连接
OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);
con.connect();
} catch (ConnectException e) {
System.out.println("获取OpenOffice连接失败...");
e.printStackTrace();
// 创建转换器
DocumentConverter converter = new OpenOfficeDocumentConverter(con);
// 转换文档问html
converter.convert(docFile, htmlFile);
// 关闭openoffice连接
con.disconnect();
return htmlF
* 将word转换成html文件,并且获取html文件代码。
* @param docFile
需要转换的文档
* @param filepath
文档中图片的保存位置
* @return 转换成功的html代码
public static String toHtmlString(File docFile, String filepath) {
// 转换word文档
File htmlFile = convert(docFile, filepath);
// 获取html文件流
StringBuffer htmlSb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(htmlFile)));
while (br.ready()) {
htmlSb.append(br.readLine());
br.close();
// 删除临时文件
htmlFile.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
// HTML文件字符串
String htmlStr = htmlSb.toString();
// 返回经过清洁的html文本
return clearFormat(htmlStr, filepath);
* 清除一些不需要的html标记
* @param htmlStr
带有复杂html标记的html语句
* @return 去除了不需要html标记的语句
protected static String clearFormat(String htmlStr, String docImgPath) {
// 获取body内容的正则
String bodyReg = "&BODY .*&/BODY&";
Pattern bodyPattern = pile(bodyReg);
Matcher bodyMatcher = bodyPattern.matcher(htmlStr);
if (bodyMatcher.find()) {
// 获取BODY内容,并转化BODY标签为DIV
htmlStr = bodyMatcher.group().replaceFirst("&BODY", "&DIV")
.replaceAll("&/BODY&", "&/DIV&");
// 调整图片地址
htmlStr = htmlStr.replaceAll("&IMG SRC=\"", "&IMG SRC=\"" + docImgPath
// 把&P&&/P&转换成&/div&&/div&保留样式
// content = content.replaceAll("(&P)([^&]*&.*?)(&\\/P&)",
// "&div$2&/div&");
// 把&P&&/P&转换成&/div&&/div&并删除样式
htmlStr = htmlStr.replaceAll("(&P)([^&]*)(&.*?)(&\\/P&)", "&p$3&/p&");
// 删除不需要的标签
htmlStr = htmlStr
.replaceAll(
"&[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^&]*?&",
// 删除不需要的属性
htmlStr = htmlStr
.replaceAll(
"&([^&]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^&]+)([^&]*)&",
"&$1$2&");
return htmlS
类组织的不好,博友凑合看,代码注释比较详细了,不多说。
两个公开的方法是独立使用的,toHtmlString(...)方法是转化文件并获取html代码,以备存入数据库。
参考了,表示感谢。
阅读(...) 评论()20497人阅读
J2EE(34)
develop(6)
&OpenOffice.org 是一套跨平台的办公室软件套件,能在 Windows、Linux、MacOS X (X11)、和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用、及推广它。
OpenOffice org 的 API 以 UNO (UniversalNetwork Object) 写成,所以本身是电脑语言中立的。现在来说,OpenOffice org主要是以 C++ 撰写的,但也能以 Java(TM) 来撰写。
1. 需要用的软件
& &&OpenOffice 下载地址
& &&JodConverter 下载地址,也可以直接从附件里面下载
2.启动OpenOffice的服务
& & 我到网上查如何利用OpenOffice进行转码的时候,都是需要先用cmd启动一个soffice服务,启动的命令是:soffice -headless -accept=&socket,host=127.0.0.1,port=8100;&。
& & 但是实际上,对于我的项目,进行转码只是偶尔进行,然而当OpenOffice的转码服务启动以后,该进程(进程名称是soffice.exe)会一直存在,并且大约占100M的内存,感觉非常浪费。于是我就想了一个办法,可以将执行该服务的命令直接在JAVA代码里面调用,然后当转码完成的时候,直接干掉这个进程。在后面的JAVA代码里面会有解释。
& &&所以,实际上,这第2步可以直接跳过
3.将JodConverter相关的jar包添加到项目中
& & 将JodConverter解压缩以后,把lib下面的jar包全部添加到项目中
4. 下面就是重点喽,详见Java代码解析
附件里面有现成的可以用的项目示例,直接导入eclipse就可以运行
&&&&&*&将Office文档转换为PDF.&运行该函数需要用到OpenOffice,&OpenOffice下载地址为&
&&&&&*&http://www.openoffice.org/&
&&&&&*&&pre&&
&&&&&*&方法示例:&
&&&&&*&String&sourcePath&=&&F:\\office\\source.doc&;&
&&&&&*&String&destFile&=&&F:\\pdf\\dest.pdf&;&
&&&&&*&Converter.office2PDF(sourcePath,&destFile);&
&&&&&*&&/pre&&
&&&&&*&@param&sourceFile&
&&&&&*&&&&&&&&&&&&源文件,&绝对路径.&可以是Office全部格式的文档,&Office2010的没测试.&包括.doc,&
&&&&&*&&&&&&&&&&&&.docx,&.xls,&.xlsx,&.ppt,&.pptx等.&示例:&F:\\office\\source.doc&
&&&&&*&@param&destFile&
&&&&&*&&&&&&&&&&&&目标文件.&绝对路径.&示例:&F:\\pdf\\dest.pdf&
&&&&&*&@return&操作成功与否的提示信息.&如果返回&-1,&表示找不到源文件,&或url.properties配置错误;&如果返回&0,&
&&&&&*&&&&&&&&&则表示操作成功;&返回1,&则表示转换失败&
&&&&public&static&int&office2PDF(String&sourceFile,&String&destFile)&{&&
&&&&&&&&try&{&&
&&&&&&&&&&&&File&inputFile&=&new&File(sourceFile);&&
&&&&&&&&&&&&if&(!inputFile.exists())&{&&
&&&&&&&&&&&&&&&&return&-1;//&找不到源文件,&则返回-1&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&//&如果目标路径不存在,&则新建该路径&&
&&&&&&&&&&&&File&outputFile&=&new&File(destFile);&&
&&&&&&&&&&&&if&(!outputFile.getParentFile().exists())&{&&
&&&&&&&&&&&&&&&&outputFile.getParentFile().mkdirs();&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&String&OpenOffice_HOME&=&&D:\\Program&Files\\OpenOffice.org&3&;//这里是OpenOffice的安装目录,&在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是绝对没问题的&&
&&&&&&&&&&&&//&如果从文件中读取的URL地址最后一个字符不是&'\',则添加'\'&&
&&&&&&&&&&&&if&(OpenOffice_HOME.charAt(OpenOffice_HOME.length()&-&1)&!=&'\\')&{&&
&&&&&&&&&&&&&&&&OpenOffice_HOME&+=&&\\&;&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&//&启动OpenOffice的服务&&
&&&&&&&&&&&&String&command&=&OpenOffice_HOME&&
&&&&&&&&&&&&&&&&&&&&+&&program\\soffice.exe&-headless&-accept=\&socket,host=127.0.0.1,port=8100;\&&;&&
&&&&&&&&&&&&Process&pro&=&Runtime.getRuntime().exec(command);&&
&&&&&&&&&&&&//&connect&to&an&OpenOffice.org&instance&running&on&port&8100&&
&&&&&&&&&&&&OpenOfficeConnection&connection&=&new&SocketOpenOfficeConnection(&&
&&&&&&&&&&&&&&&&&&&&&127.0.0.1&,&8100);&&
&&&&&&&&&&&&connection.connect();&&
&&&&&&&&&&&&//&convert&&
&&&&&&&&&&&&DocumentConverter&converter&=&new&OpenOfficeDocumentConverter(&&
&&&&&&&&&&&&&&&&&&&&connection);&&
&&&&&&&&&&&&converter.convert(inputFile,&outputFile);&&
&&&&&&&&&&&&//&close&the&connection&&
&&&&&&&&&&&&connection.disconnect();&&
&&&&&&&&&&&&//&关闭OpenOffice服务的进程&&
&&&&&&&&&&&&pro.destroy();&&
&&&&&&&&&&&&return&0;&&
&&&&&&&&}&catch&(FileNotFoundException&e)&{&&
&&&&&&&&&&&&e.printStackTrace();&&
&&&&&&&&&&&&return&-1;&&
&&&&&&&&}&catch&(ConnectException&e)&{&&
&&&&&&&&&&&&e.printStackTrace();&&
&&&&&&&&}&catch&(IOException&e)&{&&
&&&&&&&&&&&&e.printStackTrace();&&
&&&&&&&&}&&
&&&&&&&&return&1;&&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:509990次
积分:5834
积分:5834
排名:第3012名
原创:124篇
转载:119篇
评论:90条
(1)(3)(1)(2)(1)(10)(1)(1)(13)(2)(9)(25)(13)(3)(3)(5)(2)(1)(14)(12)(15)(18)(13)(4)(20)(34)(14)(1)(2)

我要回帖

更多关于 openoffice帮助文档 的文章

 

随机推荐