iText 当单元格中的数据相同时如何合并单元格
iText How to merge cells when the data in the cells is the same
当单元格中的数据相同时如何合并单元格?我想合并具有相同值的后续单元格,例如 Header 1
.
正如 mkl 在他的评论中解释的那样,iText 不会自动检测和合并具有相同内容的单元格。您必须在数据中进行这些检查并相应地设置必要的行跨度值。确切的实现将取决于您的 table 数据的数据结构,但从概念上讲它看起来像这样:
PdfWriter w = new PdfWriter("SO67333436.pdf");
PdfDocument pdfDoc = new PdfDocument(w);
Document doc = new Document(pdfDoc);
String[,] tabledata = new String[,] {
{ "Header 1", "Row 1" },
{ "Header 1", "Row 2" },
{ "Header 1", "Row 3" },
{ "Header 2", "Row 4" },
{ "Header 2", "Row 5" },
{ "Header 3", "Row 6" },
{ "Header 3", "Row 7" },
{ "Header 3", "Row 8" },
{ "Header 3", "Row 9" }
};
// Add the table without any preprocessing
doc.Add(new Paragraph("Table without any rowspans:"));
Table regulartable = new Table(2);
for (int r = 0; r < tabledata.GetLength(0); r++)
{
for (int c = 0; c < tabledata.GetLength(1); c++) {
String celldata = tabledata[r,c];
regulartable.AddCell(new Cell().Add(new Paragraph(celldata)));
}
}
doc.Add(regulartable);
doc.Add(new Paragraph("Table with rowspans:"));
Table headertable = new Table(2);
int nextheader = 0;
for (int row = 0; row < tabledata.GetLength(0); row++)
{
for (int column = 0; column < tabledata.GetLength(1); column++)
{
String celldata = tabledata[row, column];
// first column
if (column == 0)
{
// skip cells that have been merged
if (row >= nextheader)
{
int rowspan = 1;
// Check how many subsequent rows have the same cell content
while (row + rowspan < tabledata.GetLength(0) && celldata.Equals(tabledata[row + rowspan,column]))
{
rowspan++;
}
nextheader = row + rowspan;
// Add the cell with the appropriate rowspan
Cell cell = new Cell(rowspan, 1).Add(new Paragraph(celldata));
headertable.AddCell(cell);
}
}
// other columns
else
{
Cell cell = new Cell().Add(new Paragraph(celldata));
headertable.AddCell(cell);
}
}
}
doc.Add(headertable);
doc.Close();
结果输出:
当单元格中的数据相同时如何合并单元格?我想合并具有相同值的后续单元格,例如 Header 1
.
正如 mkl 在他的评论中解释的那样,iText 不会自动检测和合并具有相同内容的单元格。您必须在数据中进行这些检查并相应地设置必要的行跨度值。确切的实现将取决于您的 table 数据的数据结构,但从概念上讲它看起来像这样:
PdfWriter w = new PdfWriter("SO67333436.pdf");
PdfDocument pdfDoc = new PdfDocument(w);
Document doc = new Document(pdfDoc);
String[,] tabledata = new String[,] {
{ "Header 1", "Row 1" },
{ "Header 1", "Row 2" },
{ "Header 1", "Row 3" },
{ "Header 2", "Row 4" },
{ "Header 2", "Row 5" },
{ "Header 3", "Row 6" },
{ "Header 3", "Row 7" },
{ "Header 3", "Row 8" },
{ "Header 3", "Row 9" }
};
// Add the table without any preprocessing
doc.Add(new Paragraph("Table without any rowspans:"));
Table regulartable = new Table(2);
for (int r = 0; r < tabledata.GetLength(0); r++)
{
for (int c = 0; c < tabledata.GetLength(1); c++) {
String celldata = tabledata[r,c];
regulartable.AddCell(new Cell().Add(new Paragraph(celldata)));
}
}
doc.Add(regulartable);
doc.Add(new Paragraph("Table with rowspans:"));
Table headertable = new Table(2);
int nextheader = 0;
for (int row = 0; row < tabledata.GetLength(0); row++)
{
for (int column = 0; column < tabledata.GetLength(1); column++)
{
String celldata = tabledata[row, column];
// first column
if (column == 0)
{
// skip cells that have been merged
if (row >= nextheader)
{
int rowspan = 1;
// Check how many subsequent rows have the same cell content
while (row + rowspan < tabledata.GetLength(0) && celldata.Equals(tabledata[row + rowspan,column]))
{
rowspan++;
}
nextheader = row + rowspan;
// Add the cell with the appropriate rowspan
Cell cell = new Cell(rowspan, 1).Add(new Paragraph(celldata));
headertable.AddCell(cell);
}
}
// other columns
else
{
Cell cell = new Cell().Add(new Paragraph(celldata));
headertable.AddCell(cell);
}
}
}
doc.Add(headertable);
doc.Close();
结果输出: