Export Panel in ASP.NET


Introduction

In this article I'll show you how we can export a panel containing controls like Labels, Textboxes etc. In many cases we need to export our GridView to Excel and some other controls on the page to Excel.

Background

For exporting our page content in this article I'm using a third party DLL which contains same panel like ASP.Net panel. Doing a Google search I found this DLL and I'm willing to share it with you. This export panel can be exported in various formats like Excel, Word, HTML, PowerPoint and WordPerfect. By simply using just two or three lines of code we can export our panel in th above mentioned format.

In so many cases we need to prepare reports; normally these reports we are displayed in a GridView which you can export to Excel format with lots of stuff but in this case you are able to export only GridView and only in Excel format. But using this export panel control you can export anything in the panel to various formats and with little stuff. So let's see this export panel control.

Step 1:

Start a new web site and add a reference to the ExportPanel.dll file given with the upload in the bin directory.

Step 2:

Register tagprefix to ExportPanel.dll on Default.aspx page like below.

<%@ Register TagPrefix="cc1" Namespace="ControlFreak" Assembly="ExportPanel" %> 

Step 3:

After registering tagprefix you can use this Exportpanel control like below.

<cc1:exportpanel id="ExportPanel1" runat="server" Height="207px" Width="688px" BorderWidth="1px" BorderStyle="Dotted" ScrollBars="Both"></cc1:exportpanel>

Step 4:

In this panel you can keep any control with data which you want to export in Excel Word, HTML etc. I'm keeping GridView and label. 

<cc1:exportpanel id="ExportPanel1" runat="server" Height="207px" Width="688px" BorderWidth="1px"
                                                            BorderStyle="Dotted" ScrollBars="Both">
        <table class="style1">
            <tr>
                <td>
                    <asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow"
                        BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
                        GridLines="None">
                        <AlternatingRowStyle BackColor="PaleGoldenrod" />
                        <FooterStyle BackColor="Tan" />
                        <HeaderStyle BackColor="Tan" Font-Bold="True" />
                        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
                            HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                        <SortedAscendingCellStyle BackColor="#FAFAE7" />
                        <SortedAscendingHeaderStyle BackColor="#DAC09E" />
                        <SortedDescendingCellStyle BackColor="#E1DB9C" />
                        <SortedDescendingHeaderStyle BackColor="#C2A47B" />
                    </asp:GridView>
                </td>
                <td valign="top">
                    Some Thing Here like controls or any text also.</td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#0000CC"
                        Text="I'm Lable"></asp:Label>
                </td>
                <td bgcolor="#99FFCC">
                    <strong>This is Table Cell</strong></td>
            </tr>
        </table>
                                                           
                                                </cc1:exportpanel>

Put one RadioButtonList to specify the format in which te user wants to export the panel.

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="HTML" Selected="True">HTML</asp:ListItem>
                                                            <asp:ListItem Value="Excel">Excel</asp:ListItem>
                                                            <asp:ListItem Value="Word">Word</asp:ListItem>
                                                            <asp:ListItem Value="Powerpoint">Powerpoint</asp:ListItem>
                                                            <asp:ListItem Value="WordPerfect">WordPerfect</asp:ListItem>
    </asp:RadioButtonList>

Step 5:

Populate the GridView; here I'm creating a simple datatable with some rows and binding it to the GridView.

DataTable dt = new DataTable("RunnerUp");
            DataColumn dc1=new DataColumn("Name",typeof(string));
            DataColumn dc2 = new DataColumn("Points", typeof(int));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            DataRow dr1 = dt.NewRow();
            dr1[0] = "Vulpes";
            dr1[1] = 235;
            DataRow dr2 = dt.NewRow();
            dr2[0] = "SP Nayak";
            dr2[1] = 135;
            DataRow dr3 = dt.NewRow();
            dr3[0] = "Krishna";
            dr3[1] = 40;
            dt.Rows.Add(dr1);
            dt.Rows.Add(dr2);
            dt.Rows.Add(dr3);
            GridView1.DataSource = dt;
            GridView1.DataBind();

Step 6:

In the Export button click event, write the following code to export the panel in the user selected format.

switch (RadioButtonList1.SelectedValue.ToString())
        {
            case "HTML":
                  ExportPanel1.ExportType = ExportPanel.AppType.HTML;
                break;
            case "Excel":
                ExportPanel1.ExportType = ExportPanel.AppType.Excel;
                break;
            case "Word":
                ExportPanel1.ExportType = ExportPanel.AppType.Word;
                break;
            case"Powerpoint":
                ExportPanel1.ExportType = ExportPanel.AppType.PowerPoint;
                break;
            case"WordPerfect":
                ExportPanel1.ExportType = ExportPanel.AppType.WordPerfect;
                break;
        }

Step 7:

Run the application and export the panel in Excel, Word, WordPerfect, PowerPoint and HTML.

Conclusion:

In this way we can export anything in panel in various formats using a little bit code.


Similar Articles