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

I'm trying to populate a xls sheet with data from Dataset
Following is my approach .

private void WriteExcel(DataTable dataTable)
       {
           string docupath = ConfigurationManager.AppSettings.Get("LMSFilePath");

           string filePath = MapPath(docupath);

           string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;";
               OleDbConnection connection = new OleDbConnection(connectionString);
               try
               {
                   connection.Open();
                   DataTable MyTable = dataTable;
                   string nonQuery = "INSERT INTO [Sheet1$] IN 'filePath' 'Excel 12.0;' SELECT * FROM [MyTable]";

                   //set up first query
                   OleDbCommand selectCommand = new OleDbCommand(nonQuery);
                   selectCommand.Connection = connection;


                   selectCommand.Transaction = connection.BeginTransaction();
                   selectCommand.ExecuteNonQuery();

                   selectCommand.Transaction.Commit();
                   connection.Dispose();
                   connection.Close();
               }

               catch (Exception)
               {

               }


I'm getting exception on line
selectCommand.ExecuteNonQuery();
as "
The Microsoft Jet database engine could not find the object 'MyTable'.  Make sure the object exists and that you spell its name and the path name correctly.
"

How to proceed.

Thanks in Advance.
Posted
Comments
Singh_Vinay 10-May-13 8:07am    
MyTable is the dataTable from Which I have to copy Data into sheet.
[no name] 10-May-13 8:29am    
Actually you are having the data in the datatable so u don't need a connection to oledb for exporting the data into excel sheet. Any data can be exported into excel sheet by the use of a stringwriter (sw). So please go through the link that I had shared in the answer section.

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