zoukankan      html  css  js  c++  java
  • GridView导出到excel

    将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, "<input.*checked=\"checked\".*?/>", "Received&nbsp;/", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, "<input.*type=\"checkbox\".*?/>", "Not received&nbsp;/", RegexOptions.IgnoreCase);
            result = result.Replace("&nbsp;&nbsp;", "");
    
            //替换掉链接和外围的div
            result = Regex.Replace(result, "<a.*?\">", "", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, "<div>|</div>|</a>|<span.*?>|</span>|\n", "", RegexOptions.IgnoreCase);
    
            //TODO:处理图片,把图片替换成文字
            string replaceText = string.Empty;
            Regex re = new Regex("<img.*?/>", RegexOptions.None);
    
            //行标题特殊处理
            Regex re2 = new Regex("<th.*?</th>", RegexOptions.None);
            
            return result;
        }
    
        public static string BasicHTML
        {
            get
            {
                return @"<html xmlns:x='urn:schemas-microsoft-com:office:excel'>
                            <head>
                                <meta http-equiv=Content-Type content='text/html;charset=utf-8'>
                                <!--[if gte mso 9]><xml>
                                    <x:ExcelWorkbook>
                                        <x:ExcelWorksheets>
                                            <x:ExcelWorksheet>
                                                <x:Name>Worksheet</x:Name>
                                                <x:WorksheetOptions>
                                                    <x:Print>
                                                        <x:ValidPrinterInfo />
                                                    </x:Print>
                                                </x:WorksheetOptions>
                                            </x:ExcelWorksheet>
                                        </x:ExcelWorksheets>
                                    </x:ExcelWorkbook>
                                </xml>
                                <![endif]-->
                            </head>
                            <body>
                                {0}
                            </body>
                        </html>";
            }
        }
  • 相关阅读:
    故障诊断 | 系统级追踪诊断方法及案例分享
    Linux 一个网卡添加多个IP
    DB2 Vs MySQL系列 | MySQL与DB2的数据类型对比
    新年寄语 | 2018 以及 Oracle 18c 一个时代的开启
    perl 全文搜索tr/td标签内容
    perl 监控activemq队列
    年终总结 | 从Oracle到MySQL大家最关注的竟然是...
    辞旧迎新:2018年的分区你们建了吗?
    mysql 执行长sql
    Ubuntu搭建NTP服务器
  • 原文地址:https://www.cnblogs.com/michelledawm/p/4239207.html
Copyright © 2011-2022 走看看