共享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)
        
{
        }

    }

}


转载于:https://www.cnblogs.com/LifelongLearning/archive/2007/11/12/957245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值