openoffice 是一个开源的办工软件;在web开发中可以用来进行文档类型转换,比如说:doc-->pdf;xls-->pdf doc-->html;xls-->html;支持*.pdf *.doc *.odt *.sxw *.rtf *.txt *.wiki 七种格式间的相互转换,其中,PDF格式的文件不能作为被转换的对象
2)进入DOS界面,进入到openoffice安装目录下的:program目录,并运行如下命令: soffice.exe -accept="socket,port=8100;urp;" 服务已经启动,可以在程序中进行调试了;
在JAVA中简单的转换如下:
File nowFile = file;
String fileName = nowFile.getName();
int i = fileName.indexOf(".");
int leg = fileName.length();
//获得传入文件的后缀名
String exName = (i > 0 ? (i + 1) == leg ? "" : fileName.substring(i, fileName.length()) : "");
//建立一个临时文件,用来保存目标文件;文件的名称和传进来的一样,类型为传入的类型
File toFile= new File(nowFile.getPath().replace(exName, "." + type));
try {
//连接openoffice 8100 端口和上面在DOS中启动服务设置的端口一致,这里还可以将openoffice放在别的机器上,通过IP和端口来连接,如果不填IP,则默认是localhost:
OpenOfficeConnection connection = new SocketOpenOfficeConnecti on( 8100); connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConver ter( connection);
//转换,传入源文件和目标文件;
converter.convert(nowFile, toFile);
connection.disconnect();
} catch (ConnectException e) {
e.printStackTrace();
}
//返回目标文件
return toFile;
}
1)在http://zh.openoffice.org/new/zh_cn/downloads.html下载OpenOffice.org最新版本,并安装到任意位置.
private File convertTo(File file, String type)
{
//new SocketOpenOfficeConnecti on("192.168.0.120", 8100);
//当openoffice服务是在远程机器上的时候要注意的是:nowFile必须在本地和远程都存在;不然会报错,原因未知;但转换后的内容则是按远程机器上的内容来的;被转换后的文件保存在远程服务器上;
调用如下:
假设本地D盘有一文件名为:test.doc;
File testFile = new File("d:\\test.doc");
File resultFile = convertTo(testFile,"pdf");
File resultHtmlFile = convertTo(testFile,"html");
这时候我们就获得了一个pdf文件,一个html文件;在D盘下可以看到;而且在程序中我们也得到了它:resultFile, resultHtmlFile;