Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code...

C#
constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", FilePath);
        Econ = new OleDbConnection(constr);

 ExcelConn(FilePath);
        Econ.Open();
        Query = string.Format("Select [Emp ID],[Emp Name],[Log Date],[LogTime],[Type] FROM [{0}]", "One Month Report$");
        OleDbCommand Ecom = new OleDbCommand(Query, Econ);
       

        DataSet ds = new DataSet();
        OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
        Econ.Close();
        oda.Fill(ds);
        DataTable Exceldt = ds.Tables[0];

        //creating object of SqlBulkCopy    
        SqlBulkCopy objbulk = new SqlBulkCopy(conn);
        //assigning Destination table name    
        objbulk.DestinationTableName = " Attendancetable";
        //Mapping Table column
        objbulk.ColumnMappings.Add("Emp ID", "Emp ID");
        objbulk.ColumnMappings.Add("Emp Name", "Emp Name");
        objbulk.ColumnMappings.Add("Log Date", "Log Date");
        objbulk.ColumnMappings.Add("LogTime", "LogTime");
        objbulk.ColumnMappings.Add("Type", "Type");
        //inserting Datatable Records to DataBase    
        conn.Open();
        objbulk.WriteToServer(Exceldt);
        conn.Close();


Using this code am getting "External table is not in expected formt" this error.
opened excel sheet uploading is successfull but in closed excel sheet file uploading process showing this error.
please anyone help me out.
Posted
Updated 31-Jul-15 20:21pm
v3

1 solution

C#
//
string HDR = hasHeaders ? "Yes" : "No";
           string constr ;
           if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
               constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
           else
               constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900