Chevy Mark Sunderland

Chevy Mark Sunderland

  • NA
  • 188
  • 167.3k

Export GridView to Excel

Apr 27 2012 12:04 PM
Hello there.
First of all I must say that I am a newbie when it comes to net.
Here is my problem.

I need Export GridView to Excel and try this your article:
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx

The export working but I have two problems:
  1. In my excel file only first page of GridView1.
  2. I need not show first column of gridwiev
I try this but I don't have change in the output.

If you have link for similar task, please give it me.

Your help would be very appreciated.

thanks for your time and hints.
I hope your help.


<%@ Page Language="C#" Title="Home page" Culture="it-IT" AutoEventWireup="true" EnableEventValidation ="false" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Odbc" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>

public override void VerifyRenderingInServerForm(Control control)
{

}

protected void Button2_Click(object sender, EventArgs e)
{
ExportGridToExcel(GridView1, Request.Cookies["MyCookie"].Value);
}

public void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.Clear();
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}.xls", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdGridView.AllowPaging = false;
grdGridView.DataSource = SqlDataSource1;
grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

Answers (1)