如何在Java中使用POI添加边框单元格

问题描述 投票:0回答:1

0

我正在尝试使用下面的 Java 代码使用 apache poi 在 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);
}
java excel apache-poi
1个回答
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)
,这很容易失控,并生成一个巨大的电子表格 - 因此需要花时间(但不能提供您期望的输出)

在顶部,注意 CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)

 的界面 - 特别是参数名称。在您的情况下,
firstCol
lastCol
始终为1。 
firstRow
 始终(除了第一次迭代)101,而 
lastRow
 急剧上升

© www.soinside.com 2019 - 2024. All rights reserved.