我正在使用 itextsharp 生成动态事件列表的 pdf。我已将 gridview 设置为 visible = false 但我的理解是我需要一个 gridview 才能导出为 pdf。
我打开pdf,它是空白的,就像网格中的信息没有正确复制一样。我可能错过了使用 gridview 进行数据绑定的步骤。
这是网格视图:
<asp:GridView BackColor="Green" ID="gvEventCaf" runat="server" visible="false" >
<Columns>
<asp:TemplateField HeaderText="EventName"
SortExpression="EventName" >
<ItemTemplate>
<%# Eval("EventName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EventDate"
SortExpression="EventDate">
<ItemTemplate>
<%# Eval("EventDate") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EventTime"
SortExpression="EventTime">
<ItemTemplate>
<%# Eval("EventTime") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description"
SortExpression="Description">
<ItemTemplate>
<%# Eval("Description") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>`
这是代码隐藏:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
public partial class Objects_EventList : System.Web.UI.UserControl
{
public string city;
public int showcount;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Load_Events();
};
}
protected void Load_Events()
{
EventsDataContext edc = new EventsDataContext();
var events = (from e in edc.tblEvents_Cafes
where e.EventDateTime >= DateTime.Now && e.VenueCity.Trim() == city.Trim() && (e.VenueName.Contains("Café") || e.VenueName.Contains("Cafe") )
orderby e.EventDateTime
select new {
EventName = e.EventName,
EventDate = e.EventDate,
EventTime = e.EventTime,
Description = edc.tblEvents_Cafe_Descriptions.OrderBy(d => d.Priority).Where(d => d.Keywords.ToLower() == e.EventName.ToLower()).Select(d => d.Description).First(), // edc.tblEvents_Cafe_Descriptions.OrderBy(d => d.Priority).Where(d => d.Keywords.ToLower() == e.EventName.ToLower() || d.Keywords.ToLower().CompareTo(e.EventName.ToLower()) >= 0).Select(d => d.Description).First()
}).Take(showcount);
gvEventCaf.DataSource = events;
gvEventCaf.DataBind();
}
protected void btnGenerateReport(object sender, EventArgs e)
{
gvEventCaf.DataBind();
//Create a table
iTextSharp.text.Table table = new iTextSharp.text
.Table(gvEventCaf.Columns.Count);
table.Cellpadding = 5;
//Transfer rows from GridView to table
for (int i = 0; i < gvEventCaf.Rows.Count; i++)
{
if (gvEventCaf.Rows[i].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < gvEventCaf.Columns.Count; j++)
{
string cellText = Server.HtmlDecode
(gvEventCaf.Rows[i].Cells[j].Text);
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
//Set Color of Alternating row
if (i % 2 != 0)
{
cell.BackgroundColor = new Color(System.Drawing
.ColorTranslator.FromHtml("#C2D69B"));
}
table.AddCell(cell);
}
}
}
//Create the PDF Document
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" +
"filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}