Sanjay Surendra
Sanjay Surendra

Reputation: 47

Read excel files in C# without using third party dlls/oledb/interop/sdk

I want to read excel files in C#. I dont want to use following things while reading:

  1. ThirdParty dll( My client is not allowing me to use third party dll )
  2. SDK(Same as above reason as am not allowed to download)
  3. Not allowed to use interop/oledb as in target server there is no office installed.
  4. OpenXml

Note: Am using vs 2005.

Please suggest me anything else>>

Thanks in advance, Sanjay

Upvotes: 0

Views: 7114

Answers (1)

Steve
Steve

Reputation: 216293

As I have said in my comment, using OleDb is not the same thing that Interop.
OleDb is part of the NET framework and, if your customer uses your application, it has already the Framework installed and running. So this example could be of help to demonstrate the fact that OleDb could read your target excel file without Office installed.

In this example I have a simple worksheet with three columns (and headers in the first line) The first and second column are simple text column while the third contains numeric values

try
{
    string con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;Extended Properties='Excel 8.0;HDR=Yes;'";
    using(OleDbConnection connectin = new  System.Data.OleDb.OleDbConnection(con));
    {
        connectin.Open();
        OleDbCommand command = new System.Data.OleDb.OleDbCommand("select * from [Sheet1$]", connectin);
        using(System.Data.OleDb.OleDbDataReader dr = command.ExecuteReader())
        {
            while (dr.Read)
            {
                if(dr.HasRows)
                {
                    Console.Write(dr[0].ToString() + " ");
                    Console.Write(dr[1].ToString() + " ");
                    Console.WriteLine(Convert.ToInt32(dr[2]));
                }
            } 
        }
    }
}
catch(Exception ex) 
{
    Console.WriteLine(ex.ToString()); 
}

Upvotes: 2

Related Questions