0

我正在尝试以 pdf 格式导出多个网格视图,但它们未在 HtmlTextWriter 中呈现网格视图是动态创建的,嵌套在转发器控件中。作为数据源,我使用的是数据表,尽管这些表显示在浏览器中(连同它们的数据),但它们并没有呈现在 pdf 文件中。

在代码下方谢谢!

ASPX:

<asp:Panel ID="pnlContent" runat="server">
        <asp:Repeater ID="rptResult" runat="server" OnItemDataBound="rptResult_ItemDataBound" EnableViewState="false">
            <HeaderTemplate>                                
                Detalii test<br />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Literal ID="ltGroupId12" runat="server" Visible="false" Text='<%# Eval("GroupId12")%>'></asp:Literal>                    
                <asp:GridView ID="gvResultDetail" runat="server" AutoGenerateColumns="false" ShowHeader="false" GridLines="Both"
                     OnRowDataBound="gvResultDetail_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="Group1Desc" ItemStyle-CssClass="gridviewItemGroup1TestResult" />
                        <asp:BoundField DataField="Group2Desc" ItemStyle-CssClass="gridviewItemGroup2TestResult"/>
                        <asp:TemplateField ItemStyle-CssClass="gridviewItemStdTestResult">
                            <ItemTemplate>
                                test
                                <asp:Label ID="lblRequestedResultText" runat="server" Visible="false" Text="Position request<br />Total: "></asp:Label>
                                <asp:Label ID="lblRequestedResultValue" runat="server" Visible="false" Text='<%# Eval("RequestedResult")%>'></asp:Label>                                
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-CssClass="gridviewItemStdTestResult">
                            <ItemTemplate>
                                <asp:Label ID="lblActualResultText" runat="server" Visible="false" Text="Answer level<br />Total: "></asp:Label>
                                <asp:Label ID="lblActualResultValue" runat="server" Visible="false" Text='<%# Eval("ActualResult")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-CssClass="gridviewItemStdTestResult">
                            <ItemTemplate>
                                <asp:Label ID="lblDifferencesText" runat="server" Visible="false" Text="Difference:"></asp:Label>
                                <asp:Label ID="lblDifferencesValue" runat="server" Visible="false" Text='<%# Eval("Differences")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-CssClass="gridviewItemStdTestResult">
                            <ItemTemplate>
                                <asp:Label ID="lblPercentageText" runat="server" Visible="false" Text="KQ<br />"></asp:Label>
                                <asp:Label ID="lblPercentageValue" runat="server" Visible="false" Text='<%# Eval("Percentage") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField Visible="false">
                            <ItemTemplate>
                                <asp:Literal ID="ltGroupType" runat="server" Visible="false" Text='<%# Eval("RecordType")%>'></asp:Literal>
                                <asp:Literal ID="ltNoRows" runat="server" Visible="false" Text='<%# Eval("NoRows")%>'></asp:Literal>
                                <asp:Literal ID="ltBasicDomain" runat="server" Visible="false" Text='<%# Eval("BasicDomain")%>'></asp:Literal>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <HeaderStyle CssClass="gridviewHeaderTestResult" />
                    <RowStyle CssClass="gridviewRowTestResult" />
                    <AlternatingRowStyle CssClass="gridviewRowTestResult" />                
                </asp:GridView>
            </ItemTemplate>                
            <SeparatorTemplate>
                <br />
            </SeparatorTemplate>
            <FooterTemplate>
                End
            </FooterTemplate>        
        </asp:Repeater>

        <br />
    </asp:Panel>

ASPX.CS:

Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=SDT.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
DataTable dtDetailedResult = (DataTable)Session["dtDetailedResult"];
    rptResult.DataSource = dtDetailedResult;
    rptResult.DataBind();        
    pnlContent.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 70f, 70f, 20f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
4

1 回答 1

0

我不确定嵌套的网格视图,但要将网格视图导出为 pdf,您必须从 Internet 下载 itextsharp.dll 并将其放在项目的 bin 文件夹中。

有一个示例输出示例,说明如何将 gridview 导出为 pdf。你可以参考这里。

导出-gridview-to-pdf

希望能帮助到你。

于 2012-12-07T09:50:26.597 回答