<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
    TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    public enum ExportType
    {
        Word,
        Excel,
        Csv
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (sender == Button1)
        {
            Export(ExportType.Word);
        }
        else if (sender == Button2)
        {
            Export(ExportType.Excel);
        }
        else if (sender == Button3)
        {
            Export(ExportType.Csv);
        }
    }

    public void Export(ExportType type)
    {
        string html = Content1.Value.Replace("&lt;", "<");

        Response.Clear();
        Response.Buffer = true;

        Response.ContentEncoding = Encoding.UTF8;

        Response.Charset = "";

        if (type == ExportType.Word)
        {
            Response.ContentType = "application/vnd.ms-word";
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + "Word.doc" + "\"");
        }
        else if (type == ExportType.Excel)
        {
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + "Excel.xls" + "\"");
        }
        else if (type == ExportType.Csv)
        {
            Response.ContentType = "text/xls; charset-UTF-8;";
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + "Excel.csv" + "\"");
        }

        if (type != ExportType.Csv)
        {
            Response.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Datos</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<style>\n</style>\n</head>\n<body>\n");
        }

        if (type == ExportType.Csv)
        {
            Response.BinaryWrite(System.Text.Encoding.Default.GetBytes(html));
        }
        else
        {
            Response.Write(html);
        }

        if (type != ExportType.Csv)
        {
            Response.Write("</body>");
            Response.Write("</html>");
        }

        Response.End();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%;">
<head runat="server">
    <title>SilverlightGridExport</title>

    <script type="text/javascript">
        function getContent(sender, e) {
            if (sender.id == "Button1" || sender.id == "Button2") {
                var content = $get("Xaml1").Content.Page.ToHtml();
                $get("Content1").value = content.replace(/</g, "&lt;");
            }
            else {
                $get("Content1").value = $get("Xaml1").Content.Page.ToCsv();
            }
        }
    </script>

    <style type="text/css">
        html, body, form
        {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0px;
            padding: 0px;
        }
    </style>
</head>
<body>
    <form runat="server">
    <asp:HiddenField ID="Content1" runat="server" />
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div style="height: 10%;">
        <div style="margin: 20px">
            <asp:Button ID="Button1" Text="Export to Word" runat="server" OnClientClick="getContent(this, event);"
                OnClick="Button1_Click" />
            <asp:Button ID="Button2" Text="Export to Excel" runat="server" OnClientClick="getContent(this, event);"
                OnClick="Button1_Click" />
            <asp:Button ID="Button3" Text="Export to Csv" runat="server" OnClientClick="getContent(this, event);"
                OnClick="Button1_Click" />
        </div>
    </div>
    <div style="height: 90%;">
        <asp:Silverlight ID="Xaml1" HtmlAccess="Enabled" runat="server" Source="~/ClientBin/SilverlightGridExport.xap"
            MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />
    </div>
    </form>
</body>
</html>
