My requirement is to read a excel file from a local folder and import into DataTable. Importing work fine if the first row contains string value, If the value is int in first 3 rows the datatype of column becomes Integer and it is ignoring string values. I want to read a the values. I tried to insert a row of string values, but due to int datatype its not allowing. Plz help.. i am in a big trouble. I Tried with IMEX=1 in connection string, but no go
string Extension = ".xlsx";
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
conStr = String.Format(conStr, strPath);
oledbConn = new OleDbConnection(conStr);
if (oledbConn.State != ConnectionState.Open)
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand(); ;
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataTable dt = new DataTable();
dt.Columns.Add("Store", typeof(string)).DefaultValue = strPath.Substring(18, 3);
var sheets = oledbConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
cmd.Connection = oledbConn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = " SELECT * FROM [" + sheets.Rows[0]["TABLE_NAME"].ToString() + "] ";
oleda = new OleDbDataAdapter(cmd);
//oleda.FillSchema(dt, SchemaType.Source);
dt.TableName = strPath.Substring(18, 3);
oleda.Fill(dt);