-1
我想用iTextSharp将多个gridviews导出到单个pdf中。我正在循环访问GridView,然后遍历GridView的行。循环会好起来的。但在下载pdf后,只能看到最后一个gridview。看来gridviews互相覆盖,只剩下最后一个了。这是我的代码。我究竟做错了什么?使用iTextsharp多个gridviews到单个pdf
protected void btnExportToPDF_Click(object sender, EventArgs e)
{
GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 };
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
for (int i = 0; i < gvExcel.Length; i++)
{
if (gvExcel[i].Visible)
{
PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count);
foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
pdfTbl.AddCell(pdfCell);
}
foreach (GridViewRow gvRow in gvExcel[i].Rows)
{
foreach (TableCell tblCell in gvRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor);
pdfTbl.AddCell(pdfCell);
}
}
//Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(pdfTbl);
}
}
pdfDoc.Close();
//Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=report_" + startDate + "-" + endDate + ".pdf");
Response.Write(pdfDoc);
Response.Flush();
Response.End();
}