我正在尝试在pdf中导出几个gridviews,但它们不会在HtmlTextWriter中呈现 gridview是动态创建的,嵌套在转发器控件中。 作为数据源,我使用数据表,尽管表格显示在浏览器中(连同它们的数据),但它们不会以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();
答案 0 :(得分:0)
我不确定嵌套网格视图,但是要将gridview导出为pdf,您必须从Internet下载itextsharp.dll并将其放在项目的bin文件夹中。
有一个示例输出如何将gridview导出为pdf。你可以在这里参考。
希望它有所帮助。