你可以用jacob包,肯定可以把xml,转换为doc文件,我试过
我给你一个例子,但必须有jacob包,这个例子表示word文件转化为xml,转换为html, 文件可以相互转化,不知道是否可以,
import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;public class Exam_2
{
public static void main(String[] args)
{
System.out.println("run");
//ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
ActiveXComponent app = new ActiveXComponent("X9.Z.1");//启动word
//System.out.println("run1");
// Object obj = app.getObject();
// Object obje=Dispatch.get(obj,"XX");
// System.out.println("first");
System.out.println("version="+app.getProperty("intcom"));
//System.out.println("version="+Dispatch.get(obj, "intcom"));
String inFile = "D:\\test\\Jacob\\my.doc";//要转换的word文件
String tpFile = "D:\\test\\Jacob\\my.htm";//临时文件
String otFile = "D:\\test\\Jacob\\my.xml";//目标文件
boolean flag = false;
System.out.println("first");
try {
int x=2;
int y=7;
Variant[] var=new Variant[2];
Variant var1=new Variant();
var[0]=new Variant(x);
var[1]=new Variant(y);
System.out.println(var[0].getInt());
// var[1]=new Variant(y);
//var1=app.getProperty("x");
// Object docs = app.getProperty("Documents").toDispatch();
//Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开word文件
// Dispatch.invoke("SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);//作为html格式保存到临时文件
//var1=app.invoke();
System.out.println("pass");
/*app.setProperty("Visible", new Variant(false));//设置word不可见
System.out.println("second");
System.out.println("three");
System.out.println("four");
Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);//作为html格式保存到临时文件
System.out.println("five");
Variant f = new Variant(false);
System.out.println("six");
Dispatch.call(doc, "Close", f);
System.out.println("seven");*/
flag = true;
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
// app.invoke("Quit", new Variant[] {});
}
System.out.println("flag="+flag);
/*if ( flag )
{
app = new ActiveXComponent("Excel.Application");//启动excel
try {
app.setProperty("Visible", new Variant(false));//设置excel不可见
Object workbooks = app.getProperty("Workbooks").toDispatch();
Object workbook = Dispatch.invoke(workbooks,"Open",Dispatch.Method,new Object[]{tpFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开临时文件
System.out.println("run excelll");
Dispatch.invoke(workbook,"SaveAs", Dispatch.Method, new Object[]{otFile,new Variant(46)}, new int[1]);//以xml格式保存到目标文件
System.out.println("run pass");
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
}
catch (Exception e)
{
System.out.println("run exception");
e.printStackTrace();
}
finally
{
System.out.println("run end");
app.invoke("Quit", new Variant[] {});
try
{
File file = new File(tpFile);
file.delete();
} catch (Exception e) {}
}
}*/
}
}
/////D:\test\Jacob>javac -classpath jacob.jar Exam_2.java/*D:\test\Jacob>java -cp jacob.jar;. Exam_2
run excelll
run exception
com.jacob.com.ComFailException: Invoke of: SaveAs
Source: Microsoft Excel 8.0
Description: at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invoke(Dispatch.java)
at Exam_2.main(Exam_2.java:38)
run end
你要这个包,我也可以给你
主要思想是java调用dll,
我给你一个例子,但必须有jacob包,这个例子表示word文件转化为xml,转换为html, 文件可以相互转化,不知道是否可以,
import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;public class Exam_2
{
public static void main(String[] args)
{
System.out.println("run");
//ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
ActiveXComponent app = new ActiveXComponent("X9.Z.1");//启动word
//System.out.println("run1");
// Object obj = app.getObject();
// Object obje=Dispatch.get(obj,"XX");
// System.out.println("first");
System.out.println("version="+app.getProperty("intcom"));
//System.out.println("version="+Dispatch.get(obj, "intcom"));
String inFile = "D:\\test\\Jacob\\my.doc";//要转换的word文件
String tpFile = "D:\\test\\Jacob\\my.htm";//临时文件
String otFile = "D:\\test\\Jacob\\my.xml";//目标文件
boolean flag = false;
System.out.println("first");
try {
int x=2;
int y=7;
Variant[] var=new Variant[2];
Variant var1=new Variant();
var[0]=new Variant(x);
var[1]=new Variant(y);
System.out.println(var[0].getInt());
// var[1]=new Variant(y);
//var1=app.getProperty("x");
// Object docs = app.getProperty("Documents").toDispatch();
//Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开word文件
// Dispatch.invoke("SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);//作为html格式保存到临时文件
//var1=app.invoke();
System.out.println("pass");
/*app.setProperty("Visible", new Variant(false));//设置word不可见
System.out.println("second");
System.out.println("three");
System.out.println("four");
Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);//作为html格式保存到临时文件
System.out.println("five");
Variant f = new Variant(false);
System.out.println("six");
Dispatch.call(doc, "Close", f);
System.out.println("seven");*/
flag = true;
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
// app.invoke("Quit", new Variant[] {});
}
System.out.println("flag="+flag);
/*if ( flag )
{
app = new ActiveXComponent("Excel.Application");//启动excel
try {
app.setProperty("Visible", new Variant(false));//设置excel不可见
Object workbooks = app.getProperty("Workbooks").toDispatch();
Object workbook = Dispatch.invoke(workbooks,"Open",Dispatch.Method,new Object[]{tpFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开临时文件
System.out.println("run excelll");
Dispatch.invoke(workbook,"SaveAs", Dispatch.Method, new Object[]{otFile,new Variant(46)}, new int[1]);//以xml格式保存到目标文件
System.out.println("run pass");
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
}
catch (Exception e)
{
System.out.println("run exception");
e.printStackTrace();
}
finally
{
System.out.println("run end");
app.invoke("Quit", new Variant[] {});
try
{
File file = new File(tpFile);
file.delete();
} catch (Exception e) {}
}
}*/
}
}
/////D:\test\Jacob>javac -classpath jacob.jar Exam_2.java/*D:\test\Jacob>java -cp jacob.jar;. Exam_2
run excelll
run exception
com.jacob.com.ComFailException: Invoke of: SaveAs
Source: Microsoft Excel 8.0
Description: at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invoke(Dispatch.java)
at Exam_2.main(Exam_2.java:38)
run end
你要这个包,我也可以给你
主要思想是java调用dll,
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货