繁体   English   中英

IErrorInfo.GetDescription在asp.net c#中的E_FAIL(0x80004005)失败了?

[英]IErrorInfo.GetDescription failed with E_FAIL(0x80004005) in asp.net c#?

我正在尝试读取一个excel文件并将该文件的内容转换为数据表但我不断收到此异常IErrorInfo.GetDescription失败,E_FAIL(0x80004005)指向一个特定的行POCCommand.Fill(dt); 这是我到目前为止所尝试的。 我能做错什么?

string POCpath = @"p.xlsx";
 string POCConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + POCpath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";

    OleDbConnection POCcon = new OleDbConnection(POCConnection);
    OleDbCommand POCcommand = new OleDbCommand();
    DataTable dt = new DataTable();
    OleDbDataAdapter POCCommand = new OleDbDataAdapter("select * from [Sheet1$] ", POCcon);
    POCCommand.Fill(dt);
    Console.WriteLine(dt.Rows.Count);

首先,由于您使用的是.NET,因此需要将从IDisposable继承或实现的任何内容包装到using语句中。

如:

using (OleDbConnection connection = new OleDbConnection("connectionstring")){
    //Do stuff here
}

第二:请参阅MethodMan关于此SO答案的这个好答案。 它拥有您需要知道的一切。 他同时执行OleDb连接和TextFieldParser,这将更快,更便宜。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM