Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
when the html table is exported to excel from asp.net the table is displayed but the content inside the table is not displayed  below is my code



C#
protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            lblCategorystatusReportHeader.Visible = true;
            Response.AddHeader("content-disposition",
             "attachment;filename=StatusReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            //Panel preOrderPanel = (Panel)rpt1.FindControl("pnlPreOrder");
            //rptCP.RenderControl(hw);
            //pnlPreOrder.RenderControl(hw);
            order.RenderControl(hw);
            string output = sw.ToString();
            output = "<html><head><meta charset=" + Encoding.UTF8.WebName + " /></head><body>" + output + "</body></html>";
            Response.Output.Write(output);
            
            Response.Flush();
            Response.End();
            lblCategorystatusReportHeader.Visible = false;
        }


below is the aspx code
ASP.NET
this is the aspx code

<asp:Panel ID="order" runat="server">
    <table>
     <tr style="text-align:center">
            <td colspan="20" style="font-size:16px; padding-removed100px;background-color:#90cad9;border:1px solid black;display:none">
                <asp:Label ID="lblCategorystatusReportHeader" runat="server" Text="Status Report">
            </td>
        </tr>
        </table>
     <table>
        <tr>
           <td>
                
           </td>
        </tr>
    </table>
     <asp:Table ID="tbColor" runat="server" BorderWidth="1" GridLines="Both" BorderColor="Black" >
                <asp:TableRow ID="trColor" runat="server" >
                    <asp:TableCell ID="tdLegend" BorderWidth="2" Style="text-align:center">
                        <asp:Label ID="lblLegend" Text="Legend" runat="server">
                            
                    <asp:TableCell ID="tdOnTime" BorderWidth="2" Style="text-align:center">
                        <asp:Label ID="lblOntime" Text="On Time" ForeColor="White" runat="server">
                        
                     
                    <asp:TableCell ID="tdAceeptableDelay" BorderWidth="2" Style="text-align:center">
                        <asp:Label ID="lblAcceptableDelay" Text="Acceptable Delay" ForeColor="White" runat="server">
                    
                    <asp:TableCell ID="tdDelayed" BorderWidth="2" Style="text-align:center">
                        <asp:Label ID="lblDelayed" Text="Delayed" ForeColor="White" runat="server">
                    
                    
                
            
    <table>
        <tr>
           <td>
                
           </td>
        </tr>
    </table>
    
    <table>
          <%-- <tr style="text-align:center">
            <td colspan="20" style="font-size:16px; padding-removed100px;background-color:#90cad9;border:1px solid black;display:none">
                <asp:Label ID="lblCategorystatusReportHeader" runat="server" Text="Category Status Report">
            </td>
        </tr>--%>
        <tr>
            <td style="width:150px !important;vertical-align:middle;text-align:center; border-style:solid; border-width:1px" rowspan="2">
                <asp:Label ID="lblCPPre" runat="server">
            </td>
            <td>
               <%-- <asp:Panel ID="pnlPreOrder" runat="server">--%>
                    <table border="1" style="border-collapse:collapse">
                        <tr>
                            <%--<td stylwe="width:40px">
                                <asp:Label ID="lblCPPre" runat="server">
                            </td>--%>
                            <td style="vertical-align:middle;background-color:#90cad9;text-align:center">
                                <div style="width:100px !important">Category CP</div>
                            </td>
                            <td>
                                    <asp:Label ID="lblMessagePre" runat="server" Text="No Records found" Visible ="false" ForeColor="Red">
                                    <asp:Repeater ID="rptStylePO" runat="server" OnItemDataBound="rptStylePO_ItemDataBound">
                                        <itemtemplate>
                                            <table border="1" style="border-collapse:collapse;border-spacing:0px">
                                                <tr>
                                                    <td class="stylestatusReport">
                                                       <div style="width:100px !important"> <asp:HiddenField ID="styleid" runat="server" Value='<%# Container.DataItem.ToString() %>'/>
                                                        <asp:Label ID="lblStyle" runat="server"></div>
                                                    </td>
                                                    <td>
                                                        <asp:Label ID="lblNoMilestonePre" runat="server" Text="No Records found" Visible ="false" ForeColor="Red">
                                                        <asp:Repeater ID="rptt" runat="server" OnItemDataBound="rptt_ItemDataBound">
                                                            <itemtemplate>
                                                                <table>
                                                                    <tr>
                                                                        <td style="width:60px;vertical-align:middle;text-align:center">
                                                                            <div style="width:60px !important"><asp:HiddenField ID="stlId" runat="server" Value='<%# Container.DataItem.ToString() %>' /></div>
                                                                            
                                                                        </td>
                                                                        <td>
                                                                            <asp:DataList ID="dataListPO" runat="server" RepeatDirection="Horizontal" OnItemDataBound="dataListPO_ItemDataBound">
                                                                                <itemtemplate>
                                                                                    <table border="1" style="border-collapse:collapse; border-spacing:0px">
                                                                                        <tr>
                                                                                            <td style="word-wrap:break-word;width:110px; height:60px; color:#000000;vertical-align:middle;text-align:center; background-color:#90cad9 ; font-size:11px;">
                                                                                               <div style="word-wrap:break-word;width:100%;font-size:13px">
                                                                                                    <asp:Label ID="lblMilestoneValue" runat="server" Text='<%# Eval("MilestoneName") %>'></div>
                                                                                            </td>
                                                                                        </tr>
                                                                                        <tr>
                                                                                            <td style="text-align:left;">
                                                                                                <asp:Table ID="tblStatus" runat="server" Width="100%">
                                                                                                    <asp:TableRow ID="rowStatus" runat="server">
                                                                                                   
                                                                                                     <asp:TableCell ID="cellStatus1" Width="25px"  runat="server"><div style="width:100px; text-align:justify"><asp:Label ID="lblForecastedDate" Font-Size="13px" runat="server" Width="50%" ForeColor="Black"></div>
                                                                                                    <asp:TableCell ID="cellStatus" Width="25px" runat="server">  <div style="width:100px; text-align:center">                                                                                                      
                                                                                                        <asp:Label ID="lblStatus" Font-Size="13px" runat="server"  ForeColor="White"></div>                                                                                                 
                                                                                                    
                                                                                                   
                                                                                                                                                      
                                                                                                <asp:HiddenField ID="hdnPlannedDate" runat="server" Value='<%# Eval("MilestonePlannedDate") %>'>
                                                                                                <asp:HiddenField ID="hdnRevisedDate" runat="server" Value='<%# Eval("MilestoneRevisedDate") %>'>
                                                                                                <asp:HiddenField ID="hdnActualDate" runat="server" Value='<%# Eval("MilestoneActualDate") %>'>
                                                                                                <asp:HiddenField ID="hdnMilestoneStatus" runat="server" Value='<%# Eval("MilestoneStatus") %>' />
                                                                                            </td>
                                                                                        </tr>
                                                                                    </table>
                                                                                </itemtemplate>
                                                                            
                                                                         </td>
                                                                    </tr>
                                                                 </table>
                                                            </itemtemplate>
                                                        
                                                    </td>
                                                </tr>
                                            </table>
                                        </itemtemplate>
                                    
            
                            </td>
                        </tr>
                    </table>
               <%-- --%>
            </td>
        </tr>
        <tr>
            <td>
               <%-- <asp:Panel ID="pnlPostOrder" runat="server">--%>
                    <table border="1" style="border-collapse:collapse;">
                        <tr>
                            <%--<td style="width:40px">
                                <asp:Label ID="lblCPPost" runat="server">
                            </td>--%>
                            <td style=" background-color:#90cad9; font-size:14px;vertical-align:middle;text-align:center;white-space:nowrap; "><div style="width: 100px;">Item CP</div></td>
                            <td style="font-size:14px">
                                <asp:Label ID="lblMessagePost" runat="server" Text="No Records found" Visible ="false" ForeColor="Red">
                                    <asp:Repeater ID="rptStylePostOrder" runat="server" OnItemDataBound="rptStylePostOrder_ItemDataBound">
                                        <itemtemplate>
                                            <table border="1" style="border-collapse:collapse">
                                                <tr>
                                                    <td class="stylestatusReport">
                                                        <div style="width:100px !important"><asp:HiddenField ID="styleid" runat="server" Value='<%# Container.DataItem.ToString() %>'/>
                                                        <asp:Label ID="lblStyle" runat="server"></div>
                                                    </td>
                                                    <td>
                                                        <asp:Label ID="lblNoMilestonePost" runat="server" Text="No Records found" Visible ="false" ForeColor="Red">
                                                        <asp:Repeater ID="rptPO" runat="server" OnItemDataBound="rptPO_ItemDataBound">
                                                            <itemtemplate>
                                                                <table>
                                                                    <tr>
                                                                        <td style="word-wrap:break-word;width:60px;vertical-align:middle;text-align:center;">
                                                                            <div style="width:60px">
                                                                                <asp:Label ID="lblPostOrderNumber" runat="server">
                                                                            <asp:HiddenField ID="hdnStylePOID" runat="server" Value='<%# Container.DataItem.ToString() %>'/></div>    
                                                                        </td>
                                                                        <td>
                                                                            <asp:DataList ID="dataListPostOrder" runat="server" RepeatDirection="Horizontal" OnItemDataBound="dataListPostOrder_ItemDataBound">
                                                                                <itemtemplate>
                                                                                    <table border="1" style="border-collapse:collapse">
                                                                                        <tr>
                                                                                            <td style="word-wrap:break-word;width:110px; height:60px;background-color:#90cad9 ; font-size:11px;vertical-align:middle;text-align:center">
                                                                                                <div style="word-wrap:break-word;width:100%; font-size:13px"><asp:Label ID="lblMilestoneValuePost" runat="server" Text='<%# Eval("MilestoneName") %>'></div>
                                                                                            </td>
                                                                                        </tr>
                                                                                        <tr>
                                                                                            <td style="text-align:left">
                                                                                                <asp:Table ID="tblStatusPost" runat="server" Width="100%">
                                                                                                    <asp:TableRow ID="rowStatusPost" runat="server">
                                                                                                     <asp:TableCell ID="cellStatus1Post" Width="25px"  runat="server">
                                                                                                         <div style="width:100px;text-align:justify;"><asp:Label ID="lblForecastedDatePost" Font-Size="13px" runat="server" Width="50%" ForeColor="Black"></div>
                                                                                                    <asp:TableCell ID="cellStatusPost" runat="server"><div style="width:100px;text-align:center">
                                                                                                        <asp:Label ID="lblStatusPost" Font-Size="13px" runat="server" Width="100%" ForeColor="White"></div>
                                                                                                <asp:HiddenField ID="hdnPlannedDatePost" runat="server" Value='<%# Eval("MilestonePlannedDate") %>'>
                                                                                                <asp:HiddenField ID="hdnRevisedDatePost" runat="server" Value='<%# Eval("MilestoneRevisedDate") %>'>
                                                                                                <asp:HiddenField ID="hdnActualDatePost" runat="server" Value='<%# Eval("MilestoneActualDate") %>'>
                                                                                                <asp:HiddenField ID="hdnMilestoneStatusPost" runat="server" Value='<%# Eval("MilestoneStatus") %>' />
                                                                                            </td>
                                                                                        </tr>
                                                                                    </table>
                                                                                </itemtemplate>
                                                                            
                                                                        </td>
                                                                    </tr>
                                                                </table>

                                                            </itemtemplate>

                                                        
                                                    </td>
                                                </tr>
                                            </table>
                                        </itemtemplate>
                                    
           
                            </td>
                        </tr>
                    </table>
               <%-- --%>
            </td>
        </tr>
    </table>
