How come there are only 900 rows were written into the destination excel file? Does anyone face the same issue and how to resolve it? Please advice me... Thanks a lot...
public void ExportDataSetToExcel(DataSet ds, string filename)
{
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
hieupt
Member
13 Points
44 Posts
How to save all data in GridView to Excel
Aug 07, 2012 03:21 AM|LINK
Hi all,
How would I save all data in a GridView to an Excel spreadsheet?
Is there any controls to select the destination folder and enter the desired filename at client side?
Please advice me... Thanks a lot...
Pbalan.in
Contributor
2142 Points
483 Posts
Re: How to save all data in GridView to Excel
Aug 07, 2012 04:21 AM|LINK
Hi,
Use this code below..
Include the namespace as
Imports System.IO
Imports System.Threading
on the button click event
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=GridView1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter StringWriter = new System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
GridView1.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: How to save all data in GridView to Excel
Aug 09, 2012 10:12 AM|LINK
Export GridView To Excel ASP.NET:
http://csharpdotnetfreak.blogspot.com/2011/10/export-gridview-to-excel.html
Export GridView To Word Excel PDF CSV Formats in ASP.Net
http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx
Feedback to us
Develop and promote your apps in Windows Store
hieupt
Member
13 Points
44 Posts
Re: How to save all data in GridView to Excel
Jan 22, 2013 02:31 AM|LINK
Hi all,
How come there are only 900 rows were written into the destination excel file? Does anyone face the same issue and how to resolve it? Please advice me... Thanks a lot...
public void ExportDataSetToExcel(DataSet ds, string filename) { HttpResponse response = HttpContext.Current.Response; // first let's clean up the response.object response.Clear(); response.Charset = ""; // set the response mime type for excel response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\""); // create a string writer using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // instantiate a datagrid DataGrid dg = new DataGrid(); dg.DataSource = ds.Tables[0]; dg.DataBind(); dg.RenderControl(htw); response.Write(sw.ToString()); response.End(); } } }