如何读取和解析 Vert.x 文件系统中的 .xlsx 文件

How to Read and Parse .xlsx file in Vert.x File System

我正在开发一个使用 angular 前端和 Vert.x 后端解析 .xlsx 文件的应用程序。我有一个 angular 组件将文件发布到 URL,Vert.x 后端处理“/upload”,这工作得很好。

但是,我需要在 Java 的后端解析这个文件,我已经使用 Apache POI 编写了一个解析器。我可以在 Vert.x 文件系统中将文件作为 AsyncFile 打开,但我不确定如何以 Apache Poi 可以理解的方式使用此 AsyncFile 对象(java.io.InputStream,java.io.File ,例如)。

有没有办法做到这一点或 Vert.x 中的功能我可以用来解析 .xlsx 文件并使用它来动态填充我的 angular 应用程序?

如果您想继续使用 Apache POI 并使用 Vertx 读取文件,那么您有两个选项可以使用 Vert.x 的文件系统 API:

非阻塞

vertx.fileSystem().readFile("/path-to-file/foo.xlsx", asyncResult -> {
    if (asyncResult.succeeded()) {
        Buffer result = asyncResult.result();
        ByteArrayInputStream bais = new ByteArrayInputStream(result.getBytes());
        // use apache poi with the input stream
    } else {
        // handle errors
    }
});

阻塞(应该避免,它可以阻塞event-loop)

Buffer result = vertx.fileSystem().readFileBlocking("/path-to-file/foo.xlsx");
ByteArrayInputStream bais = new ByteArrayInputStream(result.getBytes());
// use apache poi with the input stream