Generell funksjon for å eksportere en gridview til Excel

1 comment
Store deler av denne koden er hentet fra denne bloggen: http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
Eneste improve er at klasser, styles osv blir beholdt + muligheten for å ta med et stylesheet ved exporten.
Og her kommer koden:
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/// <summary>
/// Util for å exportere Gridview til Excel
/// </summary>
public class GridViewExportUtil
{
/// <summary>
/// Exporter Gridview til Excel
/// </summary>
/// <param name="fileName"></param>
/// <param name="gv"></param>
public static void Export(string fileName, GridView gv)
{
Export(fileName, gv, "");
}
/// <summary>
/// Exporterer GridView til Excel
/// </summary>
/// <param name="fileName"></param>
/// <param name="gv"></param>
/// <param name="styleSheet"></param>
public static void Export(string fileName, GridView gv, string styleSheet)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table tbl=new Table();
TableRow rad=new TableRow();
tbl.Controls.Add(rad);
TableCell cell=new TableCell();
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
}

foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
}

if (gv.FooterRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
}
cell.Controls.Add(gv);
rad.Controls.Add(cell);
tbl.Controls.Add(rad);
tbl.RenderControl(htw);
// hvis stylesheet er spesifisert ta det med
if (!String.IsNullOrEmpty(styleSheet))
{
try
{
StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(styleSheet));
string s = sr.ReadToEnd();
sr.Close();
HttpContext.Current.Response.Write("<HEAD><STYLE>");
HttpContext.Current.Response.Write(s.ToString());
HttpContext.Current.Response.Write("</STYLE></HEAD>");
}
catch { }
}
// skriv htmlwriter til response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
/// <summary>
/// Erstatt kontroller med text
/// </summary>
/// <param name="control"></param>
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "Ja" : "Nei"));
}
if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current);
}
}
}
}

Posted by email from Henris blogg (posterous)

1 comment :

  1. eToro is the best forex broker for rookie and professional traders.

    ReplyDelete