I have a test web app. I am testing OLEdB connections under various trust levels. I built an app to test an OLEDB connection using the Microsoft.ACE.OLEDB.12.0 provider. I built the app on my development machine which has Office 2010 installed on it. I also built an excel file on that machine directly under the application root. I ran the web app in debug everything worked fine. I published the app and deployed it to the test web server directly under wwwroot. Since the web server does not have Office Installed on it I ran AccessDatabaseEngine_x64.exe on that machine. Then I ran the app. The App opens but when I push the button to run the code the read the file I get "The Microsoft Access database engine could not find the object 'Sheet1$'." This puzzling because the on sheet in the work book is Sheet1. Not to mention it worked just fine on the developer machine.
Here is the Page_Load code.
if (IsPostBack) { String cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/TestWebExcel.xlsx") + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\""; OleDbConnection cn = new OleDbConnection(cs); cn.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.CommandText = "Select PartNumber FROM [Sheet1$]"; cmd.CommandType = System.Data.CommandType.Text; cmd.Connection = cn; OleDbDataReader dr = cmd.ExecuteReader(); this.ListBox1.DataSource = dr; this.ListBox1.DataTextField = "PartNumber"; this.ListBox1.DataValueField = "PartNumber"; this.ListBox1.DataBind(); }
Here is the location of the Excel File C:\inetpub\wwwroot\TestWebAppExcelRedo\TestWebExcel.xlsx .
I rewrote the app to merely display the connection string being used. Like this:
if (IsPostBack) { String cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/TestWebExcel.xlsx") + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\""; OleDbConnection cn = new OleDbConnection(cs); cn.Open(); this.Label2.Text = cs; }
The result of this site shows the connection string as being
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\TestWebAppExcelRedo\TestWebExcel.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES"
This is the case even when Trust Level is set to Full, It is also the case when Trust Level is set to custom trust Level with OLEDB permission added.
But when the Web app project is moved to the serverwithout publishing it, then this error doesnot occur.
What's going on here?