Tuesday, February 17, 2009

C# Code to read excel book







C# code to read excel document.
using System.Collections;
using Microsoft.Office.Interop.Excel;
using System;
using System.Reflection;
class Sample
{
    static void Main()
    {
        string file = @"c:\csnet\jap\ex1\sample1.xlsx";
        Microsoft.Office.Interop.Excel.ApplicationClass ap = new ApplicationClass();
        Missing m=Missing.Value;
       Workbook wb=ap.Workbooks.Open(file, m, m, m, m, m, m, m, m, m, m, m, m, m, m);
       Worksheet sh =(Worksheet) wb.Sheets[1];
       for (int i = 1; i < 10; i++)
       {
           for(int j=1;j<=3;j++)
           {
               Range r =(Range) sh.Cells[i, j];
               Console.Write(r.Value2);
           }
           Console.WriteLine();
       }
        ap.Quit();
      
    }
}