【问题标题】:Grouping elements while exporting dataGridView to PDF using iTextSharp使用 iTextSharp 将 dataGridView 导出为 PDF 时对元素进行分组
【发布时间】:2015-10-09 10:22:45
【问题描述】:

我正在尝试将我的 datagridview 导出为 PDF,但是在执行此操作时,我想对具有相同组名的行进行分组。 我用来导出为 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 文件,但它不能按我想要的方式工作,它没有按“组名”对列进行分组
我陷入了这个问题,任何帮助将不胜感激。

【问题讨论】:

    标签: c# winforms linq-to-sql datagridview pdf-generation


    【解决方案1】:

    您可以对结果进行排序,为每个有数据的“组”创建一个 pdfTable 吗?

    【讨论】:

    • 你能分享一个链接或发送你的建议的示例代码吗?
    【解决方案2】:

    我已经用一个小技巧解决了这个问题,我已经在一个名为“testlist”的列表中列出了所有组,所以我可以在 1 个 pdfTable 内管理处理问题 有代码sn-p:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多