2015-07-20 82 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