从Spring REST API获取数据并使用AngularJS另存为Excel文件流的正确方法?

时间:2022-08-24 22:21:52

We have some Spring REST APIs which fetch data from our backend DB and return them to client as JSON data. AngularJS is the client side JavaScript framework we are using now. Now we need to save some of the data as Excel file so customers can download them. The file need to be downloaded as a stream because some of the file may be in big size. Is there any elegant solution for the problem?

我们有一些Spring REST API,它们从我们的后端数据库中获取数据,并将它们作为JSON数据返回给客户端。 AngularJS是我们现在使用的客户端JavaScript框架。现在我们需要将一些数据保存为Excel文件,以便客户可以下载它们。该文件需要作为流下载,因为某些文件可能很大。这个问题有没有优雅的解决方案?

1 个解决方案

#1


0  

I assume that you have an endpoint that generates a JSON report. Let's name it /api/reports/report1. So a request like this:

我假设您有一个生成JSON报告的端点。我们将它命名为/ api / reports / report1。所以这样的请求:

GET /api/reports/report1 HTTP/1.1
Accept: application/json

returns the report in JSON format:

以JSON格式返回报告:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8

[
    {
        "fullName": "John Doe",
        "annualIncome": 100000
    },
    (...)
]

Option 1

Extend the endpoint to produce application/vnd.ms-excel. So the conversation would go like this:

扩展端点以生成application / vnd.ms-excel。所以谈话会像这样:

GET /api/reports/report1 HTTP/1.1
Accept: application/vnd.ms-excel

HTTP/1.1 200 OK
Content-Type: application/vnd.ms-excel

(... binary excel data ...)

This will not work well if you want to embed the downloads as a HTML links because there's no way to set the Accept header then.

如果您希望将下载作为HTML链接嵌入,则无法正常工作,因为无法设置Accept标头。

Option 2

Generate download links. This will work with large downloads (which is the case here as you mentioned). You will need a different URL for the Excel representation of your report in this case though (e.g. /api/reports/report1/excel or /api/reports/report1?format=excel). Or you could go with Option 1 and make Excel the default mime for your enpoint (when Accept header is set to */* in the request).

生成下载链接。这将适用于大量下载(正如您所提到的那样)。在这种情况下,您将需要一个不同的URL来表示报告的Excel表示(例如/ api / reports / report1 / excel或/ api / reports / report1?format = excel)。或者您可以使用选项1并使Excel成为您的enpoint的默认mime(当Accept头在请求中设置为* / *时)。

Implementation

  • You can use Apache POI for generating the EXCEL files - see this SO thread about creating XLS with Java
  • 您可以使用Apache POI生成EXCEL文件 - 请参阅此SO线程,了解如何使用Java创建XLS
  • For downloading a file in angular see this and this
  • 要以角度下载文件,请参阅此和此

Additional reference

#1


0  

I assume that you have an endpoint that generates a JSON report. Let's name it /api/reports/report1. So a request like this:

我假设您有一个生成JSON报告的端点。我们将它命名为/ api / reports / report1。所以这样的请求:

GET /api/reports/report1 HTTP/1.1
Accept: application/json

returns the report in JSON format:

以JSON格式返回报告:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8

[
    {
        "fullName": "John Doe",
        "annualIncome": 100000
    },
    (...)
]

Option 1

Extend the endpoint to produce application/vnd.ms-excel. So the conversation would go like this:

扩展端点以生成application / vnd.ms-excel。所以谈话会像这样:

GET /api/reports/report1 HTTP/1.1
Accept: application/vnd.ms-excel

HTTP/1.1 200 OK
Content-Type: application/vnd.ms-excel

(... binary excel data ...)

This will not work well if you want to embed the downloads as a HTML links because there's no way to set the Accept header then.

如果您希望将下载作为HTML链接嵌入,则无法正常工作,因为无法设置Accept标头。

Option 2

Generate download links. This will work with large downloads (which is the case here as you mentioned). You will need a different URL for the Excel representation of your report in this case though (e.g. /api/reports/report1/excel or /api/reports/report1?format=excel). Or you could go with Option 1 and make Excel the default mime for your enpoint (when Accept header is set to */* in the request).

生成下载链接。这将适用于大量下载(正如您所提到的那样)。在这种情况下,您将需要一个不同的URL来表示报告的Excel表示(例如/ api / reports / report1 / excel或/ api / reports / report1?format = excel)。或者您可以使用选项1并使Excel成为您的enpoint的默认mime(当Accept头在请求中设置为* / *时)。

Implementation

  • You can use Apache POI for generating the EXCEL files - see this SO thread about creating XLS with Java
  • 您可以使用Apache POI生成EXCEL文件 - 请参阅此SO线程,了解如何使用Java创建XLS
  • For downloading a file in angular see this and this
  • 要以角度下载文件,请参阅此和此

Additional reference