如何通过名称从excel中的特定工作表中读取

时间:2015-05-19 14:34:22

标签: c# excel office-interop

我有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");

        }

谢谢大家

2 个答案:

答案 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"];