2

JAVAレストサービスでダウンロードするためのajaxリクエストへの応答としてExcelファイルを送信しようとしています。しかし、エンコーディング タイプが間違っているようです。ここに私のJavaクラスがあります

@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 の成功関数では、次のことを行います。

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

Excel ファイルが開きますが、エンコーディングが異なるように見えるため、ジャンク テキストが表示されます。

前もって感謝します

4

2 に答える 2

0

Excel の有効な MIME タイプは次のとおりです。

BIFF .xls ファイルの場合

application/vnd.ms-excel

ソース: http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx

Excel2007 以降の .xlsx ファイルの場合

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
于 2013-07-02T09:09:05.153 に答える