---
title: "How to write excel file from datatable in c#"  
description: "How to write excel file from datatable in c#"  
author: "Pravesh Singh"  
published: 2012-01-10  
updated: 2019-04-03  
canonical: https://www.mindstick.com/forum/374/how-to-write-excel-file-from-datatable-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to write excel file from datatable in c#

Hello Everyone,

Can [anyone tell me](https://www.mindstick.com/interview/23033/can-anyone-tell-me-about-hadoop-toolbox) how to write [excel](https://www.mindstick.com/articles/12937/how-to-import-excel-data-in-sql-server-2014) [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) from [datatable in C#](https://www.mindstick.com/forum/12872/getting-count-of-all-repeated-rows-in-a-datatable-in-c-sharp).Net. Please [resolve](https://www.mindstick.com/forum/159427/windows-app-dll-not-found-resolve-library-issue) my [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) as soon as possible.

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!!

## Replies

### Reply by bear pu

Post is removed by the Admin.

### Reply by garry macin

You can turn a DataTable into an Excel worksheet with some very readable code:

XLWorkbook wb = new XLWorkbook();

DataTable dt = GetDataTableOrWhatever();

wb.Worksheets.Add(dt,"WorksheetName");

### Reply by Richard Boolman

Hi,You can use this Excel .NET component to write excel file from a datatable.The C# code below shows how to do this:\

```
DataTable dt = new DataTable();ExcelWorkbook Wbook = new ExcelWorkbook();Wbook.Worksheets.Add("WSheet1").ReadFromDataTable(dt);Wbook.WriteXLS(@"c:\output.xls");
```

### Reply by robert galp

You can use OLEDB

http://csharp.net-informations.com/excel/csharp-excel-oledb.htm

rogal.

### Reply by Anonymous User

Hello Pravesh Singh,\
\
Check this code...\
\
// Create sql connection string \
\
string conString = @"Data Source = ServerName ; Initial Catalog = MyDatabase; User Id = XXXXXX ; Password = XXXXXX;";\
\
SqlConnection sqlCon = new SqlConnection(conString);\
\
sqlCon.Open();\
\
// Fill data into MyTable\
\
SqlDataAdapter da = new SqlDataAdapter("select * from MyTableName", sqlCon);\
\
System.Data.[DataTable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) 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);\
\
// Write data into excel sheet cells

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();\
\
}\
\
}\
\
// save excel file \
\
ExcelApp.ActiveWorkbook.SaveCopyAs("SaveAsFilePath");\
\
ExcelApp.ActiveWorkbook.Saved = true;\
\
ExcelApp.Quit();\
\
It might be resolve your problem.\


---

Original Source: https://www.mindstick.com/forum/374/how-to-write-excel-file-from-datatable-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
