如何访问c#中的Excel工作表单元格

我正在使用C#中的Microsoft.Office.Interop.Excel命名空间来访问Excel工作表。

我有一个访问工作表中的特定单元格有点麻烦。

这是我迄今为止:

 using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Windows; using System.Windows.Forms; using System.Windows.Input.Manipulations; using Application = Microsoft.Office.Interop.Excel.Application; namespace excelProgram { class Program { [STAThreadAttribute] public static void Main(string[] args) { Program p = new Program(); p.openWorkSheet(); Console.ReadKey(true); } private void openWorkSheet() { var xl= new Application(); xl.Visible = true; xl.Workbooks.Open(@"C:\Documents and Settings\user\Desktop\Book1.xls"); } } } 

我创build了一个Application()对象,它在我的PC上打开一个Excel工作簿。 这工作正常,但是我不知道如何访问工作簿内的工作表中的特定单元格,例如单元格B1。

有人可以帮忙吗?

获得参考特定的表格,然后到特定的范围,例如像这样。

(示例使用对Excel 2007 PIA的引用:C:\ Windows \ assembly \ GAC \ Microsoft.Office.Interop.Excel \ 12.0.0.0__71e9bce111e9429c \ Microsoft.Office.Interop.Excel.dll)

 using Microsoft.Office.Interop.Excel; using Application = Microsoft.Office.Interop.Excel.Application; using Excel = Microsoft.Office.Interop.Excel; Application excelApplication = new Excel.Application { Visible = true, ScreenUpdating = true }; _Workbook workbook = excelApplication.Workbooks.Open(@"C:\Temp\Book1.xlsx"); _Worksheet sheet = workbook.Worksheets[1]; Range range = sheet.Range["B1"]; range.Formula = "Test"; excelApplication.Quit();