该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
分享一个刚写的小例子给大家
import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;@ParentPackage("struts-default")@Namespace("/word")@Action("getWord")@Results({ @Result(name = "success",location = "/images/aaaa.html") })@SuppressWarnings("all")public class GetWordAction { public String getWord(){ ActiveXComponent app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", new Variant(false)); Dispatch doc1 = app.getProperty("Documents").toDispatch(); //打开aaaa.doc Dispatch doc2 = Dispatch.invoke( doc1, "Open", Dispatch.Method, new Object[]{"d://tt.doc", new Variant(false), new Variant(true)}, new int[1] ).toDispatch(); //另存为aaaa.html String name="aaaa.html"; Dispatch.invoke( doc2, "SaveAs", Dispatch.Method, new Object[]{ "d://"+name, new Variant(8)//7为txt格式, 8保存为html格式 }, new int[1] ); Variant f = new Variant(false); Dispatch.call(doc2, "Close", f); File file = new File("d://aaaa.html"); String realPath = ServletActionContext.getServletContext() .getRealPath("/images"); File filetemp = new File(realPath); File f1 = new File(filetemp, "aaaa.html"); try { FileUtils.copyFile(file, f1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "success"; }}