无法在asp.net中将html表内容导出为ex​​cel

时间:2016-05-16 04:54:14

标签: c# html asp.net excel

当html表从asp.net导出到excel时,会显示该表,但下面没有显示表中的内容是我的代码。

        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代码                                                      的                                                                          &安培; NBSP                                                                                                                                                                                                                                                                                                                                                               

            </asp:TableRow>
        </asp:Table>
<table>
    <tr>
       <td>
           &nbsp
       </td>
    </tr>
</table>

<table>
      <%-- <tr style="text-align:center">
        <td colspan="20" style="font-size:16px; padding-left:100px;background-color:#90cad9;border:1px solid black;display:none">
            <b><asp:Label ID="lblCategorystatusReportHeader" runat="server" Text="Category Status Report"></asp:Label></b>
        </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"></asp:Label>
        </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"></asp:Label>
                        </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:Label>
                                <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"></asp:Label></div>
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblNoMilestonePre" runat="server" Text="No Records found" Visible ="false" ForeColor="Red"></asp:Label>
                                                    <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") %>'></asp:Label></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"></asp:Label></div></asp:TableCell>
                                                                                                <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"></asp:Label></div>                                                                                                 
                                                                                                </asp:TableCell>

                                                                                                                                                  </asp:TableRow></asp:Table>
                                                                                            <asp:HiddenField ID="hdnPlannedDate" runat="server" Value='<%# Eval("MilestonePlannedDate") %>'></asp:HiddenField>
                                                                                            <asp:HiddenField ID="hdnRevisedDate" runat="server" Value='<%# Eval("MilestoneRevisedDate") %>'></asp:HiddenField>
                                                                                            <asp:HiddenField ID="hdnActualDate" runat="server" Value='<%# Eval("MilestoneActualDate") %>'></asp:HiddenField>
                                                                                            <asp:HiddenField ID="hdnMilestoneStatus" runat="server" Value='<%# Eval("MilestoneStatus") %>' />
                                                                                        </td>
                                                                                    </tr>
                                                                                </table>
                                                                            </ItemTemplate>
                                                                        </asp:DataList>
                                                                     </td>
                                                                </tr>
                                                             </table>
                                                        </ItemTemplate>
                                                    </asp:Repeater>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </asp:Repeater>

                        </td>
                    </tr>
                </table>
           <%-- </asp:Panel>--%>
        </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"></asp:Label>
                        </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:Label>
                                <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"></asp:Label></div>
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblNoMilestonePost" runat="server" Text="No Records found" Visible ="false" ForeColor="Red"></asp:Label>
                                                    <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:Label>
                                                                        <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") %>'></asp:Label></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"></asp:Label></div></asp:TableCell>
                                                                                                <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"></asp:Label></div></asp:TableCell></asp:TableRow></asp:Table>
                                                                                            <asp:HiddenField ID="hdnPlannedDatePost" runat="server" Value='<%# Eval("MilestonePlannedDate") %>'></asp:HiddenField>
                                                                                            <asp:HiddenField ID="hdnRevisedDatePost" runat="server" Value='<%# Eval("MilestoneRevisedDate") %>'></asp:HiddenField>
                                                                                            <asp:HiddenField ID="hdnActualDatePost" runat="server" Value='<%# Eval("MilestoneActualDate") %>'></asp:HiddenField>
                                                                                            <asp:HiddenField ID="hdnMilestoneStatusPost" runat="server" Value='<%# Eval("MilestoneStatus") %>' />
                                                                                        </td>
                                                                                    </tr>
                                                                                </table>
                                                                            </ItemTemplate>
                                                                        </asp:DataList>
                                                                    </td>
                                                                </tr>
                                                            </table>

                                                        </ItemTemplate>

                                                    </asp:Repeater>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </asp:Repeater>

                        </td>
                    </tr>
                </table>
           <%-- </asp:Panel>--%>
        </td>
    </tr>
</table>

   [Below is the image of the table that is being displayed in excel.]
       http://i.stack.imgur.com/JhHQo.png

实际图片         [1]:http://i.stack.imgur.com/ghl8I.png

0 个答案:

没有答案