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

random

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

Exporting a Div to Excel in C#




In this article I will show how to export a div dynamic data into excel file in Asp.Net using C#. Find the source code below:-

exporting, content, div tag,dynamic content, export div, div to excel,export ASP.Net Repeater Control exported to excel, excel sheet, dynamic data asp.net, export to excel
Default.aspx
<%@ 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>Exporting a Div to Excel in 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;
            }

        /*.CompApp_DataTable tr td:first-child {
                    text-align: center;
                    font-weight: bold;
                   
                }*/
        /*-------------------------*/
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <h4>Exporting Div Dynamic Content To Excel in C# - ASP.Net</h4>
        <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>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
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)
        {
            throw ex;
        }
    }

    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)
        {
            throw ex;
        }
    }
}
Download the source code, click here ..
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading! :)
Exporting Div Dynamic Content To Excel in C# - ASP.Net Reviewed by Ravi Kumar on 1:00 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.