保存Excel文件需要很长时间

时间:2018-05-10 14:34:40

标签: java apache-poi

我尝试打开Excel文件,写入内容然后保存。我的问题是保存部分(workbook.write)需要花费很多时间。 这是我的代码:

 FileInputStream file = new FileInputStream(f2);
 XSSFWorkbook workbook = new XSSFWorkbook(file);
 XSSFSheet sheet = workbook.getSheet("Tabelle1");
 //Some clonesheet, createrows, createcells, setting style & value
 //about 2 sheets ,10 rows * 10 cells
 workbook.write(new FileOutputStream(path)); //takes about 2 minutes
 workbook.close();

我正在生产机器上执行代码,我无法在JVM上进行任何修改。我知道堆内存非常小。 (我不能打开超过2mb的excel文件)我不知道这是否与我的问题有关。

谢谢

1 个答案:

答案 0 :(得分:0)

使用缓冲区而不是直接字段路径,例如:

public class FooRouteBuilder extends StatusAwareRouteBuilder {
    @Override
    public void configure() {
        // Here I want to have this route know how to notify something
        // that this processing has begun, but I do not want to have
        // to explicitly call a processor to make it happen, but it
        // should know what to do by virtue of extending a custom
        // route builder, if appropriate, or by some other/better
        // mechanism

        // Now conduct any route-specific logic
        from("vm:myAction")
            .process("myProcessor");

        // Now handle the status notification that this is finished...
        // Here I want to have this route know how to notify something
        // that this processing has finished
    }
}

或跟随documentation

  

如果stream是联网驱动器上的FileOutputStream或具有高   与每个写入字节相关的成本/延迟,考虑包装    BufferedOutputStream 中的OutputStream可提高写入性能。