如何让用户将数据库中的数据从 Java/Struts 中的 Web 应用程序下载到 Excel 表格文件中? [英] How to let user download the data in database to an excel sheet file from web application in Java/Struts?

查看:37
本文介绍了如何让用户将数据库中的数据从 Java/Struts 中的 Web 应用程序下载到 Excel 表格文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个报告,其中包含一个从数据库中的数据生成的 Excel 表.我正在使用 Apache POI HSSF 在模型中创建 Excel 表格文件.

I want to generate a report which includes an excel sheet which is generated from the data from the database . I am using Apache POI HSSF for creating the excel sheet file in the model.

现在如何让用户下载我创建的文件?

Now how to let the user download the file i have created ?

推荐答案

只需使用 servlet.将 response.getOutputStream() 馈送到 POI HSSF 以将工作簿写入其中.最重要的一点是 Content-Disposition 响应头.如果你把它设置为attachment,那么浏览器会弹出一个另存为对话框.

Just use a servlet. Feed response.getOutputStream() to POI HSSF to write the workbook to. Most important bit is the Content-Disposition response header. If you set it to attachment, then the browser will pop a Save As dialogue.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=name.xls");
WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());
// ...

然后让下载 URL 指向该 servlet,如有必要,使用一些请求参数或路径信息.

Then let the download URL point to that servlet, if necessary with some request parameters or path info.

这篇关于如何让用户将数据库中的数据从 Java/Struts 中的 Web 应用程序下载到 Excel 表格文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