ä½¿ç¨ java æä½ openoffice å®ç°æ ¼å¼è½¬æ¢ è¾è¦äºå 天ã ä¸æ¢ç¬äº«ææ
é¦å
ï¼è¦å®è£
openoffice ï¼åºè¯- -ï¼
å建JAVA项ç®çæ¶å éè¦è³å°ä»¥ä¸4个å
juh.jar,jurt.jar,ridl.jar,unoil.jar
以ä¸æ¯è·¯å¾
..\OpenOffice.org 2.3\program\classes
æåªå®ç°äºæè¦çåè½ï¼å°±æ¯æä¸ç§æä»¶æ ¼å¼è½¬æå¦ä¸ç§
Java代ç
package testh;
import java.io.*;
import com.sun.star.uno.UnoRuntime;
public class testcls {
public static void readdoc(String paths, String savepaths)
{
File d = new File(paths);
//åå¾å½åæ件夹ä¸æææ件åç®å½çå表
File lists[] = d.listFiles();
String pathss = new String("");
//对å½åç®å½ä¸é¢æææ件è¿è¡æ£ç´¢
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
//åå¾æ件类å
filetype = filename.substring((filename.length() - 3), filename.length());
//å¤ææ¯å¦ä¸ºdocæ件
if(filetype.equals("doc"))
{
System.out.println("å½åæ£å¨æ£ç´¢....");
//æå°å½åç®å½è·¯å¾
System.out.println(paths);
//æå°docæ件å
String fname=filename.substring(0, (filename.length() - 4));
System.out.println("æ£ç´¢å°æ件"+fname);
try
{
//æå®æ件路å¾åå称
String path = savepaths+fname+".html";
File outfilename = new File(path);
/** *//**
* æ£æ¥æ件æ¯å¦åå¨.
* @throws IOException
*
*/
if (!outfilename.exists()) {
System.err.println("ç®æ è·¯å¾æ ååæ件ï¼å¼å§è½¬æ¢");
System.out.print("æ£å¨è½¬æ¢æ件ï¼"+fname);
Dump(paths,fname,savepaths);
}
else
{
System.out.print("æ件已åå¨ï¼æ¾å¼å建ï¼è¯·å¤çåå¨æ件ååè¿è¡...\n");
continue;
}
RandomAccessFile mm = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
public static void Dump(String FilePath,String FileName,String OutPath)
{
com.sun.star.uno.XComponentContext xContext = null;
try {
// get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
System.out.println("Connected to a running office ...");
// get the remote office service manager
com.sun.star.lang.XMultiComponentFactory xMCF =
xContext.getServiceManager();
Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
com.sun.star.frame.XComponentLoader xCompLoader =
(com.sun.star.frame.XComponentLoader)
UnoRuntime.queryInterface(
com.sun.star.frame.XComponentLoader.class, oDesktop);
java.io.File sourceFile = new java.io.File(FilePath+FileName+".doc");//读åçæ件
StringBuffer sLoadUrl = new StringBuffer("file:///");
sLoadUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
sourceFile = new java.io.File(OutPath+FileName+".html");//è¾åºçæ件
System.out.print(OutPath+" "+FileName);
StringBuffer sSaveUrl = new StringBuffer("file:///");
sSaveUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
com.sun.star.beans.PropertyValue[] propertyValue =
new com.sun.star.beans.PropertyValue[1];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Hidden";
propertyValue[0].Value = new Boolean(true);
Object oDocToStore = xCompLoader.loadComponentFromURL(
sLoadUrl.toString(), "_blank", 0, propertyValue );
com.sun.star.frame.XStorable xStorable =
(com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
com.sun.star.frame.XStorable.class, oDocToStore );
propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Overwrite";
propertyValue[0].Value = new Boolean(true);
propertyValue[1] = new com.sun.star.beans.PropertyValue();
propertyValue[1].Name = "FilterName";
propertyValue[1].Value = "HTML (StarWriter)";//ä½ ä¸å®åç°äºï¼æè¿éæ¹æå
¶ä»åæ°ï¼å¯ä»¥ä¿å为ä¸åçæ件 MS Word 97ï¼writer_pdf_Export
xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );
System.out.println("\nDocument \"" + sLoadUrl + "\" saved under \"" +
sSaveUrl + "\"\n");
com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
oDocToStore );
if (xCloseable != null ) {
xCloseable.close(false);
} else
{
com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
UnoRuntime.queryInterface(
com.sun.star.lang.XComponent.class, oDocToStore );
xComp.dispose();
}
System.out.println("document closed!");
}
catch( Exception e ) {
e.printStackTrace(System.err);
System.exit(1);
}
}
public static void main(String args[]) throws Exception
{
String paths = new String("c:\\a\\");
String savepaths = new String ("f:\\");
readdoc(paths,savepaths);
}
}
温馨提示:答案为网友推荐,仅供参考