博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GridView导出到excel
阅读量:4317 次
发布时间:2019-06-06

本文共 2175 字,大约阅读时间需要 7 分钟。

将web页面的GridView导出到excel文件,包括GridView中checkbox,image的处理。

public static void Export(Control control, string defaultFileName)    {        HttpContext.Current.Response.Clear();        HttpContext.Current.Response.BufferOutput = true;        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + defaultFileName);        HttpContext.Current.Response.ContentType = "application/ms-excel";        control.EnableViewState = false;         HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");        System.IO.StringWriter stringWriter = new System.IO.StringWriter();        System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);        control.RenderControl(textWriter);        string result = stringWriter.ToString();        result = SpecialHandler(result);        HttpContext.Current.Response.Write(string.Format(ExportToExcelHelper.BasicHTML, result));        HttpContext.Current.Response.End();    }    private static string SpecialHandler(string result)    {        //把checkbox改成文字Yes/No        result = Regex.Replace(result, "
", "Received /", RegexOptions.IgnoreCase); result = Regex.Replace(result, "
", "Not received /", RegexOptions.IgnoreCase); result = result.Replace("  ", ""); //替换掉链接和外围的div result = Regex.Replace(result, "
", "", RegexOptions.IgnoreCase); result = Regex.Replace(result, "
|
||
||\\n", "", RegexOptions.IgnoreCase); //TODO:处理图片,把图片替换成文字 string replaceText = string.Empty; Regex re = new Regex("
", RegexOptions.None); //行标题特殊处理 Regex re2 = new Regex("
", RegexOptions.None); return result; } public static string BasicHTML { get { return @"
{0} "; } }

 

转载于:https://www.cnblogs.com/michelledawm/p/4239207.html

你可能感兴趣的文章
Linux重启Mysql命令
查看>>
前端模块化:RequireJS(转)
查看>>
应用程序缓存的应用(摘抄)
查看>>
jQuery基础知识,很赞的!!!
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>
一款jQuery打造的具有多功能切换的幻灯片特效
查看>>
SNMP从入门到开发:进阶篇
查看>>
@ServletComponentScan ,@ComponentScan,@Configuration 解析
查看>>
unity3d 射弹基础案例代码分析
查看>>
thinksns 分页数据
查看>>
os模块
查看>>