Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to insert an table data from excel to sql server but facing the following problem..

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.
C#
Line 68:             OleDbCommand oledbcmd = new OleDbCommand(exceldataquery, oledb);
Line 69:             oledb.Open();
Line 70:             OleDbDataReader dr = oledbcmd.ExecuteReader();
Line 71:             SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlconnectionstring);
Line 72:             bulkcopy.DestinationTableName = sqltable;



My Code

C#
protected void importdatafromexcel(string filepath)
       {
           string sqltable = "PFDummyExcel";
           string exceldataquery = "select EmployeeId,EmployeeName,Amount from [Sheet1$]";
           string excelconnectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0;Persist Security Info=False";
           string sqlconnectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["HRGold"].ConnectionString;
           SqlConnection con = new SqlConnection(sqlconnectionstring);
           OleDbConnection oledb = new OleDbConnection(excelconnectionstring);
           OleDbCommand oledbcmd = new OleDbCommand(exceldataquery, oledb);
           oledb.Open();
           OleDbDataReader dr = oledbcmd.ExecuteReader();
           SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlconnectionstring);
           bulkcopy.DestinationTableName = sqltable;
           while (dr.Read())
           {
               bulkcopy.WriteToServer(dr);
           }
           oledb.Close();
       }


Please tell me how will i solve this..
Posted
Comments
Ganesh Raja 6-Dec-13 1:48am    
Have u verified the path(try to manually find the file using the path though explorer) and sheet name ?(xls or xlsx)
ajays3356 6-Dec-13 2:24am    
sheet name is xlsx.Yes i have tried to namually giving the path of excelsheet but still facing the same problem.

1 solution

Can you verify the below links:

http://www.codeproject.com/Tips/636719/Import-MS-Excel-data-to-SQL-Server-table-using-Csh

http://www.aspdotnet-suresh.com/2012/12/c-importupload-data-from-excel-to-sql.html[^]
 
Share this answer
 

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