2016-04-29 45 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)的值相应更新。

相关问题