Accessing Excel data from WebForm
-
Hello All, I have a quick question!!! I have an excel file (.xls) with one sheet (Sheet1) with 4 columns, I’m trying to access the data in this file from an ASP.Net application, but no luck. Here is what I did so far
string conString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("ExcelData.xls") + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection con = new OleDbConnection(conString); con.Open(); OleDbDataAdapter da = new OleDbDataAdapter("Select * from ExcelData", conString); DataSet ds = new DataSet("ds"); da.Fill(ds);
When I try to run that I get the following error
The Microsoft Jet database engine could not find the object 'ExcelData'. Make sure the object exists and that you spell its name and the path name correctly. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine could not find the object 'ExcelData'. Make sure the object exists and that you spell its name and the path name correctly. Source Error: Line 55: OleDbDataAdapter da = new OleDbDataAdapter("Select * from [ExcelData]", conString); Line 56: DataSet ds = new DataSet("ds"); Line 57: da.Fill(ds); Line 58: /// Put the Data Grid Here. Line 59: con.Close();
So, I was wondering if any one have an idea on how to access the data from an excel sheet from an ASP.Net form.? Any help would be greatly appreciated. Thank you all in Advance.
-
Hello All, I have a quick question!!! I have an excel file (.xls) with one sheet (Sheet1) with 4 columns, I’m trying to access the data in this file from an ASP.Net application, but no luck. Here is what I did so far
string conString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("ExcelData.xls") + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection con = new OleDbConnection(conString); con.Open(); OleDbDataAdapter da = new OleDbDataAdapter("Select * from ExcelData", conString); DataSet ds = new DataSet("ds"); da.Fill(ds);
When I try to run that I get the following error
The Microsoft Jet database engine could not find the object 'ExcelData'. Make sure the object exists and that you spell its name and the path name correctly. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine could not find the object 'ExcelData'. Make sure the object exists and that you spell its name and the path name correctly. Source Error: Line 55: OleDbDataAdapter da = new OleDbDataAdapter("Select * from [ExcelData]", conString); Line 56: DataSet ds = new DataSet("ds"); Line 57: da.Fill(ds); Line 58: /// Put the Data Grid Here. Line 59: con.Close();
So, I was wondering if any one have an idea on how to access the data from an excel sheet from an ASP.Net form.? Any help would be greatly appreciated. Thank you all in Advance.
I think the error message is quite clear. The ExcelData is not a valid object that can be used in the query statement. For example, I want to read data from the 'Sheet1' sheet in the .xls file. The command text looks something like this:
string command = "SELECT * FROM [Sheet1$]";
Remember to use the $ after the object you make a reference.