本文介绍了将Excel数据导出到SQL Server表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的代码如下My code as followsprivate 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 followsExternal 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 followsprivate 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 followsExternal table is not in the expected format.how to solve this error "External table is not in the expected format".推荐答案 当我运行并选择文件并上传时,错误显示如下 外部表格不是预期的格式。 如何解决此错误外部表格不符合预期格式。 我的尝试: 我的代码如下when i run and select the file and upload, then error shows as followsExternal 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 followsprivate 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 当我运行并选择文件并上传时,错误显示如下 外部表格不是预期格式。 如何解决此错误外部表格不符合预期格式。when i run and select the file and upload, then error shows as followsExternal table is not in the expected format.how to solve this error "External table is not in the expected format". 这篇关于将Excel数据导出到SQL Server表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 12:16
查看更多