2015-07-20 92 views
0

我试图将我的datagridview导出为PDF,但是这样做时我想将具有相同组名称的行分组。 我用来输出为pdf的代码位于下方;使用iTextSharp将dataGridView导出为PDF时将元素分组

private void PrintReport_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      //create iTextSharp table 
      PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount); 
      pdfTable.DefaultCell.Padding = 3; 
      pdfTable.WidthPercentage = 30; 
      pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; 
      pdfTable.DefaultCell.BorderWidth = 1; 
      //Adding Header row 
      PdfPCell cell = new PdfPCell(new Phrase("Report")); 
      cell.Colspan = 11; 
      cell.BackgroundColor = new iTextSharp.text.Color(50, 50, 120); 
      cell.HorizontalAlignment = 1; 
      pdfTable.TotalWidth = 1200f; 
      pdfTable.LockedWidth = true; 
      pdfTable.AddCell(cell); 
      pdfTable.AddCell("Group"); 
      pdfTable.AddCell("Numara"); 
      pdfTable.AddCell("Müşteri ID"); 
      pdfTable.AddCell("Tanım"); 
      pdfTable.AddCell("IP Adresi"); 
      pdfTable.AddCell("Kullanıcı"); 
      pdfTable.AddCell("Şifre"); 
      pdfTable.AddCell("Domain"); 
      pdfTable.AddCell("2.IP"); 
      pdfTable.AddCell("2.Kullanıcı"); 
      pdfTable.AddCell("2.Kullanıcı Şifre"); 


      //Adding DataRow 
      for (int i = 0; i < dataGridView1.Rows.Count; i++) 
      { 
       for (int j = 0; j < dataGridView1.Columns.Count; j++) 
       { 
        if (dataGridView1.Rows[i].Cells[j].Value != null) 
        { 
         if (j == 6|| j == 10) 
         { 
          pdfTable.AddCell("*****");         
         } 
         else if(j==0) 
         { 
          pdfTable.AddCell(dataGridView1.Rows[i].Cells[6].Value.ToString()); 
         } 
         else if(j==6) 
         { 
          pdfTable.AddCell(dataGridView1.Rows[i].Cells[0].Value.ToString()); 
         } 
         else 
         { 
          pdfTable.AddCell(dataGridView1.Rows[i].Cells[j - 1].Value.ToString());         
         } 
        } 
        else 
        { 
         pdfTable.AddCell(" "); 
        } 
       } 
      } 

      //pdfTable.AddCell(cells.Value.ToString()); 
      //Exporting to PDF 
      string folderPath = "C:\\PDFs\\"; 
      if (!Directory.Exists(folderPath)) 
      { 
       Directory.CreateDirectory(folderPath); 
      } 
      using (FileStream stream = new FileStream(folderPath + "Rapor.pdf", FileMode.Create)) 
      { 
       Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f); 
       PdfWriter.GetInstance(pdfDoc, stream); 
       pdfDoc.Open(); 
       pdfDoc.Add(pdfTable); 
       pdfDoc.Close(); 
       stream.Close(); 
      } 

      MessageBox.Show("C:\\PDFs uzantısına rapor kaydedildi!"); 
     } 
     catch (Exception msg) 
     { 
      MessageBox.Show(msg.Message, "Error"); 
     } 

    } 

代码工作得很好,它导出的datagridview为PDF文件,但它不工作的方式我想,它通过“组名称”
不列群组我被困在这个问题上的任何帮助将不胜感激。

回答

0

我有一个小窍门,解决了这个问题,我已经列出的所有组名为“testlist”所以我能管理内1的处理sutiation pdfTable 有代码片段列表:

for (int element = 0; element < testList.Count;element++) 
      { 
       string name = testList.ElementAt(element).ToString(); 
       PdfPCell cell1 = new PdfPCell(new Phrase(name)); 
       cell1.BackgroundColor = new iTextSharp.text.Color(160, 160, 210); 
       cell1.Colspan = 11; 
       cell1.HorizontalAlignment = 1; 
       pdfTable.AddCell(cell1); 
       for (int i = 0; i < dataGridView1.Rows.Count; i++) 
       { 
        for (int j = 0; j < dataGridView1.Columns.Count; j++) 
        { 
         if (dataGridView1.Rows[i].Cells[j].Value != null) 
         { 
          if(dataGridView1.Rows[i].Cells[6].Value.ToString() == name.ToString()) 
          { 
           if (j == 6 || j == 10) 
           { 
            pdfTable.AddCell("*****"); 
           } 
           else if (j == 0) 
           { 
            pdfTable.AddCell(dataGridView1.Rows[i].Cells[6].Value.ToString()); 
           } 
           else if (j == 6) 
           { 
            pdfTable.AddCell(dataGridView1.Rows[i].Cells[0].Value.ToString()); 
           } 
           else 
           { 
            pdfTable.AddCell(dataGridView1.Rows[i].Cells[j - 1].Value.ToString()); 
           } 

          }         
         } 
         else 
         { 
          pdfTable.AddCell(" "); 
         } 

        } 
       } 

      } 
0

你可以对结果进行排序,为每个有数据的'组'创建pdfTable?

+0

你能分享一个链接或发送你的建议样本代码吗? – BarisY