【发布时间】:2015-01-05 15:23:37
【问题描述】:
我正在尝试将旧的 XLS 表生成器转换为 PDF 表生成器。而在 HSSFWOrkbook 中实际上可能在创建时设置单元格索引。但是我怎样才能对 PDF 中的 PdfPTable 做同样的事情呢? 只有方法可用 .addCell()。没有太多。
【问题讨论】:
我正在尝试将旧的 XLS 表生成器转换为 PDF 表生成器。而在 HSSFWOrkbook 中实际上可能在创建时设置单元格索引。但是我怎样才能对 PDF 中的 PdfPTable 做同样的事情呢? 只有方法可用 .addCell()。没有太多。
【问题讨论】:
您可以使用 PdfCell 数组来做到这一点:
ncols = 6; // number of columns
nrows = 8; // number of rows
Pdfcell[][] cells = new PdfCell[nrows][ncols];
// To create the table cells
for(int icol=0; icol<ncols; icol++) {
for(int irow=0; irow<nrows; irow++) {
cells[irow][icol] = new PdfCell(new Phrase(
"Col:" + icol + ", Row:" + irow));
}
}
// To add the cells to table
for(int irow=0; irow<nrows; irow++) {
for(int icol=0; icol<ncols; icol++) {
table.addCell(cells[irow][icol]);
}
}
【讨论】: