Reading Excel File from C#

Posted by admin | Posted in C# | Posted on 01-02-2010-05-2008

0

Here is the code snippet:

add to your directives:

using Excel = Microsoft.Office.Interop.Excel;

——

Excel.Application xlApp = null;
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;
Excel.Range range = null;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(“Excel FileName”, 0, true, 5, “”, “”, true, Excel.XlPlatform.xlWindows, “\t”, false, false, 0, true, 1, 0);

//Get the First Excell Sheet from WorkBook

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

var dt = new DataTable();

for (int rCnt = 1;  rCnt <= 99;  rCnt++)
{
range = xlWorkSheet.get_Range(“A” + rCnt.ToString(), “L” + rCnt.ToString());

Array myValues = (System.Array)range.Cells.get_Value(Type.Missing);

//Add the array to datatable
AddDataRow(dt, myValues);

}

happy coding…

KennTriX.info is Digg proof thanks to caching by WP Super Cache