.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to write a Data Table to EXCEL - COPY CELL-BY-CELL COPY Method

Posted By:Sajid P K       Posted Date: November 15, 2010    Points: 15    Category: ADO.NET    URL: http://www.dotnetspark.com  

This code segment Explains, How to Export data from Data table to Excel using .Net.

We all know that, Exporting data from a .NET application to Excel is a very common requirement.   Below is CEL - By - Cell copy method.   Due this cell by cell copying there is performance problem if copying data is huge.


Click Here  For Fastest Method.


public static void ExportToExcel(System.Data.DataTable dt, Worksheet excelSheet)
				// Copy the column names (cell-by-cell)
				for (int col = 0; col < dt.Columns.Count; col++) {
					excelSheet.Cells[1, col + 1] = dt.Columns[col].ColumnName;

				((Range) excelSheet.Rows[1, Type.Missing]).Font.Bold = true;

				// Copy the values (cell-by-cell)
				for (int col = 0; col < dt.Columns.Count; col++)
					for (int row = 0; row < dt.Rows.Count; row++)
						excelSheet.Cells[row + 2, col + 1] = dt.Rows[row].ItemArray[col];



Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend