I’m importing data from an excel file and I just noticed that some cells are becoming ” ” after the import
Here’s the code I’m using
FileUploadExcel.SaveAs("C:\\datatop.xls");
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\1.xls;Extended Properties=Excel 8.0;";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand("Select MONTH, QTY FROM [Sheet1$]", connection);
DataTable tb = new DataTable();
using (System.Data.Common.DbDataReader dr = command.ExecuteReader())
{
tb.Load(dr);
}
gv.DataSource = tb;
gv.DataBind();
}
The column in question is QTY which contains:
12
14
15
11
19k/yr
4
2
It becomes a space on my gridview after the import. All other cells are displaying just fine on the gridview.
OUTPUT in GridView:
12
14
15
11
4
2
Any ideas?
When using OLEDB with Excel, the data type of a column is determined by the first few items in each column. In your case, the first few items in the column are numbers, so it assumes the column is of type
int. If you want the column to be considered text, you need to make sure you have some dummy rows at the top with data that ensures the right data types, then filter out those rows once the data is read into the data table. I know it’s a cluge, but it should work.Edit – Alternative: Using Excel Interop/COM to fill a DataTable
ComObjectManager – ensures that COM objects are properly disposed after being used