Connecting to Excel From ASP.NET

I'm trying to connect to an excel sheet from ASP.NET. I'm battling to connect to it.
Please see the code attached and the errors (below) that I'm getting.

I would really appreciate any help you could offer!

Thanks

Errors:
 The Microsoft Jet database engine could not find the object 'Sheet1$'.  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 'Sheet1$'.  Make sure the object exists and that you spell its name and the path name correctly.

Source Error:

Line 51:         // Fill the DataSet with the information from the worksheet.
Line 52:
Line 53:         dataAdapt.Fill(ds, "AllStocks");
Line 54:         // Build a table from the original data.
Line 55:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Data.OleDb;


public partial class Portfolio : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=AllStocks.xls;Extended Properties=""Excel 8.0;HDR=YES;""";

        OleDbConnection connection = new OleDbConnection(excelConnectionString);

        connection.Open();   

        OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);

        OleDbDataAdapter dataAdapt = new OleDbDataAdapter();
   
        dataAdapt.SelectCommand = cmd;
        DataSet ds = new DataSet();
   
        dataAdapt.Fill(ds, "AllStocks");
   
        GridView3.DataSource = ds.Tables[0].DefaultView;
           connection.Close();


    }

Open in new window

richgnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Neil RussellTechnical Development LeadCommented:
I assume you do have a SHEET1$ tab?
0
richgnAuthor Commented:
I do have a Sheet1 tab.
0
kishoreb123Commented:
Please try to change
Data Source=AllStocks.xls "
to
Data Source=server.mappath("~/AllStocks.xls")
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

richgnAuthor Commented:
Hi Kishoreb123,

I changed the data source you recommended. The new line now looks like:
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=server.mappath("~/AllStocks.xls"); Extended Properties=""Excel 8.0;HDR=YES;""";

I am gettign an error for this part of that line:
~/AllStocks.xls"); Extended Properties=""Excel 8.0;HDR=YES;""";

I am being told that a ; is expected.
0
kishoreb123Commented:


String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
	"Data Source=" + Server.MapPath("~/AllStocks.xls") + ";" +
	"Extended Properties=Excel 8.0;";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
richgnAuthor Commented:
Thanks Kishoreb123,

I'm not getting an error for that. I am still getting an error when trying to run it. I have named a range of cells in excel 'MyRange'. The error message I am getting says that it is not picking up this range. I have the error message below and my latest code. I'm assuming that I can have the excel file accessed in the same folder as my ASP.NET project.

Here is the error message:
 The Microsoft Jet database engine could not find the object 'MyRange'.  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 'MyRange'.  Make sure the object exists and that you spell its name and the path name correctly.

Source Error:

Line 59:
Line 60:         // Fill the DataSet with the information from the worksheet.
Line 61:         objAdapter1.Fill(objDataset1, "MyRange");
Line 62:
Line 63:         // Bind data to DataGrid control.
protected void Page_Load(object sender, EventArgs e)
    {

        String excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" + Server.MapPath("~/AllStocks.xls") + ";" +
    "Extended Properties=Excel 8.0;";

        OleDbConnection objConn = new OleDbConnection(excelConn);

        objConn.Open();

        OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM MyRange", objConn);

        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

        objAdapter1.SelectCommand = objCmdSelect;

        DataSet objDataset1 = new DataSet();

        objAdapter1.Fill(objDataset1, "XLData");

        GridView3.DataSource = objDataset1.Tables[0].DefaultView;
        GridView3.DataBind();

        objConn.Close();

Open in new window

0
kishoreb123Commented:
i used the same coding what you have pasted. I'm able to retrieve data without any error.

Have u checked whether issue with any permissions on file..
2010-04-24-152843.gif
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.