[英]How to access an Excel WorkSheet cell in c#
我在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");
}
}
}
我创建了一个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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.