C#,ASP.NET把gridview中的数据导出到Excel中

该博客介绍了如何使用C#静态方法OutPutExcel将ASP.NET中的GridView控件中的数据导出为Excel文件。通过设置响应头、编码和内容类型,然后利用HtmlTextWriter将GridView渲染成HTML并写入响应,最后结束响应,实现数据导出。在ASP.NET页面中,通过定义一个按钮事件触发此方法。

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

cs:类

public static void OutPutExcel(GridView GridView1)
    {
        //
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Charset = "GB2312";
        //
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyy-mm-dd") + ".xls");
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        HttpContext.Current.Response.ContentType = "application/ms-excel";


        //
        CultureInfo cultureInfo = new CultureInfo("ZH-CN", true);
        StringWriter stringWriter = new StringWriter(cultureInfo);
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
        //将Gr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值