我正在尝试读取我的 asp.net 网站中的 Excel 工作表。
文件由用户上传,然后我阅读它并在 GridView 中显示结果。
当我在 Visual Studio 环境中运行它时,一切正常。
但是当我在 IIS 上尝试相同的代码时,它给出了异常 Unspecified Error
。
我正在使用以下代码:
string excelConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+Server.MapPath("~/doc/")+ filepath+";Extended Properties="+ "\"" + "Excel 12.0;HDR=YES;" + "\"";
string query = "select * from [Sheet1$]";
OleDbConnection con = new OleDbConnection(connection);
con.Open(); // Exceptionoccurs here
OleDbCommand cmd = new OleDbCommand(query, con);
cmd.CommandType = CommandType.Text;
OleDbDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
testGrid.DataSource = dt;
testGrid.DataBind();
异常堆栈跟踪:
[OleDbException (0x80004005): Unspecified error]
System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) +351
System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) +86
System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +31
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +76
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
System.Data.OleDb.OleDbConnection.Open() +43
DataBaseSolution.DbSolution.select(String connection, String providername, String query) in F:\vs backup\DataBaseSolution\DataBaseSolution\DbSolution.cs:63
_Default.insertDb(String filepath) in c:\inetpub\wwwroot\test\Default.aspx.cs:82
_Default.checkButton_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\test\Default.aspx.cs:39
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
请建议我任何代码更新或任何其他我可以用来完成此任务的方法。
已编辑:提供了堆栈跟踪并用注释标记了发生异常的行
最佳答案
检查权限 -> 安全 或者 检查您的应用程序池 -> 高级设置 -> 启用 32 个应用程序 -> True
关于c# - 无法在 IIS 上读取 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12016152/