我試圖獲取一個字符串值,該值可能位於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;
我試過.Value2但vs2015不接受。我也試過字符串AValue = Application.Selection.Cells [1,1] .Value2;但vs2015不承認選擇。我正在嘗試將excel activecell值添加到在vs2015中運行的c#winform應用程序內部的文本框中。 – jrdnoland
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