使用 iTextSharp 将 dataGridView 导出为 PDF 时对元素进行分组
Grouping elements while exporting dataGridView to PDF using iTextSharp
我正在尝试将我的 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 文件,但它没有按我想要的方式工作,它没有按 'Group Name'
对列进行分组
我陷入了这个问题,我们将不胜感激。
你能不能只对结果进行排序,为每个有数据的 'group' 创建一个 pdfTable?
我用一个小技巧解决了这个问题,我在一个名为 'testlist' 的列表中列出了所有组,这样我就可以在 1 个 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(" ");
}
}
}
}
我正在尝试将我的 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 文件,但它没有按我想要的方式工作,它没有按 'Group Name'
对列进行分组
我陷入了这个问题,我们将不胜感激。
你能不能只对结果进行排序,为每个有数据的 'group' 创建一个 pdfTable?
我用一个小技巧解决了这个问题,我在一个名为 'testlist' 的列表中列出了所有组,这样我就可以在 1 个 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(" ");
}
}
}
}