我有excel包含4张(汽车,型号,客户,联系方式)我在下面有这个代码。 我需要从excel(Tab)中的特定表中读取,如何做到这一点?我正在使用Microsoft Office Interop excel
string Mysheet = @"C:\MSFT Site Account Updates_May 2015.xlsx";
var excelApp = new Excel.Application();
Excel.Workbooks books = excelApp.Workbooks;
Excel.Workbook sheets = books.Open(Mysheet);
for (int i = 1; i < 2; i++)
{
string Name = ((excelApp.Cells[i, 1] as Excel.Range).Value);
string SiteUrl = ((excelApp.Cells[i, 4] as Excel.Range).Value);
Console.WriteLine("Account created");
}
谢谢大家
答案 0 :(得分:3)
var workbook = books.Open(Mysheet);
var worksheet = (Worksheet) workbook.Sheets["Customers"];
Console.WriteLine(worksheet.Cells[2, 2]); // for example...
答案 1 :(得分:0)
您需要获取要从中读取数据的工作表的Worksheet
。要从A1
获取范围Cars
,例如:
var app = new Application();
Workbooks workbooks = app.Workbooks;
Workbook workbook = workbooks.Open(@"C:\MSFT Site Account Updates_May 2015.xlsx");
Worksheet sheet = workbook.Sheets["Cars"];
Range range = sheet.Range["A1"];