发送Excel作为响应以在Java Rest Service中下载时要设置的编码类型是什么 [英] What is the encoding type to be set while sending excel as response for download in java rest service

查看:247
本文介绍了发送Excel作为响应以在Java Rest Service中下载时要设置的编码类型是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送Excel文件作为对JAVA Rest服务中下载Ajax请求的响应.但是编码类型似乎不正确. 这是我的Java类

I'm trying too hard to send excel file as a response to an ajax request for download in JAVA rest service. But the encoding type seems to be incorrect . Here is my java class

@Path("/ExcelExport")
public class ExportExcel {
    @POST
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    @Path("/export")
    public Response getBrowserLanguage(String meterdata) throws JSONException
    {
         JSONObject output = new JSONObject(meterdata);
         JSONArray gridArray = output.getJSONArray("finalJsonObj");
         JSONObject gridRow=null;
         ResponseBuilder response=null;
        try {
         final HSSFWorkbook workbook = new HSSFWorkbook();
         HSSFSheet sheet = workbook.createSheet("Sheet1"); 
         String colNames[]=null;
         Row row =null;
         Cell cell = null;
         int cellnum = 0;
         if(gridArray.length()>0){           
             row = sheet.createRow(0);
             gridRow=(JSONObject)gridArray.get(0);
             colNames=JSONObject.getNames(gridRow);
             for (String colName: colNames) {
                 cell = row.createCell(cellnum++);
                 cell.setCellValue(colName);
             }
             for (int i=0;i<gridArray.length();i++) {
                 row = sheet.createRow(i+1);
                 cellnum = 0;
                 gridRow=(JSONObject)gridArray.get(i);
                 for (String colName: colNames) {
                     cell = row.createCell(cellnum++);
                     cell.setCellValue(gridRow.getString(colName));
                 }
             }
         }
         response= Response.ok(new StreamingOutput() {
            @Override
            public void write(OutputStream outputStream) throws IOException,
                    WebApplicationException {
                workbook.write(outputStream);
            }
        },"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");     
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return response.header("Content-Disposition","attachment; filename=export.xls").build();
    }
}

在Xmlhttprequest的成功函数中,执行以下操作:

And in the success function of Xmlhttprequest I do the following :

window.location = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheetExpor,' + xmlhttp.responseText;

打开excel文件,但编码似乎不同,因此它向我显示了一些垃圾文本.

The excel file opens but the encoding seems to be different hence it shows me some junk text .

预先感谢

推荐答案

根据您的评论,我了解到您希望用户下载excel文件,而不是内联显示.您可以在Servlet中添加以下代码.

From your comment , I understand that you want the user to download the excel file rather than showing it inline . You can add the below code in your Servlet .

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

这篇关于发送Excel作为响应以在Java Rest Service中下载时要设置的编码类型是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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