无法将HTML表格内容导出到ASP.NET中的Excel

问题描述:

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










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;
        }





以下是aspx代码



below is the aspx code

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
								

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



Link below : NPOI

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

Link below : NPOI
[^]


I use javascript for html table to excel



I use javascript for html table to excel


(\"#btnExport\").click(function (e) {
window.open('data:application/vnd.ms-excel,' +
("#btnExport").click(function (e) { window.open('data:application/vnd.ms-excel,' +