this this image of the actual table 
<a href="http://i.stack.imgur.com/ghl8I.png">http://i.stack.imgur.com/ghl8I.png</a>[<a href="http://i.stack.imgur.com/ghl8I.png" target="_blank" title="New Window">^</a>]

below is the image that is being displayed in excel

<a href="http://i.stack.imgur.com/at6dw.png">http://i.stack.imgur.com/at6dw.png</a>[<a href="http://i.stack.imgur.com/at6dw.png" target="_blank" title="New Window">^</a>]

What I have tried:

i tried to debug the issue and i tried for fix in internet which i never got
Posted
Updated 16-May-16 3:43am
v4
Comments
Karthik_Mahalingam 16-May-16 1:01am    
what is'order' object
madhankannan 16-May-16 1:03am    
it's a panel
Karthik_Mahalingam 16-May-16 1:30am    
post the link where you got this example
madhankannan 16-May-16 1:33am    
it was in codeproject oly

Karthik_Mahalingam 16-May-16 1:37am    
i tested your code with sample table inside a panel
it works fine..
what is the issue

You can use NPOI as well, its available on nuget , sample code is available as well.

Link below : NPOI
[^]
 
Share this answer
 
I use javascript for html table to excel

JavaScript
$("#btnExport").click(function (e) {
    window.open('data:application/vnd.ms-excel,' + $('#dvData').html());
    e.preventDefault();
});


