Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one application where i am reading data from excel. My excel is in Bin folder
n the code i have written
is
string excelFilePath = "sunny.xlsx";
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=\"Excel 12.0;HDR=NO\"";
            OleDbConnection excelOledbConObj = new OleDbConnection();
            excelOledbConObj.ConnectionString = connectionString;
            DataTable excelDataTable = new DataTable();
            
            OleDbCommand oledbCmdObj = new OleDbCommand();
            oledbCmdObj.Connection = excelOledbConObj;
            try
            {
                oledbCmdObj.CommandText = "SELECT * FROM [Sheet1$]";
                OleDbDataAdapter oldataAdapterObj = new OleDbDataAdapter(oledbCmdObj);
                
                oldataAdapterObj.Fill(excelDataTable);
            }
            catch (OleDbException ex)
            {
                Response.Write(ex.Message);
            
            }

but i am getting following error Message
The Microsoft Office Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.
In my excel i have Sheet1$. please where i am doing wrong.
Posted
Updated 5-May-11 0:35am
v2

//try to find out in the following way

 OleDbConnection Con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://Documents and Settings//Admin//My Documents//Downloads//MyReport2.xls;Extended Properties=Excel 8.0;");
            Con.Open();
            OleDbCommand Cmd = new OleDbCommand("Select * from [Sheet1$]", Con);
            OleDbDataAdapter dt = new OleDbDataAdapter(Cmd);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            Con.Close();


//Or you can try this 

OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Admin\Desktop\MyReport1.xls;Extended Properties=Excel 8.0;"
 
Share this answer
 
v2
If this is window application then

keep ur excel in debug folder 
and
correct ur path as
C#
string excelFilePath = ".\\sunny.xlsx";


If this is web application
then u can use Server.Mappath()
 
Share this answer
 
Comments
Hemant__Sharma 5-May-11 7:23am    
Right... i think this should work.
I think @Ashishmau suggestion should work.

to confirm you can try
excelOledbConObj.Open(); 

after setting connectionstring property.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900