在C#中访问打开的Excel工作簿

我需要访问一个已经打开的excel文件。 我以为只是检查.Workbooks属性,它会在那里,但事实并非如此。 什么是正确的方式来参考打开的工作簿?

 var app = new Microsoft.Office.Interop.Excel.Application(); // the count is 0 =( app.Workbooks.Count == 0; 

编辑

我可以通过…获取Excel应用程序的参考

 app = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); 

但是app.Workbooks.Count仍然是0为什么它不能够获得对打开的工作簿的引用?

而不是实例化一个新的实例,检查一个现有的:

 try { Microsoft.Office.Interop.Excel.Application app = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); } catch { // Excel is not running. } 
  String constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+txtSourceFile.Text+";Extended Properties='Excel 8.0;HDR=YES;';"; String constr2 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtLibrary.Text + ";Extended Properties='Excel 8.0;HDR=YES;';"; OleDbConnection con = new OleDbConnection(constr); OleDbConnection con2 = new OleDbConnection(constr2); OleDbConnection con3 = new OleDbConnection(constr); OleDbCommand oconn = new OleDbCommand("Select * From [eudra$]", con); OleDbCommand oconn2 = new OleDbCommand("Select * From [Sheet1$]", con2); OleDbCommand oconn3 = new OleDbCommand("Select * From [eudra$] where EXAMPARM in ('with one or more serious adverse events','with one or more non-serious adverse events that met the incidence cutoff')", con); if (txtSourceFile.Text != "") { con.Open(); con2.Open(); con3.Open(); OleDbDataAdapter sda = new OleDbDataAdapter(oconn); OleDbDataAdapter sda2 = new OleDbDataAdapter(oconn2); OleDbDataAdapter sda3 = new OleDbDataAdapter(oconn3); DataTable data = new DataTable(); sda.Fill(data); DataTable data2 = new DataTable(); sda2.Fill(data2); DataTable data3 = new DataTable(); sda3.Fill(data3); var test = JoinDataTables(data, data2, (row1, row2) => (row1.Field<string>("BODY_SYS").ToUpper() == row2.Field<string>("Term").ToUpper() )); data3.Merge(test, true); dgvImp.DataSource = data3; con.Close(); } 
 // creating Excel Application Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet worksheet = workbook.Sheets["Sheet1"]; worksheet = workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Exported from gridview"; try { // storing header part in Excel for (int i = 1; i < dgvRESULTS.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dgvRESULTS.Columns[i - 1].HeaderText; worksheet.Cells[1, i].Interior.Color = System.Drawing.Color.LightYellow; } // storing Each row and column value to excel sheet for (int i = 0; i < dgvRESULTS.Rows.Count - 1; i++) { for (int j = 0; j < dgvRESULTS.Columns.Count; j++) { if (dgvRESULTS.Rows[i].Cells[j].Value != null) { worksheet.Cells[i + 2, j + 1] = dgvRESULTS.Rows[i].Cells[j].Value.ToString(); //worksheet.Cells[i + 2, j + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(dgvRESULTS.Rows[i].DefaultCellStyle.BackColor); } else { worksheet.Cells[i + 2, j + 1] = ""; } } } } catch(NullReferenceException ne) { } string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // save the application workbook.SaveAs(filePath +"\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // Exit from the application app.Quit();