简体   繁体   English

我需要知道如何从Servlet输出Excel文件

[英]I need to know how to output an excel file from a servlet

So, I am writing a java servlet. 因此,我正在编写一个Java servlet。 The user goes to the servlet and they automatically are prompted to download or open the excel file. 用户转到servlet,并自动提示他们下载或打开excel文件。 How do I do this? 我该怎么做呢? I have the excel file in my webcontent folder. 我的webcontent文件夹中有excel文件。 I know it's pretty elementary, but I've always been bad at the file and outputstream and inputstream stuff. 我知道这很基础,但是我一直对文件,输出流和输入流东西很不好。

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=sampleName.xls");

and then pipe the file contents to the response.getOutputStream 然后将文件内容通过管道传输到response.getOutputStream

I have the excel file in my webcontent folder 我的webcontent文件夹中有excel文件

Just link to it directly. 只需直接链接到它。 The servletcontainer's builtin default servlet will then worry about the streaming and setting the proper headers. 然后,servlet容器的内置默认servlet将担心流传输和设置适当的头。

<a href="filename.xls">Download filename.xls</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM