Click here to Skip to main content
14,668,615 members
Articles » Web Development » ASP.NET » General
Posted 12 Mar 2012


20 bookmarked

Export to EXCEL from Datatable in C#.Net

Rate this:
2.91 (12 votes)
Please Sign up or sign in to vote.
2.91 (12 votes)
12 Mar 2012CPOL


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

Comments and Discussions

QuestionTnks! Pin
cmercadoloayza5-Nov-17 14:51
Membercmercadoloayza5-Nov-17 14:51 
QuestionMy vote of 1 Pin
Maciej Los5-Aug-15 21:42
mveMaciej Los5-Aug-15 21:42 
Questiontnx Pin
ShahinFasihi20-Jul-14 22:46
MemberShahinFasihi20-Jul-14 22:46 
SuggestionA free alternative, to create real .xlsx files, with one line of code Pin
Michael Gledhill29-Nov-13 21:31
MemberMichael Gledhill29-Nov-13 21:31 
QuestionControl Content paceholder1 of type 'GridView' must be placed inside a form tag with runat=server.? Pin
U_subhu4u16-Sep-13 18:40
MemberU_subhu4u16-Sep-13 18:40 
GeneralMy vote of 4 Pin
Alireza_13621-Aug-13 0:14
MemberAlireza_13621-Aug-13 0:14 
GeneralMy vote of 1 Pin
arunk52518-Oct-12 2:54
Memberarunk52518-Oct-12 2:54 
QuestionAuto Work Sheet in Work Book Pin
Sarathkumar Nallathamby10-Aug-12 18:17
professionalSarathkumar Nallathamby10-Aug-12 18:17 
QuestionHi Pin
Sarathkumar Nallathamby3-Aug-12 20:48
professionalSarathkumar Nallathamby3-Aug-12 20:48 
Generalvery nice post Pin
Vishal Jaiswal (J)1-Jul-12 21:27
MemberVishal Jaiswal (J)1-Jul-12 21:27 
QuestionAn alternative method Pin
Mike Gledhill3-Apr-12 2:27
MemberMike Gledhill3-Apr-12 2:27 
SuggestionFrom IQueryable to Excel! Pin
davethewave197713-Mar-12 4:53
professionaldavethewave197713-Mar-12 4:53 

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.