简体   繁体   English

Excel通过Java Servlet下载

[英]Excel download through java servlet

I have a requirement of downloading excel sheet(HSSF workbook). 我需要下载Excel工作表(HSSF工作簿)。 In the response , I have set the content type and header as 在响应中,我将内容类型和标头设置为

response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment; filename=user.xls");
        OutputStream out = response.getOutputStream();
        workBook.write(out);

When ever i click the button, it is calling the servlet but the excel is not getting downloaded. 每当我单击该按钮时,它都在调用servlet,但未下载excel。 In alert I'm getting the response like 在警报中,我得到像

??????????????????????????????????????????? ?????????????????????????????????????????????? Root ???? 根 ???? Entry WorkBook ??????????????????????????????????????????????????????????????????????? 入门工作簿??????????????????????????????????????????????????? ???????????????????????????

Suggest me any ideas to resolve the issue. 向我提出任何解决问题的想法。 Thanks in advance. 提前致谢。

Try to specify the mime type. 尝试指定mime类型。 For example: 例如:

response.setContentType("application/octet-stream");

For a more general solution try to use this: 对于更通用的解决方案,请尝试使用此方法:

    String mimeType = URLConnection
            .guessContentTypeFromName(file.getName());
    if (mimeType == null) {
        mimeType = "application/octet-stream";
    }

    response.setContentType(mimeType);

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

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