Connecting Tech Pros Worldwide Help | Site Map

Help for reading data from Excel file

Newbie
 
Join Date: Aug 2009
Posts: 3
#1: Aug 2 '09
hi,

I am trying to read data from an excel file. in one column data is like this:
1
1.1
1.1.1
1.1.2
1.2
1.2.1
1.2.2
and so on.

I use OleDbConnection to get data. my code is like this:
(1)
while (reader.Read())
{
string temp= reader[1].ToString();
}
as a result, it missed data: 1, 1.1, 1.2. (looks like these data are treated as double, not string)

(2)
when I use
temp = reader.GetString(1); // it gave me InvalidCastException

Then I tried to use datatable and it also gave me same problem.
So, how to read all those data and save them into string array?
I really appreciate if anyone can help me.

Thank you very much
Newbie
 
Join Date: Aug 2009
Posts: 3
#2: Aug 3 '09

re: Help for reading data from Excel file


Never mind my question. it is solved already. Thanks anyway
Newbie
 
Join Date: Sep 2007
Posts: 1
#3: Sep 30 '09

re: Help for reading data from Excel file


Hi, I'm trying to read data from Excel in VC++. Can you post your solution. Thanks in advance.
Newbie
 
Join Date: Aug 2009
Posts: 3
#4: Oct 1 '09

re: Help for reading data from Excel file


Hi, LalithaSJ,

if you have the same problem, just add something after connection string:
Example:
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
your excel file location+
";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");

Here,
HDR=Yes: First row of excel file will be treated as column name.
HDR=No: No column name. first row will be treated as data.
IMEX=1: All data in excel file will be treated as String.
IMEX=0: Datatype of each column will be collected based on excel file
column datatype setting

BTW, I am using C#. But I believe it will be pretty similar to C++. Hope it could give an idea to solve data reading problem.

Cheer.
Harbin2010
Reply