我正在尝试使用iTextSharp将网格视图导出为pdf报告。 gridView包含一个未显示在pdf文件中的HyperLink字段。
<asp:GridView ID="GridView1" runat="server" CssClass="table table-striped" Style="font-family: sans-serif; font-size: medium" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" Width="903px" DataKeyNames="ID" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Start_Date" HeaderText="Start_Date" SortExpression="Start_Date" />
<asp:BoundField DataField="End_Date" HeaderText="End_Date" SortExpression="End_Date" />
<asp:HyperLinkField DataTextField="Agenda_Title" DataNavigateUrlFields="ID" DataNavigateUrlFormatString="~/Views/Portal/ViewAgenda.aspx?ID={0}" HeaderText="Title" ItemStyle-Width="150px"></asp:HyperLinkField>
<asp:BoundField DataField="Start_Time" HeaderText="Start_Time" SortExpression="Start_Time" />
<asp:BoundField DataField="End_Time" HeaderText="End_Time" SortExpression="End_Time" />
</Columns>
</asp:GridView>
我已将一个SQLDatasource附加到gridview控件,该控件使用Select Command来填充gridview。
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ArtistManagementSystem %>" SelectCommand="SELECT [Start Date] AS Start_Date, [End Date] AS End_Date, Title AS Agenda_Title, [Start Time] AS Start_Time, [End Time] AS End_Time, ID FROM Agenda WHERE ([Event ID] = @Event_ID) ORDER BY Start_Date, Start_Time">
<SelectParameters>
<asp:QueryStringParameter Name="Event_ID" QueryStringField="evtID" Type="String" />
</SelectParameters>
然后使用Button将gridview导出为PDF。
<asp:Button ID="exportToPDF" Text="Export To PDF" CssClass="btn btn-default" runat="server" ForeColor="#ffffff" BackColor="#b52e31" OnClick="exportToPDF_Click" />
这是背后的代码:
protected void exportToPDF_Click(object sender, EventArgs e)
{
PdfPTable pdfTable = new PdfPTable(GridView1.HeaderRow.Cells.Count);
foreach (TableCell headerCell in GridView1.HeaderRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(headerCell.Text, font));
pdfCell.BackgroundColor = new BaseColor(GridView1.HeaderStyle.BackColor);
pdfTable.AddCell(pdfCell);
}
foreach(GridViewRow gridviewRow in GridView1.Rows)
{
foreach(TableCell tblCell in gridviewRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(GridView1.RowStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
pdfCell.BackgroundColor = new BaseColor(GridView1.RowStyle.BackColor);
pdfTable.AddCell(pdfCell);
}
}
Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
pdfDocument.Open();
pdfDocument.Add(pdfTable);
pdfDocument.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=Agenda.pdf");
Response.Write(pdfDocument);
Response.Flush();
Response.End();
}
标题栏始终显示为空。
请帮助我解决这个问题。感谢。
答案 0 :(得分:0)
在这一行:
PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
对于链接列,我认为您不会填充tblCell.Text
。相反,您需要通过检查tblCell.Controls属性来检查tblCell
子控件。通过迭代tblCell.Controls
,您应该能够在那里找到您的链接控件,可能就像tblCell.Controls[0]
一样。
只需在调试器中停留标题列的该行,然后查看tblCell.Controls
包含的内容。然后将适当的链接控件转换为调试器中显示的实际链接控件类型,并访问链接控件的Text属性。
HTH