Click here to Skip to main content
13,088,846 members (53,644 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 12 Mar 2012

Export to EXCEL from Datatable in C#.Net

, 12 Mar 2012
Rate this:
Please Sign up or sign in to vote.


Export the Datatable records to Excel sheet in 

Using the code 

This below function used to generate excel file based on datatable.

public void ExportToExcel(DataTable dt)
	if (dt.Rows.Count > 0)
		string filename = "DownloadMobileNoExcel.xls"; 
		System.IO.StringWriter tw = new System.IO.StringWriter();
		System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
		DataGrid dgGrid = new DataGrid();
		dgGrid.DataSource = dt;

		//Get the HTML for the control.
		//Write the HTML back to the browser.
		//Response.ContentType = application/;
		Response.ContentType = "application/";
		Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
		this.EnableViewState = false;

 Call the above function in button click event

protected void btnSave_Click(object sender, EventArgs e)
public override void VerifyRenderingInServerForm(Control control)





This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ramanujam Shankar
Web Developer Mahindra Logisoft Business Solution Limited, Chenn
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionAn alternative method Pin
Mike Gledhill3-Apr-12 2:27
memberMike Gledhill3-Apr-12 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 12 Mar 2012
Article Copyright 2012 by Ramanujam Shankar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid