Connecting Tech Pros Worldwide Help | Site Map

Problem Retrieving From Excel in C#

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 14 '09
Hi all, Im using the following codes to retrieve records from excel file.

OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0 ; Data Source=" + Path + "; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'");

OleDbCommand cmd = new OleDbCommand("SELECT TOP 10 * FROM [Sheet1$] WHERE [Project] <> ''", conn);

DataSet ds = new DataSet();
da = new OleDbDataAdapter(cmd);
da.Fill(ds);

DataTable table = ds.Tables[0];

In the excel file, there's a column storing values such as "IRAS 2009", "Nov-07" etc. When i retrieve from the DataTable it return me NULL and "31-Nov-07" respectively.

Why is it so? Why im unable to get the exact value and those in the excel file? Any help is greatly appreciated.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#2: Oct 14 '09

re: Problem Retrieving From Excel in C#


I'm unsure about why "IRAS 2009" goes to NULL, but I'm pretty sure the date is a formatting thing. If it's an actual date in excel, then yea, your data table is just giving you a different format, but it's still the same date.

You can either change it to a string in excel, or format the date on output differently. The latter would probably be the better choice ;)
Reply

Tags
csharp, datatable, excel, oledbdataadapter