Tuesday, May 26, 2009

Export To Excel

protected void lnkExport_Click(object sender, EventArgs e)
{
//Disable paging
gvCompanyList.AllowPaging = false;
BindGridView("");

// exluded columns arraylist
ArrayList defaultExcludedColumns = new ArrayList();

// Always exclude these columns
defaultExcludedColumns.Add("MyHiddenFieldName");
defaultExcludedColumns.Add(" ");

PrepareGridViewForExport(gvCompanyList);
// Send to base Excel export method
ExportGridView(gvCompanyList, "defaultFileName", defaultExcludedColumns);

// Rebind with paging enabled
gvCompanyList.AllowPaging = true;
BindGridView("");

//PrepareGridViewForExport(gvCompanyList);
//ExportGridView();

}

///
/// Export GridView data to Excel.
///

/// GridView control to export.
/// Filename of excel spreadsheet.
/// ArrayList of columns to exlude.
protected void ExportGridView(GridView grdView, string filename, ArrayList excludedColumnList)
{
// Clear response content & headers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();

// Add header
Response.AddHeader("content-disposition","attachment;filename="+filename+".xls");

Response.Charset = string.Empty;
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
Response.ContentType = "application/vnd.xls";


// Create stringWriter
System.IO.StringWriter stringWrite = new System.IO.StringWriter();

// Create HtmlTextWriter
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

// Remove controls from Column Headers
if (grdView.HeaderRow != null && grdView.HeaderRow.Cells != null)
{
for (int ct = 0; ct < grdView.HeaderRow.Cells.Count; ct++)
{
// Save initial text if found
string headerText = grdView.HeaderRow.Cells[ct].Text;
// string TemplateText=grdView.

// Check for controls in header
if (grdView.HeaderRow.Cells[ct].HasControls())
{
// Check for link button
if (grdView.HeaderRow.Cells[ct].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
{
// link button found, get text
headerText = ((LinkButton)grdView.HeaderRow.Cells[ct].Controls[0]).Text;
}

// Remove controls from header
grdView.HeaderRow.Cells[ct].Controls.Clear();
}

// Reassign header text
grdView.HeaderRow.Cells[ct].Text = headerText;
}
}

// Remove footer
if (grdView.FooterRow != null)
{
grdView.FooterRow.Visible = false;
}

// Remove unwanted columns (header text listed in removeColumnList arraylist)
foreach (DataControlField field in grdView.Columns)
{
if (excludedColumnList.Contains(field.HeaderText))
{
field.Visible = false;
}
}

// Call gridview's renderControl
grdView.RenderControl(htmlWrite);

// Write Response to browser
Response.Write(stringWrite.ToString());

Response.End();
}
private void ExportGridView()
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();

Response.AddHeader("content-disposition","attachment;filename=FileName.xls");

Response.Charset ="";
// If you want the option to open the Excel file without saving than comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType ="application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);
gvCompanyList.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
private void PrepareGridViewForExport(Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);

}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}

}
}

No comments: