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.

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

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.

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

privatevoid btnExportData_Click(object sender, EventArgs e)
{
SqlConnection Con = newSqlConnection("Data Source=DataSource Name;Initial Catalog=Dataabse-Name;Integrated Security=true");
Con.Open();
SqlDataAdapter Da = newSqlDataAdapter(txtQuery.Text, Con);
DataTable Tab = newDataTable();
Da.Fill(Tab);
dataGridView1.DataSource = Tab;
Con.Close();
}
Using above code we only retrieved the data from data table and show this data into datagridview.
Save Data Into Excel






Lionel MbelePosted Jun 15, 2023, 5:11 PM
Thank you buddy
Acep SetiawanPosted Feb 1, 2020, 10:48 PM
Why Datetime type error 0000-00-00?
shubham sahuPosted Aug 16, 2018, 11:29 AM
Ut is datagridview , what if it is datagrid
shubham sahuPosted Aug 16, 2018, 11:29 AM
It is datagridview what if it is datagrid
maryam abidPosted Aug 12, 2018, 3:06 AM
While saving to database it is giving error invalid column name
Geet PriyadarshiniPosted Mar 14, 2018, 2:18 AM
Whlie imoprting the file it showing path not found error
azhar hussainPosted Oct 31, 2017, 8:27 AM
Sir kindly provide this source code
Davronbek UmirovPosted Jun 16, 2016, 7:52 AM
nice article...
Ankit BansalPosted Feb 6, 2016, 11:37 AM
nice share..great use of spire..
Shashangka ShekharPosted Feb 5, 2016, 2:51 AM
Nice share, Thanks