如何一次导出/生成两个文件?

发布于 2024-09-11 02:08:05 字数 801 浏览 3 评论 0原文

我需要一次生成两个excel文件。我们使用 Apache POI 生成 excel 文件和 struts 框架。

在操作类中,步骤如下,

OutputStream outputStream = response.getOutputStream();

然后从数据库填充数据并使用 POI 创建一个文件并调用,

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
                 "attachment; filename=ExportFile1.xls");
workbook.write(outputStream);
outputStream.flush();

并对第二个文件执行相同的步骤(从数据库填充数据,写入 excel 内容),

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
                 "attachment; filename=ExportFile2.xls");
workbook.write(outputStream);
outputStream.flush();

但只生成一个文件。

java中可以同时生成两个文件吗?有人实施过这个吗?

请注意:不需要在一个 Excel 文件中生成两张工作表。

I have an requirement of generating two excel files at once. We are using Apache POI for generating excel files and struts framework.

Within the action class, the steps are like,

OutputStream outputStream = response.getOutputStream();

and then populate data from db and created one file using POI and called,

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
                 "attachment; filename=ExportFile1.xls");
workbook.write(outputStream);
outputStream.flush();

and followed same steps(populate data from db, write excel content) for second file as,

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
                 "attachment; filename=ExportFile2.xls");
workbook.write(outputStream);
outputStream.flush();

But only one file is generated.

Is this possible in java to generate two files at once? Have anyone implemented this?

Pls note: Generating two sheets within one excel file is not required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被你宠の有点坏 2024-09-18 02:08:05

我需要一次生成两个Excel文件。

我有类似的要求,但这并不是“一键生成”,因为如果向用户显示两个或多个文件的“另存为”对话框,最终用户会感到非常困惑。

  1. 因此有一个“生成”按钮,它将生成文件(如果该过程需要更多时间,则带有进度条),并且不会重定向到包含这些多个文件的单独链接的另一个页面(或弹出窗口),因此用户可以逐个获取它们,或者仅获取所需的文件。

  2. 如果用户仍然需要“只需单击一下”,那么压缩所有文件是标准过程。为了提高可用性,可以将其与场景 #1 中的屏幕或弹出窗口结合/改进:例如,显示每个文件的带有复选框的列表,以及底部的“以 zip 形式获取所有内容”的链接。这样,用户可以下载单独的文件,但也可以仅选择感兴趣的文件,或者只是获取全部文件。

这就是大多数网络应用程序的做法,因为它是最用户友好的。

I have an requirement of generating two excel files at once.

I had similar requirements, but this wasn't to "generate with one click", because if the user would be presented with "save as" dialogs for two or more files would be very confusing to the end-user.

  1. So there was a "generate" button, that would generate the files (with a progress bar if the procedure takes more time) and than redirect to another page (or popup) that would contain individual links of those multiple files, so the user could take them one by one, or only the desired ones.

  2. If the user still needs to get "with only one click" than zipping all files is the standard procedure. For more usability, that could be combined/improved with the screen or popup from scenario #1 : e.g. to show a list with checkboxes for each file, and a link at the bottom to "get all as a zip". That way, the user could download individually files, but also select only the files is interested in, or simply just get all.

This is how most webapps do it since it's the most user friendly.

陈年往事 2024-09-18 02:08:05

这个问题的核心是如何在 HTTP 响应中发送多个文件。默认情况下不会发生这种情况。如果您执行当前正在执行的操作,您(自然)最终会得到一个下载的文件,其中包含两个 Excel 工作簿的串联,并且浏览器没有简单的方法将它们分开。

为了发送 2 个不同的 Excel 文件,您需要使用浏览器知道如何取消选择的某种模式对它们进行编码。一种方法是发送具有“多部分”内容类型的响应,如 RFC1341 第 7.2 节。我希望有人可以加入一个适当的 java 库来负责创建多部分内容流。

另一种方法是将两个(或更多)文件编码为 ZIP 存档。 Java 类库包含用于以编程方式创建 ZIP 存档的类。

The nub of this question is how to send multiple files in an HTTP response. This won't happen by default. And if you do what you are currently doing you (naturally) end up with one downloaded file containing a concatenation of the two excel workbooks, and no easy way for the browser to separate them.

In order to do send 2 distinct excel files, you would need to encode them using some schema that the browser knows how to unpick. One way to do this is to send a response with a "multi-part" content type as described in RFC1341 section 7.2. I expect someone can chime in with an appropriate java library that will take care of creating the multi-part content stream.

Another way would be to encode the two (or more) files as a ZIP archive. The Java class library has classes for creating a ZIP archive programmatically.

江心雾 2024-09-18 02:08:05

我对 Apache POI 不熟悉。但是,您需要有两个 OutputStream 才能写入两个文件。

使用您提供的代码,看起来您所做的就是将文件的标头从“ExportFile1.xls”更改为“ExportFile2.xls”,然后写出文件内容(标头声称将 file2) 获取到流。我怀疑您还需要第二个流的第二个共振对象。

I'm not familiar with the Apache POI. However, you need to have two OutputStreams in order to write to two files.

With the code you've provided, it looks like all you've done is change the header of the file from thinking it's "ExportFile1.xls" to "ExportFile2.xls", and then written out the file contents (with a header claiming to get file2) to a stream. I suspect you'll need a second resonse object for the second stream, too.

层林尽染 2024-09-18 02:08:05

您只能发回一个文件。

在你的情况下,我会生成一个包含两个 Excel 文件的 zip 文件并将其返回。最初使用文件进行操作,如果需要,您可以在内存中完成所有操作。

You can only send a single file back.

In your case I would generate a zip file containg the two excel files and return that. Dó it with files initially and if needed you Can dó it All in memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文