2017-11-18 138 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對象中的字節數。)

相關問題