/*
Open Your Visual Studio
Create: Visual C# - Console Application Project
Right click on: References
Select: Add references...
Select: com
Select: Microsoft Office 12.0/15.0/16.0 Object Library
In References list you will see: Microsoft.Office.Interop.Excel
*/
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace WorkWithExcel
{
class ExcelClass
{
static void Main(string[] args)
{
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
string WorkbookFileName = "e:/test.xlsx";
Excel.Workbook excelWorkbook = null;
try
{
excelWorkbook = excelApp.Workbooks.Open(WorkbookFileName);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
System.Array RangeValues = (System.Array)excelWorksheet.Range["C4..D15"].Value2;
foreach (Object rv in RangeValues)
{
if (rv != null)
Console.Write("{0}\n", rv);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}