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>";
            }
        }
  • 相关阅读:
    jquery的ready和简写
    javasript之ajax学习笔记
    用 Drupal 创建更好的评分系统的具体步骤
    drupal的FIVESTAR投票模块说明
    drupal中时间自定义格式
    drupal首页不显示默认内容列表方法
    基于 Zen 创建一个 Drupal 7 的主题(模板) ,一份简单的Drupal模板教程
    jquery tooltip事件
    HTML5地区自转代码
    转:Jmeter常见问题 (转载) http://www.51testing.com/?uid-128005-action-viewspace-itemid-84094
  • 原文地址:https://www.cnblogs.com/michelledawm/p/4239207.html
Copyright © 2011-2022 走看看