`

2010 word 转换pdf jacob

    博客分类:
  • java
阅读更多
package as.pdf;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordAsPDF {
public static void wordToPDF(String docfile, String toFile,int type) { 
        ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word 
        //ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel 
       //ActiveXComponent app = new ActiveXComponent("PowerPoint.Application"); // 启动PowerPoint 
        try { 
            app.setProperty("Visible", new Variant(false));  //转ppt的时候把false改为了true
            Dispatch docs = app.getProperty("Documents").toDispatch();  //word
            //Dispatch docs = app.getProperty("Workbooks").toDispatch();  //excel
            //Dispatch docs = app.getProperty("Presentations").toDispatch();  //ppt
            Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {
            docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); 
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { 
                    toFile, new Variant(type) }, new int[1]); 
            Variant f = new Variant(false); 
            Dispatch.call(doc, "Close", f);  //转ppt的时候把这两行代码去掉
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            app.invoke("Quit", new Variant[] {}); 
        } 
    } 

public static void main(String[] args) {
//源文件全路径
String docfile ="D:\\Downloads\\b.docx";
//String docfile ="D:\\Downloads\\a.xls";
//String docfile ="D:\\Downloads\\a.ppt";
//些路径test为实际存在的目录,s后面为要另存为的文件名
String toFile="C:\\Documents and Settings\\Administrator\\桌面\\end\\"+17;
//wordToPDF(docfile, toFile,32);//ppt
wordToPDF(docfile, toFile,17);//word
//wordToPDF(docfile, toFile,57);//excel
}
}
需要 jacob.jar, jacob.bll放到jdk下
1
0
分享到:
评论
1 楼 selectFrom 2012-06-21  
com.jacob.com.ComFailException: Invoke of: SaveAs
Source: Microsoft Office Excel
Description: 类 Workbook 的 SaveAs 方法无效

at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
at com.jacob.com.Dispatch.invoke(Dispatch.java:498)
at cn.rdeasy.wenku.web.WordAsPDF.wordToPDF(WordAsPDF.java:27)
at cn.rdeasy.wenku.web.WordAsPDF.main(WordAsPDF.java:47)


大哥:我运行的时候报上面的错误,请问是什么原因

相关推荐

Global site tag (gtag.js) - Google Analytics