export div data to excel

Tags | export div data to excel in asp.net using c#, export Html div to excel c#, export div to excel in asp.net c#, export div to excel in asp.net c#

Exporting Div Dynamic Content To Excel in C# ASP.Net

The following asp.net c# tutorial will demonstrate, how to export div data into excel using javascript. Previously I have explained Export Datatable to CSV C# ASP.Net

Export div data to excel using JavaScript and C#

Find the source code below:-

export div data to excel 2 export div data to excel Export Div data to Excel in ASP.Net using C# export div to excel

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Export div to excel in asp.net c#</title>
    <script src="jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        function ExportDIVtoExcel() {
            var html = '';

            html += $("#divAccord").html();
            html = $.trim(html);
            html = html.replace(/>/g, '&gt;');
            html = html.replace(/</g, '&lt;');

            $("input[id$='HdnValexceldata']").val(html);

        }
    </script>

    <style type="text/css">
        .accordianContainer {
            max-height: 350px;
            overflow: auto;
        }
        /*----------------Data Grid inside Pop up----------------------*/
        .CompApp_TableScroll {
            max-height: 255px;
            overflow: auto;
        }

        .CompApp_DataTable {
            border-collapse: collapse;
        }

            .CompApp_DataTable tr th {
                background-color: #dedede;
                color: #333333;
                padding: 5px;
                border: 1px solid #cccccc;
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
                font-weight: normal;
                white-space: nowrap;
            }

            .CompApp_DataTable tr:nth-child(2n+2) {
                background-color: #f3f4f5;
            }

            .CompApp_DataTable tr:nth-child(2n+1) td {
                background-color: #d6dadf;
                color: #454545;
            }

            .CompApp_DataTable tr td {
                padding: 5px;
                color: #454545;
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
                border: 1px solid #cccccc;
                /*text-align: justify;*/
                white-space: nowrap;
            }

        /*-------------------------*/
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <h2>Exporting Div Dynamic Content To Excel in C# ASP.Net</h2>

        <div id="divAccord" class="accordianContainer" runat="server">
        </div>
        <br />
        <br />

        <asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClientClick="ExportDIVtoExcel();" OnClick="btnExport_Click" />
        <asp:HiddenField ID="HdnValexceldata" runat="server" />
    </form>
</body>
</html>

using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepeater();

        }
    }

    private void BindRepeater()
    {
        try
        {
            Repeater rContainer = null;

            DataTable dt = new DataTable();
            dt.Columns.Add("EmpId", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Address", typeof(string));


            dt.Rows.Add(25, "Rk", "Gurgaon");
            dt.Rows.Add(50, "Sachin", "Noida");
            dt.Rows.Add(10, "Nitin", "Noida");
            dt.Rows.Add(21, "Aditya", "Meerut");
            dt.Rows.Add(100, "Mohan", "Banglore");

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            if (dt.Rows.Count > 0)
            {
                rContainer = new Repeater();
                rContainer.DataSource = dt;
                rContainer.DataBind();

                foreach (DataTable dtCluster in ds.Tables)
                {
                    rContainer = new Repeater();

                    rContainer.ItemTemplate = new RepeaterTemplate(ListItemType.Item);
                    rContainer.HeaderTemplate = new RepeaterTemplate(ListItemType.Header);
                    rContainer.FooterTemplate = new RepeaterTemplate(ListItemType.Footer);

                    rContainer.DataSource = dtCluster;
                    divAccord.Controls.Add(rContainer);

                    this.DataBind();
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        try
        {
            string html = HdnValexceldata.Value;

            html = html.Replace("&gt;", ">");
            html = html.Replace("&lt;", "<");

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=DemoExcelFile_" + DateTime.Now.ToString("M_dd_yyyy_H_M_s") + ".xls");
            HttpContext.Current.Response.ContentType = "application/xls";
            HttpContext.Current.Response.Write(html);
            HttpContext.Current.Response.End();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}

Download Source Code

Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

LEAVE A REPLY

Please enter your comment!
Please enter your name here