Import and Export Data From Excel to Database

Import and Export of data from excel to database is always a hot issues for developers. There are many libraries using that we can implement these task. Today I will explain about “Spire.xls” libraries, using this we can easily import data from excel to database and export data from database to excel. Now I will explain using some simple steps.

Step 1: Go to File option and select a project. Here I selected a “Window Form Application”. Now right click on project and select the “Manage NuGet Packages” option. Now a window will popup, write “spire” in search box and select “Spire.Office. for .NET” package and install this package.

Window Form Application

Now go to Reference Folder and you will find following “dll” file has been added.

reference

Step 2: We create a window form as below. In this form we take a Datagridview in which we will show the data. We divided this form mainly in two part. In first part we will export the data from database to excel and using second section we will import the data from excel to database.

data from excel to database

Step 3: Export Data from Database to Excel.

We will export the data from below table to excel sheet using the C# code.

Export Data from Database to Excel

Export Data From Database


  1. privatevoid btnExportData_Click(object sender, EventArgs e)    
  2. {    
  3.     SqlConnection Con = newSqlConnection("Data Source=DataSource Name;Initial Catalog=Dataabse-Name;Integrated Security=true");    
  4.     Con.Open();    
  5.     SqlDataAdapter Da = newSqlDataAdapter(txtQuery.Text, Con);    
  6.     DataTable Tab = newDataTable();    
  7.     Da.Fill(Tab);    
  8.     dataGridView1.DataSource = Tab;    
  9.     Con.Close();    
  10. }  

 

Using above code we only retrieved the data from data table and show this data into datagridview.

Save Data Into Excel:
  1. privatevoid btnSaveFile_Click(object sender, EventArgs e)    
  2. {    
  3.     string fileName;    
  4.     Spire.DataExport.XLS.CellExport cellExport = new Spire.DataExport.XLS.CellExport();    
  5.     Spire.DataExport.XLS.WorkSheet worksheet1 = new Spire.DataExport.XLS.WorkSheet();    
  6.     worksheet1.DataSource = Spire.DataExport.Common.ExportSource.DataTable;    
  7.     worksheet1.DataTable = this.dataGridView1.DataSource asDataTable;    
  8.     worksheet1.StartDataCol = ((System.Byte)(0));    
  9.     cellExport.Sheets.Add(worksheet1);    
  10.     cellExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;    
  11.     fileName = txtFileName.Text.ToString() + ".xls";    
  12.     cellExport.SaveToFile(fileName);    
  13. }   
Using above code we read the data from Datagridview and save into excel sheet. We can see in below image that complete data is inserted into excel sheet.

Excel

Step 4: Import Data From Excel to Database:

Now I will explain how to import data from excel sheet to database. I will use below “Employee.xls” file and import the data of this file into data table.

Employee.xls

We have already a table in database in database that is empty in current.

database

Import Data From Excel To DataGridview

Import Data From Excel To DataGridview
  1. privatevoid btnImport_Click(object sender, EventArgs e)    
  2. {    
  3.     string fileName;    
  4.     fileName = txtExcelToDatabase.Text.ToString();    
  5.     Workbook workbook = newWorkbook();    
  6.     workbook.LoadFromFile(fileName);    
  7.     Worksheet sheet = workbook.Worksheets[0];    
  8.     this.dataGridView1.DataSource = sheet.ExportDataTable();    
  9. }   
Using above code we retrieved the data from excel sheet and display into DataGridView.

Save Data Into Database
  1. privatevoid btnsaveTodatabase_Click(object sender, EventArgs e)    
  2. {    
  3.     SqlConnection Con = newSqlConnection("Data Source=Data-Source;Initial Catalog=Database-Name;Integrated Security=true");    
  4.     SqlCommand com;    
  5.     string str;    
  6.     Con.Open();    
  7.     for (int index = 0; index < dataGridView1.Rows.Count - 1; index++)    
  8.     {    
  9.         str = @ "Insert Into Employee(Emp_Id,Emp_Name,Manager_Id,Project_Id) Values(" + dataGridView1.Rows[index].Cells[0].Value.ToString() + ", '" + dataGridView1.Rows[index].Cells[1].Value.ToString() + "'," + dataGridView1.Rows[index].Cells[2].Value.ToString() + "," + dataGridView1.Rows[index].Cells[3].Value.ToString() + ")";    
  10.         com = newSqlCommand(str, Con);    
  11.         com.ExecuteNonQuery();    
  12.     }    
  13.     Con.Close();    
  14. }   
Using above code we retrieved data from Datagridview as row by row and inserted this data into “Employee” table. Now if we examine the “Employee” table , we will find that data from excel has been inserted into table.

Employee table

Using Spire.Office library we can easily export data from database to excel sheet and also import data from excel sheet to database just using some line of code.