Reputation: 47
I want to read excel files in C#. I dont want to use following things while reading:
Note: Am using vs 2005.
Please suggest me anything else>>
Thanks in advance, Sanjay
Upvotes: 0
Views: 7114
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