能不能帮我看看我的代码:
在classpath中配置jacob.jar;
在system32中添加了dll文件;import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;public class FileExtracter {    private ActiveXComponent MsWordApp = null;
    private Dispatch document = null;    public static void main(String[] args) {
        String filepath = "c:/test.doc";
        FileExtracter word = new FileExtracter();
        word.openWord(true);        word.openDocument(filepath); 
   }    public void openWord(boolean makeVisible) {        if (MsWordApp == null) {
            MsWordApp = new ActiveXComponent("Word.Application");
        }        Dispatch.put(MsWordApp, "Visible",
                     new Variant(makeVisible));
    }    public void openDocument(String path) {        MsWordApp.setProperty("Visible", new Variant(true));
        Dispatch odocuments = MsWordApp.getProperty("Documents").toDispatch();
        document = Dispatch.call(odocuments, "Open", path).toDispatch();    }}
在cmd下运行命令出错:
/*D:\jdk142\bin>nmake -f FileExtracter.javaMicrosoft (R) Program Maintenance Utility   Version 6.00.8168.0
Copyright (C) Microsoft Corp 1988-1998. All rights reserved.FileExtracter.java(1) : fatal error U1034: syntax error : separator missing
Stop.
*/
请帮忙看看!
如果在jbuilder下运行则报错:不过word的那个窗口能出来了/*
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Documents
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at testr.FileExtracter.openDocument(FileExtracter.java:59)
at testr.FileExtracter.main(FileExtracter.java:18)
*/

解决方案 »

  1.   

    已解决,是jacob的版本问题!
      

  2.   

    之前用jacob写过一个在web上操作word的API,你如果感兴趣的话可以找给你:)
      

  3.   

    import com.jacob.com.*;
    import com.jacob.activeX.*;
    import com.jacob.com.Dispatch;public class ExcelTest {
        public ExcelTest() {
        }    private static ActiveXComponent xl;
        private static Object workbooks = null;
        private static Object workbook = null;
        private static Object sheet = null;
        private static String filename = null;
        private static boolean readonly = false;    public static void main(String[] args) {        String file = "c:\\test.xls";
            OpenExcel(file, true); //false为不显示打开Excel
            //SetValue("A1", "Value", "2");
            // System.out.println(GetValue("A3"));
            CloseExcel(false);
        }    //打开Excel文档
        private static void OpenExcel(String file, boolean f) {
            try {
                filename = file;
                xl = new ActiveXComponent("Excel.Application");
                xl.setProperty("Visible", new Variant(f));
                workbooks = xl.getProperty("Workbooks").toDispatch();
                Dispatch.put(xl, "Visible", new Variant(true));
    //以下语句出错
                workbook = Dispatch.invoke(workbooks,
                                           "Open",
                                           Dispatch.Method,
                                           new Object[] {filename,
                                           new Variant(false),
                                           new Variant(readonly)}, //是否以只读方式打开
                                           new int[1]).toDispatch();        } catch (Exception e) {
                e.printStackTrace();
            }
        }    //关闭Excel文档
        private static void CloseExcel(boolean f) {
            try {
    //以下语句出错
                Dispatch.call(workbook, "Save");
                Dispatch.call(workbook, "Close", new Variant(f));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                xl.invoke("Quit", new Variant[] {});
            }
        }//写入值
        private static void SetValue(String position, String type, String value) {
            sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
            Object cell = Dispatch.invoke(sheet, "Range",
                                          Dispatch.Get,
                                          new Object[] {position},
                                          new int[1]).toDispatch();
            Dispatch.put(cell, type, value);
        }//读取值
        private static String GetValue(String position) {
    //以下语句出错
            Object cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
                                          new Object[] {position}, new int[1]).
                          toDispatch();
            String value = Dispatch.get(cell, "Value").toString();        return value;
        }
    }
      

  4.   

    操作Excel没必要用jacob啊,有纯java的开源包
    http://qingyuan18.spaces.live.com/?_c11_blogpart_blogpart=blogview&_c=blogpart&partqs=cat%3d%25e8%25ae%25a1%25e7%25ae%2597%25e6%259c%25ba%25e4%25b8%258e%2bInternet这里面有操作excel的包和api,我用的jxl