Click here to Skip to main content
11,923,046 members (63,068 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked

Export to EXCEL from Datatable in C#.Net

, 12 Mar 2012 CPOL
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

QuestionMy vote of 1 Pin
Maciej Los5-Aug-15 22:42
protectorMaciej Los5-Aug-15 22:42 
Questiontnx Pin
Member 1095825520-Jul-14 23:46
memberMember 1095825520-Jul-14 23:46 
QuestionMessage Automatically Removed Pin
6-Dec-13 6:35
membernilesh316-Dec-13 6:35 
SuggestionA free alternative, to create real .xlsx files, with one line of code Pin
MikeGledhill29-Nov-13 22:31
memberMikeGledhill29-Nov-13 22:31 
QuestionControl Content paceholder1 of type 'GridView' must be placed inside a form tag with runat=server.? Pin
U_subhu4u16-Sep-13 19:40
memberU_subhu4u16-Sep-13 19:40 
GeneralMy vote of 4 Pin
Alireza_13621-Aug-13 1:14
memberAlireza_13621-Aug-13 1:14 
GeneralMy vote of 1 Pin
arunk52518-Oct-12 3:54
memberarunk52518-Oct-12 3:54 
QuestionAuto Work Sheet in Work Book Pin
sarathkumarnallathambi10-Aug-12 19:17
membersarathkumarnallathambi10-Aug-12 19:17 
QuestionHi Pin
sarathkumarnallathambi3-Aug-12 21:48
membersarathkumarnallathambi3-Aug-12 21:48 
Generalvery nice post Pin
Vishal Jaiswal (J)1-Jul-12 22:27
memberVishal Jaiswal (J)1-Jul-12 22:27 
QuestionAn alternative method Pin
Mike Gledhill3-Apr-12 3:27
memberMike Gledhill3-Apr-12 3:27 
SuggestionFrom IQueryable to Excel! Pin
palpacec13-Mar-12 5:53
memberpalpacec13-Mar-12 5:53 
Hi friends
I've just made some adjustments to the code to make it more re-usable.
With my classes, you can export an IQueryable to Excel.
You can see how it works on this link
Please let me know if you like it!
Thank you

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.

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