将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>";
        }
    }

 

posted on 2015-01-21 16:22  MichelleDawm  阅读(148)  评论(0编辑  收藏  举报