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学习笔记--20150122
    破解LR11 sentinel stage failed
    Linux部署环境初学(Resin、jdk)
    MongoDB操作
    TestNG
    在iOS8 下用Swift 创建自定义的键盘
    iOS 8下简单,可交互式的通知
    设计模式:策略模式
    用Swift创建一个自定义,可调整的控件
    iOS7状态栏上有趣的渐变遮罩
  • 原文地址:https://www.cnblogs.com/michelledawm/p/4239207.html
Copyright © 2011-2022 走看看