一个与 POI 相关的代码块运行缓慢 [英] A POI related code block running dead slow
问题描述
我有以下包含循环的代码块:
I have below piece of code block containing loops:
Row row = null;
Cell cell = null;
String dataVal = null;
String[] temp = null;
for (int j = 0; j < this.myDataValues.size(); j++) {
row = sheet.createRow(rownum++);
temp = this.finalRowValues.get(j);
for (int i = 0; i < 4; i++) {
cell = row.createCell(i);
dataVal = temp[i];
if (NumberUtils.isNumber(dataVal)) {
double d = Double.valueOf(dataVal);
cell.setCellValue(d);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellStyle(styles.get("currency"));
} else if (isValidDate(dataVal)) {
cell.setCellValue(dataVal);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellStyle(styles.get("date"));
} else {
cell.setCellValue(temp[i]);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellStyle(styles.get("data"));
}
sheet.autoSizeColumn(i);
}
}
其中 myDataValues
是 String[]
的 List
,每个 String[]
对象包含 4 个值.
Where myDataValues
is a List
of String[]
with each String[]
object containing 4 values.
我在 Rational Application Developer 版本 8 和 Apache POI 3.8 中运行它.
I am running this in Rational Application Developer version 8 and Apache POI 3.8.
myDataValues
中大约有 5500 个元素,我认为这是一个非常小的值.
There are around 5500 elements in myDataValues
which is a pretty small value I believe.
然而,这个代码块需要一个多小时才能运行.
However, this code block is taking more then a hour to run.
我认为这有问题.每个包含 4 个元素的 5500 个元素应该运行得非常快,并且应该是几分钟的问题.可能的原因是什么?有没有办法让这个块运行得更快?
I think there is something wrong with this. 5500 elements with each containing 4 elements should run pretty fast and should be a question of several minutes. What could be the possible cause? Is there a way to make this block run faster?
机器的可用内存或任何其他此类问题都没有问题.一切都按预期工作,我已经验证过了.问题仅在此块中.
There is nothing wrong with available memory of the machine or any other such issues. Everything is working as expected and I have verified it. The issue is in this block only.
推荐答案
您的处理速度非常慢,因为您要为每一行调用 autoSizeColumn
.来自 的 JavadocsautoSizeColumn
方法:
Your processing is very slow because you're calling autoSizeColumn
for every row. From the Javadocs for the autoSizeColumn
method:
这个过程在大纸张上可能相对较慢,所以这应该通常每列只调用一次,在你的最后处理.
This process can be relatively slow on large sheets, so this should normally only be called once per column, at the end of your processing.
将对 autoSizeColumn
的调用放在创建行的循环之外,在它自己的 for
循环中只针对列.这将最大限度地减少对此方法的调用并提高您的性能.
Place the calls to autoSizeColumn
outside of the loop that creates the rows, in its own for
loop only on the columns. This will minimize calls to this method and improve your performance.
这篇关于一个与 POI 相关的代码块运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!