---
title: "How to Import or Export SQL Server table Data in MS-Excel Sheet Using C# Code"  
description: "In this blog I will show you how to export SQL Server table data in Excel sheet using c# code. Here I’m making application which import excels data in"  
author: "Anonymous User"  
published: 2012-04-10  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/294/how-to-import-or-export-sql-server-table-data-in-ms-excel-sheet-using-c-sharp-code  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to Import or Export SQL Server table Data in MS-Excel Sheet Using C# Code

In this blog I will show you how to export SQL [Server table](https://www.mindstick.com/forum/396/how-to-selecte-random-records-from-sql-server-table) [data in Excel](https://answers.mindstick.com/qa/96531/is-it-possible-to-make-pivot-table-using-multiple-sources-of-data-in-excel) sheet using c# code. Here I’m making application which import excels data in [data table](https://www.mindstick.com/forum/12752/how-to-select-specific-columns-from-a-data-table-in-vb-dot-net) and export [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) data into [excel sheet](https://www.mindstick.com/forum/23040/to-import-excel-sheet-into-sql-database) file.\

![How to Import or Export SQL Server table Data in MS-Excel Sheet Using C# Code](https://www.mindstick.com/blogs/de756e7d-85fd-4dd8-a0df-1be294bf064d/images/9a37eb15-a00a-4966-b077-26ea615a4824.jpg)

Here I’ve two buttons; Import and Export which are using to import and export data from Excel to SQL Server and SQL Server to Excel.![How to Import or Export SQL Server table Data in MS-Excel Sheet Using C# Code](file:///C:/Users/SACHIN~1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg)

##### Application Code:

```
private void btnImport_Click(object sender, EventArgs e)        {             // Create Data Table for MS-Office 2007 or 2003            System.Data.DataTable dtExcel = new System.Data.DataTable();            dtExcel.TableName = "MyExcelData";            string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Sachindra\Desktop\MyExcel2003.xls';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";            OleDbConnection con = new OleDbConnection(SourceConstr);            string query = "Select * from [Sheet1$]";            OleDbDataAdapter data = new OleDbDataAdapter(query, con);            data.Fill(dtExcel);             MessageBox.Show("Data Imported Successfully into DataTable");        }         private void btnExport_Click(object sender, EventArgs e)        {            // Create sql connection string              string conString = @"Data Source =  XXXX ; Initial Catalog = XXXX; User Id = XXXX; Password = XXXX;";            SqlConnection sqlCon = new SqlConnection(conString);            sqlCon.Open();             SqlDataAdapter da = new SqlDataAdapter("select * from tblTest", sqlCon);            System.Data.DataTable dtMainSQLData = new System.Data.DataTable();            da.Fill(dtMainSQLData);            DataColumnCollection dcCollection = dtMainSQLData.Columns;             // Export Data into EXCEL Sheet             Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();             ExcelApp.Application.Workbooks.Add(Type.Missing);             // ExcelApp.Cells.CopyFromRecordset(objRS);            for (int i = 1; i < dtMainSQLData.Rows.Count + 1; i++)            {                 for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)                {                    if (i == 1)                        ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();                    else                        ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 1][j - 1].ToString();                 }             }             ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\Sachindra\\Desktop\\test.xls");             ExcelApp.ActiveWorkbook.Saved = true;             ExcelApp.Quit();             MessageBox.Show("Data Exported Successfully into Excel File");        }     }
```

So with the help of this application you could import [data in SQL](https://www.mindstick.com/articles/12937/how-to-import-excel-data-in-sql-server-2014) Server data table from Excel Sheet and export [data from SQL](https://www.mindstick.com/forum/12885/how-to-retrieving-data-from-sql-server-and-adding-it-to-arraylist) Server table to Excel Sheet using C# code. Thanks for reading this article.

If you’re using any third party tool to perform this task or something like this task, there is one famous tool MindStick DataConverter which provides [import export](https://www.mindstick.com/products/import-export) functionality. It is a free charge too so you can easily download it from here.

---

Original Source: https://www.mindstick.com/blog/294/how-to-import-or-export-sql-server-table-data-in-ms-excel-sheet-using-c-sharp-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