HTML
<div id="dvData">
    <table>
        <tr>
            <th>Column One</th>
            <th>Column Two</th>
            <th>Column Three</th>
        </tr>
        <tr>
            <td>row1 Col1</td>
            <td>row1 Col2</td>
            <td>row1 Col3</td>
        </tr>
        <tr>
            <td>row2 Col1</td>
            <td>row2 Col2</td>
            <td>row2 Col3</td>
        </tr>
        <tr>
            <td>row3 Col1</td>
            <td>row3 Col2</td>
            <td><a href="http://www.google.com/">google</a>
            </td>
        </tr>
    </table>
</div>
 
Share this answer
 
You can use Microsoft Open XML Format SDK. See, for example, this CodeProject article:
Creating basic Excel workbook with Open XML[^].

NPOI is another option: NPOI — Home[^].

See also my past answers on the topic referenced in this one: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA
 
Share this answer
 
v2
Quote:
when the html table is exported to excel from asp.net the table is displayed but the content inside the table is not displayed below is my code
The code that deal with your html table is not here.

The only possible advice is "use the debugger to see what your code is doing".

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
Remove all the hidden fields in the table and try to generate the excel, it should work.
refer this asp.net - Remove control from repeater before rendering - Stack Overflow[^]

if you want to retain the existing hidden controls, then you can create a duplicate copy of the panel without hidden fields and make the it hidden (display:none) and use this panel for rendering the html.
 
Share this answer
 
Comments
Karthik_Mahalingam 16-May-16 7:31am    
comment all the hidden fields and check
madhankannan 16-May-16 7:34am    
okay how do i remove the hidden fields
Karthik_Mahalingam 16-May-16 7:35am    
just comment it like this

<%--<asp:HiddenField ID="styleid" runat="server" Value='some' />--%>
madhankannan 16-May-16 7:44am    
i m getting error when i comment hiddenfields

Karthik_Mahalingam 16-May-16 7:45am    
what error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900