IErrorInfo.GetDescription com E_FAIL(0x80004005)导入EXCEL文件

时间:2019-05-03 13:31:01

标签: c# html sql asp.net excel

每当我从IIS Express文件夹中的Excel文件导入数据时,一切都会顺利进行并且没有错误,并且Excel文件会转到我在SQL中创建的表中,但是当Excel文件位于另一个位置时,它将给那个错误...为什么?

这是我的代码:

protected void Upload_Click(object sender, EventArgs e)
{
    string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(excelPath);

    string filepath = FileUpload1.PostedFile.FileName;
    string filename = Path.GetFileName(filepath);

    string ext = Path.GetExtension(filename);
    String strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";

    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

    OleDbCommand cmd = new OleDbCommand("Select [Nome],[Cidade],[Idade] from [Folha1$]", excelConnection);

    excelConnection.Open();
    cmd.ExecuteNonQuery();


    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter("Select [Nome],[Cidade],[Idade] from [Folha1$]", strConnection);

    OleDbDataReader dReader;
    dReader = cmd.ExecuteReader();
    SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);

    sqlBulk.DestinationTableName = "Teste";
    sqlBulk.WriteToServer(dReader);
    excelConnection.Close();

}

错误在线:

 excelConnection.Open();

0 个答案:

没有答案