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

查看:313
本文介绍了如何让用户在数据库中的数据下载到从Web应用程序是Java / Struts的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写工作簿。最重要的一点是内容处置响应头。如果你把它设置为附件,那么浏览器就会弹出一个的另存为的对话。

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.

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

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