How to let user download the data in database to an excel sheet file from web application in Java/Struts?

北战南征 提交于 2019-12-13 21:56:05

问题


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 ?


回答1:


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());
// ...

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



来源:https://stackoverflow.com/questions/5177725/how-to-let-user-download-the-data-in-database-to-an-excel-sheet-file-from-web-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!