Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 02, 2013 11:38 AM by arun banik
Feb 11, 2012 04:40 PM|LINK
Moderators, if this should go into another forum that is more appropriate, please feel free to place it there.
I've seen a lot of examples of how to export data into an Excel spreadsheet using ASP.NET and most have been wrong.
This code works and is very simple to follow.
This is my first contribution to the forums, enjoy...Paul
// these namespaces need to be added to your code behind file
public partial class Journal : System.Web.UI.Page
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySpotDBConnStr"].ConnectionString);
DataTable dt = new DataTable();
// regular page_load from .aspx file
protected void Page_Load(object sender, EventArgs e)
// added a button with ID=btnDownload and double clicked it's onclick event to auto create method
protected void btnDownload_Click(object sender, EventArgs e)
string queryStr = "SELECT * from table";
SqlDataAdapter sda = new SqlDataAdapter(queryStr, conn);
// this does all the work to export to excel
public void ExportTableData(DataTable dtdata)
string attach = "attachment;filename=journal.xls";
Response.ContentType = "application/ms-excel";
if (dtdata != null)
foreach (DataColumn dc in dtdata.Columns)
Response.Write(dc.ColumnName + "\t");
//sep = ";";
foreach (DataRow dr in dtdata.Rows)
for (int i = 0; i < dtdata.Columns.Count; i++)
Response.Write(dr[i].ToString() + "\t");
In IE9 the regular bar comes on down at the bottom asking to save or open, just click open and there it is
In FireFox that pesky download box comes on, then pops up a dialog, click open and there it is
in Opera it asks to save or open, click open and there it is.
Feb 12, 2012 08:49 AM|LINK
Feb 13, 2012 09:34 AM|LINK
Very appreciate you provide this wonderful codes, I try it and it works fine.
I suggest you write a article in your bolg next time. So if any one have this
problem we can inhert your bolg to him, that will help more pepole~
Anyway thanks for you contribution, good job.
Feb 14, 2012 01:58 AM|LINK
Thanks, I tried a few of the ones posted here and I couldn't get any of them to work. I took bits and pieces and and rewrote the code to actually work. I had done this about a decade ago back when i worked for ENRON--before they went bankrupt.
Feb 24, 2012 05:52 AM|LINK
Can we use this with out any database conection?
I actually need to convert a word document to excel which has text and tables in it..the number of word documents very, they all should come into one excel sheet. here the problem is the tables are not in database they are in my system in a folder..I just
need to copy the word table into the excel sheet with respect to their cell numbers.Please help me with this mann!!!
Thanks in adv!
May 30, 2012 06:58 AM|LINK
hope it wil help you
Jun 05, 2012 06:19 PM|LINK
I used closedXML to export data table to excel and it works fine :
Feb 02, 2013 11:38 AM|LINK
Really nice. Data can be transfered from source to another in various ways. "Microsoft.Office.Interop.Excel" gives us a way to export data to "Excel" and source. I am presenting a link which has an article on "Exporting data in Excel", which
allows us to export data into any version of excel.