Home » excel » c# – White spaces and oledb

c# – White spaces and oledb

Posted by: admin May 14, 2020 Leave a comment


I’m reading an excel file using OleDb on ASP. NET (C#).
All the information is returned ok and I was surprised to see that even the cell type defined on the Excel file is returned to my code.
The problem is that I have column where all the cells “general” types and since the values are only numbers Excel assumes it to be Number.
If there are no spaces olebd driver returns the right value to my code but if there are space it returns “”…

Here’s how I’m getting the information:

OleDbConnection oleDbConn = new OleDbConnection(connString);
OleDbCommand oleDbComm = new OleDbCommand("SELECT * FROM [Sheet1$]", oleDbConn);
OleDbDataAdapter oleDbDtAdapter = new OleDbDataAdapter();
oleDbDtAdapter.SelectCommand = oleDbComm;

DataSet dtSet = new DataSet();
oleDbDtAdapter.Fill(dtSet, "SMSs");
Object testZeroZero = dtSet.Tables[0].Rows[0][0];

I can’t go to the Excel and change the cell type to “text” because the end user must not worry on changing this so how can I overcome this?


How to&Answers:

Have you considered your connection string?

“IMEX=1;” tells the driver to always
read “intermixed” (numbers, dates,
strings etc) data columns as text.
Note that this option might affect
excel sheet write access negative.