共享GridView DataGrid DataTable导出到Excel代码
下面是一个导出Excel文件的代码。
code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Test
{
/**//// <summary>
/// 重载样例
/// </summary>
public class GridViewExport : ExcelExport
{
public GridViewExport(DataTable dt)
: base(dt, true)
{
}
public override void GridViewBoundEvent(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
// e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
/**//// <summary>
/// 重载样例
/// </summary>
public class DataGridExport : ExcelExport
{
public DataGridExport(DataTable dt)
: base(dt, false)
{
//不使用动态列,就在这个地方设置表格和列
//BoundColumn bc = new BoundColumn();
//bc.DataField = "company";
//bc.HeaderText = "你好";
//oDG.Columns.Add(bc);
}
/**//// <summary>
/// 设置列的样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public override void DataGridBoundEvent(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// e.Item.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
//e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
public abstract class ExcelExport
{
protected ExcelExport(DataTable dt, bool IsGV)
{
if (IsGV)
{
_IsGV = true;
gv = new GridView();
gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
gv.DataSource = dt;
}
else
{
oDG = new DataGrid();
oDG.ItemDataBound += new DataGridItemEventHandler(oDG_ItemDataBound);
oDG.DataSource = dt;
}
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewBoundEvent(sender, e);
}
protected DataGrid oDG = null;
protected GridView gv = null;
private bool _IsGV = false;
/**//// <summary>
/// 如果是GridView就是True
/// </summary>
public bool IsGridView
{
get
{
return _IsGV;
}
}
/**//// <summary>
/// 导出Excel数据
/// </summary>
public void Export()
{
if (_IsGV)
{
if (gv.Columns.Count == 0)
gv.AutoGenerateColumns = true;
else
gv.AutoGenerateColumns = false;
gv.AllowPaging = false;
gv.DataBind();
ExportData(gv);
}
else
{
if (oDG.Columns.Count == 0)
oDG.AutoGenerateColumns = true;
else
{
oDG.AutoGenerateColumns = false;
}
oDG.DataBind();
ExportData(oDG);
}
}
/**//// <summary>
/// 导出DataGrid为Excel
/// </summary>
/// <param name="dg"></param>
public static void Export(DataGrid dg)
{
if (dg == null)
throw new Exception("传入的DataGrid为空。");
ExportData(dg);
}
private static void ExportData(object ob)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
HttpContext.Current.Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
if (ob as GridView != null)
((GridView)ob).RenderControl(htw);
if (ob as DataGrid != null)
((DataGrid)ob).RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
/**//// <summary>
/// 导出GridView为Excel
/// </summary>
/// <param name="gv"></param>
public static void Export(GridView gv)
{
if (gv == null)
throw new Exception("传入的GridView为空。");
ExportData(gv);
}
void oDG_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DataGridBoundEvent(sender, e);
}
/**//// <summary>
/// 设置DataGrid列的样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void DataGridBoundEvent(object sender, DataGridItemEventArgs e)
{
}
/**//// <summary>
/// 设置GridView列的样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void GridViewBoundEvent(object sender, GridViewRowEventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Test
{
/**//// <summary>
/// 重载样例
/// </summary>
public class GridViewExport : ExcelExport
{
public GridViewExport(DataTable dt)
: base(dt, true)
{
}
public override void GridViewBoundEvent(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
// e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
/**//// <summary>
/// 重载样例
/// </summary>
public class DataGridExport : ExcelExport
{
public DataGridExport(DataTable dt)
: base(dt, false)
{
//不使用动态列,就在这个地方设置表格和列
//BoundColumn bc = new BoundColumn();
//bc.DataField = "company";
//bc.HeaderText = "你好";
//oDG.Columns.Add(bc);
}
/**//// <summary>
/// 设置列的样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public override void DataGridBoundEvent(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// e.Item.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
//e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
public abstract class ExcelExport
{
protected ExcelExport(DataTable dt, bool IsGV)
{
if (IsGV)
{
_IsGV = true;
gv = new GridView();
gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
gv.DataSource = dt;
}
else
{
oDG = new DataGrid();
oDG.ItemDataBound += new DataGridItemEventHandler(oDG_ItemDataBound);
oDG.DataSource = dt;
}
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewBoundEvent(sender, e);
}
protected DataGrid oDG = null;
protected GridView gv = null;
private bool _IsGV = false;
/**//// <summary>
/// 如果是GridView就是True
/// </summary>
public bool IsGridView
{
get
{
return _IsGV;
}
}
/**//// <summary>
/// 导出Excel数据
/// </summary>
public void Export()
{
if (_IsGV)
{
if (gv.Columns.Count == 0)
gv.AutoGenerateColumns = true;
else
gv.AutoGenerateColumns = false;
gv.AllowPaging = false;
gv.DataBind();
ExportData(gv);
}
else
{
if (oDG.Columns.Count == 0)
oDG.AutoGenerateColumns = true;
else
{
oDG.AutoGenerateColumns = false;
}
oDG.DataBind();
ExportData(oDG);
}
}
/**//// <summary>
/// 导出DataGrid为Excel
/// </summary>
/// <param name="dg"></param>
public static void Export(DataGrid dg)
{
if (dg == null)
throw new Exception("传入的DataGrid为空。");
ExportData(dg);
}
private static void ExportData(object ob)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
HttpContext.Current.Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
if (ob as GridView != null)
((GridView)ob).RenderControl(htw);
if (ob as DataGrid != null)
((DataGrid)ob).RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
/**//// <summary>
/// 导出GridView为Excel
/// </summary>
/// <param name="gv"></param>
public static void Export(GridView gv)
{
if (gv == null)
throw new Exception("传入的GridView为空。");
ExportData(gv);
}
void oDG_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DataGridBoundEvent(sender, e);
}
/**//// <summary>
/// 设置DataGrid列的样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void DataGridBoundEvent(object sender, DataGridItemEventArgs e)
{
}
/**//// <summary>
/// 设置GridView列的样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void GridViewBoundEvent(object sender, GridViewRowEventArgs e)
{
}
}
}