Tuesday 1 December 2015

Export to excel from using stringbuilder in asp.net c#

// HTML

<div>
        <div id="dvInfo" runat="server">
            <br />
            <center>
                <b>Student Informations</b>
             </center>
            <br />
            <table align='center' border='1' bordercolor='#00aeef' width='50%' class='reporttable1'
                cellspacing='0' cellpadding='0' style='font-size: 10;'>
                <tr align='center'>
                    <td style='width: 10%'>
                        <b>RollNo</b>
                    </td>
                    <td>
                        <b>Name</b>
                    </td>
                    <td>
                        <b>City</b>
                    </td>
                </tr>
                <tr>
                    <td>
                        A1001
                    </td>
                    <td>
                        Raja
                    </td>
                    <td>
                        IIT Kanpur
                    </td>
                </tr>
                <tr>
                    <td>
                        A1002
                    </td>
                    <td>
                        Mohit
                    </td>
                    <td>
                        ITI Gonda
                    </td>
                </tr>
            </table>
            <br />
            <br />
        </div>
        <center>
            <asp:Button ID="Button2" runat="server" Style="height: 30px;" OnClick="Button2_Click"
                Text="Export HTML to Excel" />
        </center>

    </div>

// ASPX.cs

protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {

        StrExport.Append(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'><head><title>Report</title>");
        StrExport.Append(@"<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div class=Section1>");
        StrExport.Append("<DIV  style='font-size:12px;'>");
        StrExport.Append(dvInfo.InnerHtml);
        StrExport.Append("</div></body></html>");
        string strFile = "Reporst.xls";
        string strcontentType = "application/excel";
        Response.ClearContent();
        Response.ClearHeaders();
        Response.BufferOutput = true;
        Response.ContentType = strcontentType;
        Response.AddHeader("Content-Disposition", "attachment; filename=" + strFile);
        Response.Write(StrExport.ToString());
        Response.Flush();
        Response.Close();
        Response.End();

        }
        catch (Exception ex)
        {
        }
    }

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...