Here’s the code I’m using to show the Excel data on a Gridview:
if (FileUploadExcel.PostedFile.FileName.Contains(".xls"))
{
FileUploadExcel.SaveAs(Server.MapPath("~/Upload/") + "data.xls");
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/Upload/") + "data.xls;Extended Properties=Excel 8.0;";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand("Select ID FROM [Sheet1$]", connection);
DataTable raw = new DataTable();
using (System.Data.Common.DbDataReader dr = command.ExecuteReader())
{
raw.Load(dr);
}
gv.DataSource = raw;
gv.DataBind();
}
}
This is what happens… I have this in my data.xls file:
ID
A-001
3929
B-001
When I upload that, the Gridview displays only these:
ID
A-001
B-001
Is there anything wrong with the code? Thanks.
Try to add
IMEX=1to your connection string.It 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.
http://www.connectionstrings.com/excel