Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

C#
private void ExcelConn(string FilePath)
        {
            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);
       }


 private void connection()
        {
       sqlconn = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            con = new SqlConnection(sqlconn);
        }


 private void InsertExcelRecords(string FilePath)
        {
            ExcelConn(FilePath);
            Query = string.Format("select * FROM [{0}]", "Sheet1$");

            OleDbCommand Ecom = new OleDbCommand(Query, Econ);
            Econ.Open();

            DataSet ds = new DataSet();
            OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
            Econ.Close();
            oda.Fill(ds);
            DataTable Exceldt = ds.Tables[0];
            connection();
      
           SqlBulkCopy objbulk = new SqlBulkCopy(con);
           objbulk.DestinationTableName = "tblfarmer";
            
            objbulk.ColumnMappings.Add("farmerctscode", "farmerctscode");
            objbulk.ColumnMappings.Add("farmersapcode", "farmersapcode");
            objbulk.ColumnMappings.Add("farmerseason", "farmerseason");
            objbulk.ColumnMappings.Add("dateofjoinseco", "dateofjoinseco");
            objbulk.ColumnMappings.Add("governmentcode", "governmentcode");
            objbulk.ColumnMappings.Add("firstname", "firstname");
            objbulk.ColumnMappings.Add("lastname", "lastname");
            objbulk.ColumnMappings.Add("surname", "surname");
            objbulk.ColumnMappings.Add("gender", "gender");
            objbulk.ColumnMappings.Add("dateofbirth", "dateofbirth");
            objbulk.ColumnMappings.Add("birthlocation", "birthlocation");
            con.Open();
            objbulk.WriteToServer(Exceldt);
            con.Close();
        }

protected void btnimport_Click(object sender, EventArgs e)
        {
  
           string CurrentFilePath = string.Concat(Server.MapPath("~/masterdata/" + 
           FileUpload1.FileName));
             InsertExcelRecords(CurrentFilePath);
        }

when i run and select the file and upload, then error shows as follows

External table is not in the expected format.

how to solve this error "External table is not in the expected format".

What I have tried:

My code as follows

C#
private void ExcelConn(string FilePath)
        {
            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);
       }


 private void connection()
        {
       sqlconn = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            con = new SqlConnection(sqlconn);
        }


 private void InsertExcelRecords(string FilePath)
        {
            ExcelConn(FilePath);
            Query = string.Format("select * FROM [{0}]", "Sheet1$");

            OleDbCommand Ecom = new OleDbCommand(Query, Econ);
            Econ.Open();

            DataSet ds = new DataSet();
            OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
            Econ.Close();
            oda.Fill(ds);
            DataTable Exceldt = ds.Tables[0];
            connection();
      
           SqlBulkCopy objbulk = new SqlBulkCopy(con);
           objbulk.DestinationTableName = "tblfarmer";
            
            objbulk.ColumnMappings.Add("farmerctscode", "farmerctscode");
            objbulk.ColumnMappings.Add("farmersapcode", "farmersapcode");
            objbulk.ColumnMappings.Add("farmerseason", "farmerseason");
            objbulk.ColumnMappings.Add("dateofjoinseco", "dateofjoinseco");
            objbulk.ColumnMappings.Add("governmentcode", "governmentcode");
            objbulk.ColumnMappings.Add("firstname", "firstname");
            objbulk.ColumnMappings.Add("lastname", "lastname");
            objbulk.ColumnMappings.Add("surname", "surname");
            objbulk.ColumnMappings.Add("gender", "gender");
            objbulk.ColumnMappings.Add("dateofbirth", "dateofbirth");
            objbulk.ColumnMappings.Add("birthlocation", "birthlocation");
            con.Open();
            objbulk.WriteToServer(Exceldt);
            con.Close();
        }

protected void btnimport_Click(object sender, EventArgs e)
        {
  
           string CurrentFilePath = string.Concat(Server.MapPath("~/masterdata/" + 
           FileUpload1.FileName));
             InsertExcelRecords(CurrentFilePath);
        }


when i run and select the file and upload, then error shows as follows

External table is not in the expected format.

how to solve this error "External table is not in the expected format".
Posted
Updated 24-Aug-18 2:05am
v2
Comments
Shashank Laxman 24-Aug-18 5:39am    
Check the number of columns are matching in both table and file.
Also column name matters a lot hence should be same in both table and file

1 solution

Have you tried it using "Microsoft Interop service". Interop give you more flexibility over reading content from MS office application.
 
Share this answer
 

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