C#中调用Exce一般采用下面的方法
Excel.Application xApp=new Excel.Application();
xBook = xApp.Workbooks._Open("abc.xls", Missing.Value,Missing.Value,Missing.Value,Missing.Value ,Missing.Value,Missing.Value,Missing.Value,Missing.Value ,Missing.Value,Missing.Value,Missing.Value,Missing.Value);如果Excel文件已经打开,怎么得到他
Excel.Application xApp=已经打开的Excel文件
哪位知道怎么实现

解决方案 »

  1.   

    关注,Workbooks._Open()里面的参数是什么意思,有人解释一下吗?
      

  2.   

    this.ofdData.Title="打开数据记录文件";
    DialogResult drl=this.ofdData.ShowDialog();
    System.Data.DataSet ds=new DataSet();
    ds.Tables.Add("tabPhone");
    if(drl==DialogResult.OK)
    {
    try
    {
    foreach(string strFileName in this.ofdData.FileNames)
    {
    Excel.Application myExcel=new Excel.Application();
    myExcel.Visible=false;
    Excel.Workbook wb=myExcel.Workbooks.Open(strFileName,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
    foreach(Excel.Worksheet ws in wb.Worksheets)
    {
    #region 读取单元格方式
    // int intTitelRow=1,intTitelCol=1;
    // for(int i=1;i<=ws.Rows.Count;i++)
    // {
    // if(ws.get_Range(ws.Cells[i,intTitelCol],ws.Cells[i,intTitelCol]).Value2==null)
    // break;
    // System.Data.DataRow dr=this.dsPhone1.tabPhone.NewRow();
    // for(int j=1;j<=ws.Columns.Count;j++)
    // {
    // Excel.Range r=ws.get_Range(ws.Cells[i,j],ws.Cells[i,j]);
    // Excel.Range r1=ws.get_Range(ws.Cells[intTitelRow,j],ws.Cells[intTitelRow,j]);
    // if(r1.Value2==null)
    // break;
    // if(r.Value2.ToString().Trim()=="card_no")
    // {
    // intTitelRow=i;
    // intTitelCol=j;
    // break;
    // }
    // dr[r1.Value2.ToString()]=r.Value2.ToString();
    // }
    // this.dsPhone1.tabPhone.Rows.Add(dr);
    // }
    #endregion
    #region 数据连拉方式
    System.Data.OleDb.OleDbConnection oc=new OleDbConnection();
    oc.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties=Excel 8.0;";
    System.Data.OleDb.OleDbDataAdapter oda=new OleDbDataAdapter("select * from ["+ws.Name+"$]",oc);
    myExcel.Quit();
    oda.Fill(ds,"tabPhone");
    this.gexPhone.SetDataBinding(ds,"tabPhone");
    this.gexPhone.RetrieveStructure();
    oc.Close();
    #endregion
    }
    }
    }
    catch(System.Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    }
      

  3.   

    VB.NET 里,可以通过GetObject获得。
    Sub GetExcel()
       Dim MyXL As Object   ' Variable to hold reference
       ' to Microsoft Excel.
       Dim ExcelWasNotRunning As Boolean   ' Flag for final release.   ' Test to see if there is a copy of Microsoft Excel already running.
       On Error Resume Next   ' Defer error trapping.
       ' Getobject function called without the first argument returns a
       ' reference to an instance of the application. 
       ' If the application is not running, an error occurs.
       MyXL = GetObject(, "Excel.Application")
       If Err().Number <> 0 Then ExcelWasNotRunning = True
       Err().Clear() ' Clear Err object in case error occurred.   ' Check for Microsoft Excel. If Microsoft Excel is running,
       ' enter it into the Running Object table.
       DetectExcel()   ' Set the object variable to reference the file you want to see.
       MyXL = GetObject("c:\vb\TEST.XLS")   ' Show Microsoft Excel through its Application property. Then
       ' show the actual window containing the file using the Windows
       ' collection of the MyXL object reference.
       MyXL.Application.Visible = True
       MyXL.Parent.Windows(1).Visible = True
       '  Do manipulations of your  file here.
       '
    End Sub
      

  4.   

    获得打开的excel的句柄
    private IntPtr _Handle;   //用于保存Excel的窗口句柄
    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);.......String WndClassCaption="Microsoft Excel - "+xlsName.Substring(xlsName.LastIndexOf (@"\")+1);
    IntPtr hwnd_win = FindWindow("XLMAIN", WndClassCaption);
    if(hwnd_win==IntPtr.Zero)
    {
    hwnd_win = FindWindow("MS-SDIa",xlsName.Substring(xlsName.LastIndexOf (@"\")+1));
    if(hwnd_win==IntPtr.Zero)
    {

    MessageBox.Show ("没找到窗口类   "+xlsName.Substring(xlsName.LastIndexOf (@"\")+1));
    return null;
    }
    }
    ////////XLMAIN,MS-SDIa 是用spy++看到的