2017-11-18 139 views
-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(); 
     } 

回答

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(); 
    } 

可能有一些其他的问题,但我希望你明白的逻辑错误:

  • 你每次进入循环时创建一个新的PDF文件与PdfWriter。如果您只想创建一个PDF,则只应创建一个PdfWriter实例。
  • 最好在MemoryStream中创建PDF,然后将该流的内容作为二进制流写入Response对象。我真的不明白你在那里做什么(以及你为什么试图这样做)。
  • 我还介绍了包含20个用户单元的SpacingAfter,否则它看起来好像所有表都粘在一起成为一个大表。

将文件发送到Response的方式可能仍然存在一些错误,但这应该已经让您顺利。 (你为什么不把文件大小发送给浏览器?你知道这个大小,不是吗?它是content对象中的字节数。)

相关问题