使用c#.net将数据从excel文件导入数据库时​​出错

时间:2015-08-01 05:13:14

标签: c# asp.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();

    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();

在打开的Excel工作表上传成功,但在关闭的Excel工作表文件上传过程中显示此错误:

  

外部表格不在预期的格式中。

0 个答案:

没有答案