GridView导出到Excel表

本文介绍了一种将数据从DataTable导出至Excel的方法,并提供了具体的实现代码。该方法支持自定义导出字段及其对应的列头名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class PrintClass
{
 public PrintClass()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    public static void ExportToExcel(DataTable dataList, string[] fields, string[] headTexts, string title)
    {
        GridView gvw = new GridView();
        int ColCount, i;

        //如果筛选的字段和对应的列头名称个数相对的情况下只导出指定的字段
        if (fields.Length != 0 && fields.Length == headTexts.Length)
        {
            ColCount = fields.Length;
            gvw.AutoGenerateColumns = false;

            for (i = 0; i < ColCount; i++)
            {
                BoundField bf = new BoundField();
                bf.DataField = fields[i];
                bf.HeaderText = headTexts[i];
                gvw.Columns.Add(bf);
            }
        }
        else
        {
            gvw.AutoGenerateColumns = true;
        }

        SetStype(gvw);
        gvw.DataSource = dataList;
        gvw.DataBind();

        ExportToExcel(gvw, title);
    }
    /// <summary>
    /// 导出数据到Excel
    /// </summary>
    /// <param name="DataList">IList Data</param>
    /// <param name="Fields">要导出的字段</param>
    /// <param name="HeadName">字段对应显示的名称</param>
    public static void ExportToExcel(DataTable dataList, string[] fields, string[] headTexts)
    {
        ExportToExcel(dataList, fields, headTexts, string.Empty);
    }

    /// <summary>
    /// 设置样式
    /// </summary>
    /// <param name="gvw"></param>
    private static void SetStype(GridView gvw)
    {
        gvw.Font.Name = "Verdana";
        gvw.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
        gvw.HeaderStyle.BackColor = System.Drawing.Color.LightCyan;
        gvw.HeaderStyle.ForeColor = System.Drawing.Color.Black;
        gvw.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        gvw.HeaderStyle.Wrap = false;
        gvw.HeaderStyle.Font.Bold = true;
        gvw.HeaderStyle.Font.Size = 10;
        gvw.RowStyle.Font.Size = 10;
    }
    /// <summary>
    /// 导出GridView中的数据到Excel
    /// </summary>
    /// <param name="gvw"></param>
    /// <param name="DataList"></param>
    private static void ExportToExcel(GridView gvw, string title)
    {
        //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        //Response.AddHeader("Content-Disposition", "attachment;filename=" + str_day + ".xls");

        string fileName;
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        fileName = string.Format("Export-File {0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
        HttpContext.Current.Response.Charset = "gb2312";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        StringWriter tw = new System.IO.StringWriter();
        HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        gvw.RenderControl(hw);
        if (!string.IsNullOrEmpty(title))
        {
            HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" + title + "</font></center></b>");
        }
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.Close();
        HttpContext.Current.Response.End();

        gvw.Dispose();
        tw.Dispose();
        hw.Dispose();

        gvw = null;
        tw = null;
        hw = null;

    }
}

 

调用方法

            DataTable iList = swevent.FindAll();//得到数据,返回一个Table
            PrintClass.ExportToExcel(iList, new string[] { "e_id","e_equementpart","e_senduser","e_remark","e_sendsim","et_id" }, new string[] { "编号","供电局","事件通知人","备注","手机号","事件" });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值