代码我没有带来~基本上就如同下面的
import   java.io.File;   
  import   com.jacob.com.*;   
  import   com.jacob.activeX.*;   
    
  public   class   Word2Xml1   {   
    
  public   void toxml(String inFile,String otFile)   {   
    
  ActiveXComponent   app   =   new   ActiveXComponent("Word.Application");//启动word   
  //String   inFile   =   "e:\\my1.doc";//要转换的word文件   
  String   tpFile   =   "e:\\my.htm";//临时文件   
  //String   otFile   =   "e:\\my.xml";//目标文件   
  boolean   flag   =   false;   
  try   {   
  app.setProperty("Visible",   new   Variant(false));//设置word不可见   
  Dispatch   docs   =   app.getProperty("Documents").toDispatch();   
  Dispatch   doc   =   Dispatch.invoke(   
  docs,   
  "Open",   
  Dispatch.Method,   
  new   Object[]   {   inFile,   new   Variant(false),   
  new   Variant(true)   },   new   int[1]).toDispatch();//打开word文件   
  Dispatch.invoke(doc,   "SaveAs",   Dispatch.Method,   new   Object[]   {   
  tpFile,   new   Variant(8)   },   new   int[1]);//作为html格式保存到临时文件   
  Variant   f   =   new   Variant(false);   
  Dispatch.call(doc,   "Close",   f);   
  flag   =   true;   
  }   catch   (Exception   e)   {   
  e.printStackTrace();   
  }   finally   {   
  app.invoke("Quit",   new   Variant[]   {});   
  }   
    
  if   (flag)   {   
  app   =   new   ActiveXComponent("Excel.Application");//启动excel   
  try   {   
  app.setProperty("Visible",   new   Variant(false));//设置excel不可见   
  Dispatch   workbooks   =   app.getProperty("Workbooks").toDispatch();   
  Dispatch   workbook   =   Dispatch.invoke(   
  workbooks,   
  "Open",   
  Dispatch.Method,   
  new   Object[]   {   tpFile,   new   Variant(false),   
  new   Variant(true)   },   new   int[1]).toDispatch();//打开临时文件   
  System.out.println("open   temp   html   successfully");   
  Dispatch.invoke(workbook,   "SaveAs",   Dispatch.Method,   
  new   Object[]   {   otFile,   new   Variant(46)   },   new   int[1]);//以xml格式保存到目标文件   
  Variant   f   =   new   Variant(false);   
  Dispatch.call(workbook,   "Close",   f);   
  }   catch   (Exception   e)   {   
  e.printStackTrace();   
  }   finally   {   
  app.invoke("Quit",   new   Variant[]   {});   
  /*   
  try   {   
  File   file   =   new   File(tpFile);   
  file.delete();   
  }   catch   (Exception   e)   {   
  }   
  */   
  }   
  }   
  }   
   public static void main(){
    new Word2Xml1.toxml("e:\\my1.doc", "e:\\my.xml");
     } 
  }   
其中有一句app.invoke("Quit",   new   Variant[]   {});  这句应该可以关闭打开的excel进程,我在这个程序里面写了一个main方法来调用这个方法,结果也能正常的关闭excel进程,但是我在别的类中以同样的方法调用这个方法却出现了一个不能结束的excel进程,请问这是怎么回事呢?

解决方案 »

  1.   

    import com.jacob.com.*;
    import com.jacob.activeX.*;
    class 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 = "f:\\java\\test.xls";
         OpenExcel(file,false);//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();
             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;
      }
    }
      

  2.   

    以上是使用JACOB进行Excel读写控制,仅供参考
      

  3.   

    楼主可以使用 ComThread.Release(); 关闭进程