提问人:tamilanban 提问时间:11/16/2023 最后编辑:Olaf Kocktamilanban 更新时间:11/17/2023 访问量:91
如何使用 POI 在 Java 中添加边框单元格
How to add border cells in Java with POI
问:
0
我正在尝试使用apache poi使用下面的Java代码在excel中添加边框。应用边界需要数小时的时间。我知道我使用了循环,因为边界必须基于它来制作,因为有要求。有没有其他方法可以减少时间消耗? x1,x2 将根据迭代不断变化
static int x1 = 1
static int x2 = 0
XSSFCellStyle style = report.createCellStyle();
style.setBorderTop(BorderStyle.MEDIUM);
style.setBorderBottom(BorderStyle.MEDIUM);
style.setBorderLeft(BorderStyle.MEDIUM);
style.setBorderRight(BorderStyle.MEDIUM);
PropertyTemplate pt = new PropertyTemplate();
for (Object cellValues : rowsSet) {
x2 = x2 + rowsSet.size();
pt.drawBorders(new CellRangeAddress(x1, x2, 1, 1),
BorderStyle.MEDIUM, BorderExtent.OUTER);
x1 = rowsSet.size() + 1;
pt.applyBorders(sheet);
}
答:
1赞
Olaf Kock
11/17/2023
#1
你希望你的循环做什么?您是否已调试过代码?
鉴于
for (Object cellValues : rowsSet) {
x2 = x2 + rowsSet.size();
pt.drawBorders(new CellRangeAddress(x1, x2, 1, 1),
BorderStyle.MEDIUM, BorderExtent.OUTER);
x1 = rowsSet.size() + 1;
pt.applyBorders(sheet);
}
如果 rowSet 的大小为 100,则 x1 和 x2 值在迭代中将如下所示:
迭 代 | x1 开始 | x1 结束 | x2 启动 | x2 结束 |
---|---|---|---|---|
0 | 1 | 101 | 0 | 100 |
1 | 101 | 101 | 100 | 200 |
2 | 101 | 101 | 200 | 300 |
3 | 101 | 101 | 300 | 400 |
... | ... | ... | ... | ... |
99 | 101 | 101 | 9900 | 10000 |
这是意料之中的吗?(假设:否)
鉴于您解决了一个 ,这很容易失控,并生成一个巨大的电子表格 - 因此需要时间(但不能提供您期望的输出)CellRangeAddress(x1, x2, 1, 1)
在顶部,请注意 - 的接口,尤其是参数名称。 在您的情况下始终为 1。 总是(除了第一次迭代)101,而急剧上升CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
firstCol
lastCol
firstRow
lastRow
评论