如何在更短的时间内使用Java添加边框单元格?

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

我正在尝试使用下面的 Java 代码使用 apache poi 在 Excel 中添加边框。应用边界需要几个小时的时间。还有其他方法可以减少时间消耗吗?

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.ALL);   
    x1 = rowsSet.size() + 1;
    pt.applyBorders(sheet);
}
java excel apache-poi border border-layout
1个回答
0
投票

PropertyTemplate 用于在 CellRangeAddress 指定的单元格范围内的单元格周围绘制边框。所以它明确不是用于在单元格循环中使用。

代码的耗时是由于在循环中错误地使用

PropertyTemplate
而导致的,其中它多次一次又一次地绘制相同的边框。

请参阅无法完成 apache poi 中的单元格边框以了解正确用法。

我猜你的正确用法是这样的:

...
int startDataRow = 1; // row 2 (index 0-based)
int endDataRow = startDataRow + rowsSet.size(); // startDataRow plus count of data rows (index 0-based)
int startDataColumn = 1; // column B (index 0-based)
int endDataColumn = 1; // column B (index 0-based)
...
PropertyTemplate propertyTemplate = new PropertyTemplate();
propertyTemplate.drawBorders(new CellRangeAddress(startDataRow, endDataRow, startDataColumn, endDataColumn), 
    BorderStyle.MEDIUM, BorderExtent.ALL);

propertyTemplate.applyBorders(sheet);
© www.soinside.com 2019 - 2024. All rights reserved.