2016-04-29 49 views
1

我試圖獲取一個字符串值,該值可能位於activecell中的任何excel 2010工作簿中,並在c#應用程序winform文本框中使用該值。我正在使用vs2015和excel 2010.獲取excel activeworkbook activesheet activecell值並將其放入c#winform文本框中

這是我嘗試過沒有成功的。

//Get active excel instance 

Excel.Application xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); 

//Get active excel workbook 

Excel.Workbook xlWorkBook = (Excel.Workbook)xlApp.ActiveWorkbook; 

//Get active excel worksheet 

Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;    

//Get value for excel 

string AValue = xlWorkSheet.Cells[2, 2].ToString(); 

//Put value in c# winform textbox 

txtSearchPattern.Text = AValue; 

回答

0

使用的Microsoft.Office.Interop.Excel

一個Range.Cells集合的成員是Range對象,而不是字符串。嘗試Cells[x,y].Value2來檢索單元格的值。

此外,當前選擇的範圍目的是應用程序範圍內可變Application.Selection,所以你應該能夠檢索使用Application.Selection.Cells[1,1].Value2而不引用任何工作簿/工作表的活動選擇左上角大部分細胞的值。

編輯:在這種情況下,您最好使用Application.Activecell,因爲Selection可以返回非範圍對象(圖片等)。

+0

我試過.Value2但vs2015不接受。我也試過字符串AValue = Application.Selection.Cells [1,1] .Value2;但vs2015不承認選擇。我正在嘗試將excel activecell值添加到在vs2015中運行的c#winform應用程序內部的文本框中。 – jrdnoland

+0

Hrmm ...我現在在VBA IDE中進行所有的開發,但假設您使用的是Microsoft.Office.Interop.Excel,您應該看到[activecell](https://msdn.microsoft.com/en -us/library/microsoft.office.interop.excel._application.activecell.aspx?cs-save-lang = 1&cs-lang = vb#code-snippet-1)(我現在意識到比選擇更好,更新了我的答案)和[選擇](https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._application.selection.aspx)應用程序對象的屬性。 – Snachmo

0

我不確定這是否是最好的方法,但它似乎允許程序運行並捕獲null異常錯誤。

  try 
      {   
      //Get active excel instance 
      Excel.Application xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); 

      //Get active excel workbook 
      Excel.Workbook xlWorkBook = (Excel.Workbook)xlApp.ActiveWorkbook; 

      //Get active excel worksheet 
      Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet; 

      //Get value for excel   
      string AValue = xlApp.ActiveCell.Value2.ToString(); 

      //Return value to winform textbox 
      txtSearchPattern.Text = AValue;  
      } 

      catch(NullReferenceException) 
      { 
       MessageBox.Show("ActiveCell was null"); 
      } 
+0

這再次停止工作。現在我似乎無法獲得活動手冊。我不清楚這種編程方式,有人能指出我的方向嗎? – jrdnoland

0

分配以下代碼具有Click事件處理程序按鈕(Button1的)一個WinForm(的button1_Click)如下:

using Microsoft.Office.Excel.Interop; 

namespace XYZ 
{ 
    public partial class ABC : Form 
    { 
    Excel.Range activecell = null; 
    Excel.Application xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); 

    public ABC() 
    { 
    InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (xlApp.Selection is Excel.Range) 
     { 
      activecell = xlApp.Selection as Excel.Range; 

      //Return value to winform textbox 
      txtSearchPattern.Text = activecell.Value2.ToString(); 
     } 
    } 
} 
} 

當你點擊在Excel單元格工作表中包含一個值,然後點擊button1,文本框(txtSearchPattern)的值相應更新。

相關問題