我知道java可以调用word,excel的控件,而且可以把.doc文件转化为.xml,html文件,是通过一个jacob包,他是一个做好的接口,可以处理word,excel,pb等,我了解不深,还没有试过打开excel文件

解决方案 »

  1.   

    我告诉你吧,我当初入门时用过xml,html,doc文件转化,主要是处理com中数据交换,我是过的一个例子,成功一半,但肯定是对的,给你例子,但你要下载jacob包,你去找哦,这个包我也有,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,
      

  2.   

    crm2000(浏览器):
    你也用过POI??
    我也看过POI,但还是不知道怎么做哦.请指点.
      

  3.   

    shangxinyu123(龙) :谢谢你.我去研究一下.