Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 与POI相关的代码块运行非常缓慢_Java_Apache Poi - Fatal编程技术网

Java 与POI相关的代码块运行非常缓慢

Java 与POI相关的代码块运行非常缓慢,java,apache-poi,Java,Apache Poi,下面是一段包含循环的代码块: 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 <

下面是一段包含循环的代码块:

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);
        }
    }
Row行=null;
Cell=null;
字符串dataVal=null;
字符串[]temp=null;
对于(int j=0;j
其中
myDataValues
字符串[]
列表,每个
字符串[]
对象包含4个值

我在RationalApplicationDeveloperVersion8和ApachePOI3.8中运行这个

myDataValues
中大约有5500个元素,我认为这是一个非常小的值

但是,此代码块需要一个多小时才能运行

我觉得这有点问题。5500个元素,每个元素包含4个元素,运行速度应该相当快,这应该是几分钟的问题。可能的原因是什么?有没有办法让这个街区跑得更快

机器的可用内存或任何其他此类问题没有问题。一切正常,我已经验证过了。问题只在这个街区

试试这个

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"));
            }
        }
    }
    for (int i = 0; i < 4; i++) {
      sheet.autoSizeColumn(i);
    }
for(int j=0;j
试试这个

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"));
            }
        }
    }
    for (int i = 0; i < 4; i++) {
      sheet.autoSizeColumn(i);
    }
for(int j=0;j
您的处理速度非常慢,因为您正在为每一行调用
autoSizeColumn
。从:

在大型板材上,此过程可能相对较慢,因此应 通常每个列只调用一次,在 处理


将对
autoSizeColumn
的调用放置在创建行的循环外部,仅在列上的
for
循环中。这将最小化对此方法的调用并提高性能。

您的处理速度非常慢,因为您正在为每一行调用
autoSizeColumn
。从:

在大型板材上,此过程可能相对较慢,因此应 通常每个列只调用一次,在 处理

将对
autoSizeColumn
的调用放置在创建行的循环外部,仅在列上的
for
循环中。这将最大限度地减少对此方法的调用并提高性能。

仅供参考

在我的例子中,我有100多万,而AutoSizeColumn仍然很慢(即使在最后)

因此,我考虑了提高性能,只存储每个值的列索引和内容长度(在字典中),而它比上次存储的值大

在所有过程结束时,只需“foreach”列表并使用sheet.SetColumnWidth设置列的宽度

  • 使用autosize:永不结束
  • 与witdh:3秒
伪码

if(!dictionary.Any(a => a.Key == columnIndex))
                {
                    dictionary.Add(columnIndex, columnContent.Length);
                }
                else if(dictionary.Any(a => a.Key == columnIndex && a.Value < columnContent.Length))
                {
                    dictionary[columnIndex] = columnContent.Length;
                }
if(!dictionary.Any(a=>a.Key==columnIndex))
{
添加(columnIndex、columnContent.Length);
}
else if(dictionary.Any(a=>a.Key==columnIndex&&a.Value
最后呢

foreach (KeyValuePair<int, int> column in dictionary)
        {
            sheet.SetColumnWidth(column.Key, column.Value*300);
        }
foreach(字典中的KeyValuePair列)
{
sheet.SetColumnWidth(column.Key,column.Value*300);
}
仅供参考

就我而言,我有1个