Insert Data Into Excel Using OLEDB In Windows Application Using C#

Today, I am going to explain how to insert a data into Excel, using OLEDB in Windows Application and how to display the data, using DataGridView.

Before we begin, let us check the following prerequisites.

Step 1

Add "Microsoft.Office.Interpol.Excel" references to your project, basically, it is the .DLL file located at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15



Step 2

Add the mentioned namespace given below.
  1. using System.Data.OleDb;  
  2. using System.IO;  
  3. using Excel = Microsoft.Office.Interop.Excel;    
Now, in my scenario, I am just going to enter four textbox values into an Excel, so I have created four textboxes with label names and one DataGridView to display the entered items, as shown below.

 

Create one Excel file with the header and add this Excel file in your project, as shown shown below.
 
 

Now, it's time to write button event coding to add the item into an Excel file; just double click the button and write the code, mentioned below.
  1. // Declare the File name    
  2. string filename = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\Detail.xlsx";  
  3. // Connection String // Here i am using HDR = YES which means excel first row consider as header  
  4. string con = String.Format(@ "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=0;READONLY=FALSE'", filename);  
  5. using(OleDbConnection cn = new OleDbConnection(con)) {  
  6.     cn.Open();  
  7.     // Insert the textbox value to the excel using insert Query   
  8.     OleDbCommand cmd1 = new OleDbCommand("INSERT INTO [Sheet1$] " + "([Name],[Age],[Emp ID],[Designation]) " + "VALUES(@value1,@value2,@value3,@values4)", cn);  
  9.     cmd1.Parameters.AddWithValue("@value1", textBox1.Text);  
  10.     cmd1.Parameters.AddWithValue("@value2", textBox2.Text);  
  11.     cmd1.Parameters.AddWithValue("@value3", textBox3.Text);  
  12.     cmd1.Parameters.AddWithValue("@value4", textBox4.Text);  
  13.     cmd1.ExecuteNonQuery();  
  14.     // Get the entered value using  OleDbAdapter  
  15.     System.Data.OleDb.OleDbDataAdapter cmd2;  
  16.     cmd2 = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", con);  
  17.     // copy the Excel value to the DataSet   
  18.     DataSet ds = new System.Data.DataSet();  
  19.     cmd2.Fill(ds);  
  20.     // Finally display the entered Item using dataGriedView   
  21.     dataGridView1.DataSource = ds.Tables[0];  
  22. }  
Just execute the program and enter the textbox value, click the Add button and you will see the result, as shown below.

 

If you get the error mentioned below, while executing the program, just right click an Excel file and go to Properties.

 

Just change the Build Action from None to Content and change the option copy to output directory to always copy and re-excute the program and you will get the expected output.

 
Ebook Download
View all
Learn
View all