ASP.NET Gridview to excel - c#

I have a page with Gridview and when button clicked I should export data from Gridview to .xls file. I have tried it in many ways. This is my code:
protected void export_Click(object sender, EventArgs e)
{
Response.ClearContent();
//Response.Buffer = true;
//Response.ClearHeaders();
//Response.CacheControl = "no-cache";
//Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Content-Disposion","attachment; filename=Clients.xls");
Response.ContentType = "application/excel";
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
grid.RenderControl(hwriter);
Response.Write(swriter.ToString());
Response.End();
}
The problem is when I click the button it is exporting aspx file. I have tried all answers in Stackoverflow and from other sources as well. However, I am not being able to correct it. I need exact reason why it is so and some possible solutions.(I tried Content-type as "application/excel", "application/vn", "application/vn-excel" as well.)

You can just follow following code, it is working fine..
protected void cmdExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=SupplierList.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter WriteItem = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlText = new HtmlTextWriter(WriteItem);
GridView1.AllowPaging = false;
DataTable dtSupplier = (DataTable)ViewState["dtSupplier"];
GridView1.DataSource = dtSupplier;
GridView1.DataBind();
GridView1.RenderControl(htmlText);
Response.Write(WriteItem.ToString());
Response.End();
}
Hope this will help you.
thanks

Related

How can we download the complete webpage data to spreadsheet

how can i download the content in the webpage to spreadsheet Below is my code which downloads only excel sheet data but it doesnot download the other content that is in web page how can i download the complete webpage data
protected void BtnSave_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=Details.ods");
Response.ContentType = "vnd.oasis.opendocument.text";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}
Replace your
<br>
with
<br/>

How to display gridLines in GridView from code behind

In aspx code i set GridLines="None"
How can i show GridLines from code behind when exporting grid-view to excel file.
protected void btnexcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=UsersReport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridusers.DataBind();
gridusers.RenderControl(hw);
//style to format numbers to string
string style = #"<style> .textmode { mso-number-format:\#; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
In function start, set:
grdAdslCompanyAdvisers.GridLines = GridLines.Both;
after export again
grdAdslCompanyAdvisers.GridLines = GridLines.None;
Hope this helps or gives you an idea.

Export gridview to excel, and replace one column

I need to export gridview data to excel on button click.
But in gridview one column data need to be changed with data that is another table from database.
For example:
Gridview showing employee ID(empid) from one table but I need to show username in place of empid in excel data.
The export to Excel:
protected void btmexport_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=Export.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
grd_data.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(grd_data);frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

could not generate excel file on button click in c#

In my project when i click on a button it should generate excel file and in the middle of first line in excel file as a heading i should get as "my first excel file". I could not get it when i am trying with following code.Any ideas?. Thanks in advance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace listofdirectories
{
public partial class ExportToExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
response.ContentType = "application/ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=practise.xls");
StreamWriter stw = new StreamWriter();
HtmlTextWriter htw = new HtmlTextWriter(stw);
stw.WriteLine("my first excel file");
response.End();
}
}
}
You forgot to add the content to the response like this-
response.Write(stw.ToString());
response.End();
Also change your content type to this -
response.ContentType = "application/vnd.ms-excel";
Instead of using StreamWriter use StringWriter and change your code like this-
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "filename.xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType ="application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
Response.Write(strwritter.ToString());
Response.End();
To load the content in to the excel file, if recommend that you load the dataset with the data, then bind a grid view with that dataset and render the gridview as html after that fill the response with that rendered html like this-
GridView gv = new GridView();
gv.DataSource = dataset; //Your datasource from database
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=filename.xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();

remove images while exporting excel from gridview

I am exporting excel from gridview and it is not giving me good format. Images are showing on excel file. I don't want the images, what can I do?
here is my code
protected void Export_to_Excel(object sender, EventArgs e)
{
//System.Diagnostics.Debugger.Break();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=vault-extract-nsf.xls");
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
I did it and images are ignored.
protected void Export_to_Excel(object sender, EventArgs e)
{
//System.Diagnostics.Debugger.Break();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=PatientSearchReport.xls");
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
string html2 = Regex.Replace(stringWrite.ToString(), #"(<input type=""image""\/?[^>]+>)", #"", RegexOptions.IgnoreCase);
html2 = Regex.Replace(html2, #"(<input class=""checkbox""\/?[^>]+>)", #"", RegexOptions.IgnoreCase);
html2 = Regex.Replace(html2, #"(<a \/?[^>]+>)", #"", RegexOptions.IgnoreCase);
Response.Write(html2.ToString());
Response.End();
}
You can do like this
protected void Export_to_Excel(object sender, EventArgs e)
{
GridView tmpGrid = new GridView();
// here bind this grid with same datasource which you have used for GridView1
//System.Diagnostics.Debugger.Break();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=vault-extract-nsf.xls");
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
tmpGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

Categories