我正在尝试使用iTextSharp将多个网格视图导出为单个pdf。我循环遍历网格视图,然后循环遍历gridview的行。循环正常。但是在pdf下载之后,只能看到最后一个gridview。看起来网格视图正在相互覆盖,只剩下最后一个。这是我的代码。我做错了什么?
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();
}
答案 0 :(得分:1)
您的一个错误不是iText错误;这是一个简单的逻辑错误,可以用常识解决。另一个错误很奇怪。您没有正确使用Response
。
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);
MemoryStream ms = new MemoryStream();
PdfWriter.GetInstance(pdfDoc, ms);
pdfDoc.Open();
for (int i = 0; i < gvExcel.Length; i++)
{
if (gvExcel[i].Visible)
{
PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count);
pdfTbl.SpacingAfter = 20f;
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);
}
}
pdfDoc.Add(pdfTbl);
}
}
pdfDoc.Close();
byte[] content = ms.ToArray();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=report_" + startDate + "-" + endDate + ".pdf");
Response.BinaryWrite(content);
Response.Flush();
Response.End();
}
可能还有其他一些问题,但我希望您理解逻辑错误:
PdfWriter
创建一个新的PDF文件。如果您只想创建一个PDF,则只应创建一个PdfWriter
实例。MemoryStream
中创建PDF,然后将该流的内容作为二进制流写入Response
对象。我真的不明白你在那里做了什么(为什么你试图这样做)。SpacingAfter
,否则看起来好像所有的表都粘在一起就是一张大表。将文件发送到Response
的方式可能仍然存在一些错误,但这应该可以让您继续前进。 (为什么不将文件大小发送到浏览器?你知道那个大小,不是吗?这是content
对象中的字节数。)