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

huangapple go评论90阅读模式
英文:

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

问题

我正在开发一个应用程序,使用Angular前端和Vert.x后端来解析.xlsx文件。我有一个Angular组件,将文件发布到由Vert.x后端处理的URL "/upload",这运行得很好。

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

是否有一种方法可以做到这一点,或者在Vert.x中是否有一个功能可以用来解析.xlsx文件,并将其用于动态填充我的Angular应用程序?

英文:

I am working on an application to parse a .xlsx file using an angular front-end and a Vert.x back-end. I have an angular component that posts the file to a URL that the Vert.x back-end handles "/upload" and this works just fine.

However, I need to parse this file within the back-end in Java and I have already written a Parser using Apache POI. I can open the file as an AsyncFile in the Vert.x File System, but I am not sure how to use this AsyncFile object in a way that Apache Poi can understand it (java.io.InputStream, java.io.File, for example).

Is there a way to do this or a functionality in Vert.x I can use to parse the .xlsx file and use it to dynamically populate my angular application?

答案1

得分: 1

如果您想继续使用Apache POI并且使用Vert.x来读取文件,那么您有两种选项可以与Vert.x的FileSystem API一起使用:

非阻塞方式

vertx.fileSystem().readFile("/path-to-file/foo.xlsx", asyncResult -> {
    if (asyncResult.succeeded()) {
        Buffer result = asyncResult.result();
        ByteArrayInputStream bais = new ByteArrayInputStream(result.getBytes());
        // 使用输入流进行Apache POI操作
    } else {
        // 处理错误
    }
});

阻塞方式(应避免使用,它可能会阻塞事件循环)

Buffer result = vertx.fileSystem().readFileBlocking("/path-to-file/foo.xlsx");
ByteArrayInputStream bais = new ByteArrayInputStream(result.getBytes());
// 使用输入流进行Apache POI操作
英文:

If you want to keep using Apache POI and use Vertx to read the file than you have two options to work with Vert.x's FileSystem API:

Non Blocking

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
    }
});

Blocking (Should be avoided, it can block the 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

huangapple
  • 本文由 发表于 2020年9月2日 02:53:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63693789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定