This is how to export data from ASP.Net database into Excelhttp://forums.asp.net/t/1768549.aspx/1?This+is+how+to+export+data+from+ASP+Net+database+into+ExcelSat, 02 Feb 2013 11:38:16 -050017685494827740http://forums.asp.net/p/1768549/4827740.aspx/1?This+is+how+to+export+data+from+ASP+Net+database+into+ExcelThis is how to export data from ASP.Net database into Excel <p>Moderators, if this should go into another forum that is more appropriate, please feel free to place it there.</p> <p>I've seen a lot of examples of how to export data into an Excel spreadsheet using ASP.NET and most have been wrong.<br> This code works and is very simple to follow.<br> This is my first contribution to the forums, enjoy...Paul</p> <p>// these namespaces need to be added to your code behind file<br> using System.Configuration;<br> using System.Data.SqlClient;<br> using System.Data;</p> <p>namespace MySpot.UserPages<br> {<br> &nbsp;&nbsp;&nbsp; public partial class Journal : System.Web.UI.Page<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;MySpotDBConnStr&quot;].ConnectionString);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = new DataTable();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // regular page_load from .aspx file<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void Page_Load(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!IsPostBack)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // added a button with ID=btnDownload and double clicked it's onclick event to auto create method<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void btnDownload_Click(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string queryStr = &quot;SELECT * from table&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter sda = new SqlDataAdapter(queryStr, conn);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sda.Fill(dt);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ExportTableData(dt);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this does all the work to export to excel<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void ExportTableData(DataTable dtdata)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string attach = &quot;attachment;filename=journal.xls&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.ClearContent();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.AddHeader(&quot;content-disposition&quot;, attach);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.ContentType = &quot;application/ms-excel&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (dtdata != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (DataColumn dc in dtdata.Columns)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(dc.ColumnName &#43; &quot;\t&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //sep = &quot;;&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(System.Environment.NewLine);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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;&nbsp;&nbsp; Response.End();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; }<br> }</p> <p>In IE9 the regular bar comes on down at the bottom asking to save or open, just click open and there it is<br> In FireFox that pesky download box comes on, then pops up a dialog, click open and there it is<br> in Opera it asks to save or open, click open and there it is.</p> 2012-02-11T16:40:05-05:004828217http://forums.asp.net/p/1768549/4828217.aspx/1?Re+This+is+how+to+export+data+from+ASP+Net+database+into+ExcelRe: This is how to export data from ASP.Net database into Excel <p>http://blogs.msdn.com/b/erikaehrli/archive/2009/01/30/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx</p> <p>http://weblogs.asp.net/gunnarpeipman/archive/2007/09/16/exporting-gridview-data-to-excel.aspx</p> 2012-02-12T08:49:11-05:004829442http://forums.asp.net/p/1768549/4829442.aspx/1?Re+This+is+how+to+export+data+from+ASP+Net+database+into+ExcelRe: This is how to export data from ASP.Net database into Excel <p>Hi</p> <p>Very appreciate you provide this&nbsp;wonderful codes, I try it and it works fine.</p> <p>I suggest you write a article in your bolg next time. So if any one have this</p> <p>problem we can inhert your bolg to him, that will help more pepole~</p> <p>Anyway thanks for you&nbsp;<span>contribution, good job.</span></p> <p><span>Dino</span></p> 2012-02-13T09:34:16-05:004830631http://forums.asp.net/p/1768549/4830631.aspx/1?Re+This+is+how+to+export+data+from+ASP+Net+database+into+ExcelRe: This is how to export data from ASP.Net database into Excel <p>Thanks, I tried a few of the ones posted here and I couldn't get any of them to work.&nbsp; I took bits and pieces and and rewrote the code to actually work.&nbsp; I had done this about a decade ago back when i worked for ENRON--before they went bankrupt.</p> <p>Paul</p> 2012-02-14T01:58:09-05:004848537http://forums.asp.net/p/1768549/4848537.aspx/1?Re+This+is+how+to+export+data+from+ASP+Net+database+into+ExcelRe: This is how to export data from ASP.Net database into Excel <p>Can we use this with out any database conection?</p> <p>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!!!</p> <p>Thanks in adv!</p> 2012-02-24T05:52:21-05:005003298http://forums.asp.net/p/1768549/5003298.aspx/1?Re+This+is+how+to+export+data+from+ASP+Net+database+into+ExcelRe: This is how to export data from ASP.Net database into Excel <p>hi</p> <p>hope it wil help you</p> <p><a href="http://www.aspdotnet-sharepoint.com/2011/11/export-data-to-excel-in-c.html" rel="nofollow" target="_blank">http://www.aspdotnet-sharepoint.com/2011/11/export-<wbr>data-to-excel-in-c.html</a></p> 2012-05-30T06:58:30-04:005012528http://forums.asp.net/p/1768549/5012528.aspx/1?Re+This+is+how+to+export+data+from+ASP+Net+database+into+ExcelRe: This is how to export data from ASP.Net database into Excel <p>I used closedXML to export data table to excel and it works fine :</p> <p>http://closedxml.codeplex.com/</p> <p></p> 2012-06-05T18:19:46-04:005289883http://forums.asp.net/p/1768549/5289883.aspx/1?Re+This+is+how+to+export+data+from+ASP+Net+database+into+ExcelRe: This is how to export data from ASP.Net database into Excel <p>Really nice. Data can be transfered from source to another in various ways. &quot;<strong>Microsoft.Office.Interop.Excel&quot;&nbsp;</strong>gives us a way to export data to &quot;Excel&quot; and source. I am presenting a link which has an article on &quot;Exporting data in Excel&quot;, which allows us to export data into any version of excel.&nbsp;</p> <p><a href="http://www.encodedna.com/2013/01/asp.net-export-to-excel.htm" title="Export to Excel in Asp.Net">http://www.encodedna.com/2013/01/asp.net-export-to-excel.htm</a></p> <p>Arun</p> <p></p> <p></p> <p></p> 2013-02-02T11:38:16-05:00