本文介绍了使用c#.net将数据从excel文件导入数据库时​​出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是我的代码... 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(); 查询= 字符串 .Format( 选择[Emp ID],[Emp Name],[Log Date],[LogTime],[Type] FROM [{0}], 一个月报告$); 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 ]; // 创建SqlBulkCopy的对象 SqlBulkCopy的objbulk = new SqlBulkCopy(conn); // 分配目的地表名称 objbulk.DestinationTableName = attendancetable; // 映射表列 objbulk.ColumnMappings.Add( Emp ID, Emp ID); objbulk.ColumnMappings.Add( Emp Name, Emp Name); objbulk.ColumnMappings.Add( Log Date, 日志日期); objbulk.ColumnMappings.Add( LogTime, LogTime); objbulk.ColumnMappings.Add( 输入, 输入); // 将数据表记录插入数据库 conn.Open(); objbulk.WriteToServer(Exceldt); conn.Close(); 使用此代码得到外部表不在预期的表格中这个错误。 打开excel表格上传成功但在封闭的Excel工作表文件上传过程中显示此错误。 请大家帮帮我。解决方案 ); 的OleDbCommand是Ecom = 新的OleDbCommand(查询,经济); 的DataSet DS = 新数据集(); OleDbDataAdapter的ODA = 新 OleDbDataAdapter的(查询,经济); Econ.Close(); oda.Fill(DS); 的DataTable Exceldt = ds.Tables [ 0 ]; // 创建SqlBulkCopy的对象 SqlBulkCopy objbulk = new SqlBulkCopy (conn); // 分配目的地表名称 objbulk.DestinationTableName = attendancetable; // 映射表列 objbulk.ColumnMappings.Add( Emp ID, Emp ID); objbulk.ColumnMappings.Add( Emp Name, Emp Name); objbulk.ColumnMappings.Add( Log Date, 日志日期); objbulk.ColumnMappings.Add( LogTime, LogTime); objbulk.ColumnMappings.Add( 输入, 输入); // 将数据表记录插入数据库 conn.Open(); objbulk.WriteToServer(Exceldt); conn.Close(); 使用此代码得到外部表不在预期的表格中这个错误。 打开excel表格上传成功,但在封闭的Excel工作表文件上传过程中显示此错误。 请有人帮帮我。 // 字符串 HDR = hasHeaders? 是: 否; string constr; 如果(FileName.Substring(FileName.LastIndexOf(' 。))。ToLower()== 。xlsx) constr = Provider = Microsoft.ACE.OLEDB.12.0; Data Source = + FileName + ;扩展属性= \Excel 12.0; HDR = + HDR + ; IMEX = 0 \; else constr = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = + FileName + ;扩展属性= \Excel 8.0; HDR = + HDR + ; IMEX = 0 \; Here is my code...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. 解决方案 "); 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.//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\""; 这篇关于使用c#.net将数据从excel文件导入数据库时​​出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 17:06