簡體   English   中英

無法在IIS上讀取excel文件

[英]Unable to read excel file on IIS

我想在我的asp.net網站上閱讀Excel表格。 文件由用戶上傳,然后我閱讀它並在網格視圖中顯示結果。 當我在Visual Studio環境中運行它時,每件事都可以正常工作。 但是當我在IIS上嘗試相同的代碼時,它會給出一個Exception 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應用程序 - >真

首先確保你有一個MS。 安裝Oledb驅動程序,然后嘗試此代碼

OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties=Excel 12.0;");
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
OleDbDataReader dr;
connection.Open();
dr = command.ExecuteReader(CommandBehavior.CloseConnection);

//dbo.Employees
cn.Open();

dt.Load(dr);
cn.Close();

我和OleDb提供商一起工作了幾次,如果你問我這真的太可怕了。 在我上一個需要使用Excel文件的項目中,我選擇了http://epplus.codeplex.com/這是一個托管庫,與OleDb-variant相比,它在速度和易用性方面都獲勝。 缺點是它只支持.xslx文件(不是.xsl),但是如果沒有它就可以生存,那么這是一個更好的方法。

請檢查您的權限和設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM