angular.js - springMVC导出POI的excel,angularjs接收,但是excel打不开xlsx
習慣沉默
習慣沉默 2017-05-15 17:10:34
0
1
776

js导出方法:

    $scope.showYes = function () {

            var needFields = getTheNeedFields();

            $http.post("/aa.do?fields="+needFields, {responseType: 'arraybuffer'}).then(function (response) {

                var data = new Blob([response.data], {type: response.headers('Content-Type')});
                var filename = response.headers('Content-Disposition').split(';')[1].trim().substr('filename='.length);

                FileSaver.saveAs(data, filename);

            });

        };

服务端处理方法:

@RequestMapping("/aa")
    public void testExportDynamic(HttpServletResponse response) throws IOException {

        String fileName = System.currentTimeMillis()+"";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8")+ ".xlsx");
        OutputStream os = response.getOutputStream();


        Map map = new HashMap();
        String fields = request.getParameter("fields");
        map.put("fields", fields);
        //Map对象,带null的对象
        List<HashMap> result = userDao.loadDatasBySelectionFields(map);
        XSSFWorkbook workbook = MehtodApis.exportExcelFile(result, fields);

//        OutputStream os = new FileOutputStream("D:/export2007_" + System.currentTimeMillis() + ".xlsx");
        //
        workbook.write(os);
        os.close();
    }

但是下载的文件是这样的:

控制台:

如果我用这种输出的excel到觉得路径下,文件是没问题的。

    Map map = new HashMap();
        String fields = request.getParameter("fields");
        map.put("fields", fields);
        //Map对象,带null的对象
        List<HashMap> result = userDao.loadDatasBySelectionFields(map);
        XSSFWorkbook workbook = MehtodApis.exportExcelFile(result, fields);

        OutputStream os = new FileOutputStream("D:/export2007_" + System.currentTimeMillis() + ".xlsx");
        workbook.write(os);
        os.close();

神马问题呢?怎么处理好呢?

習慣沉默
習慣沉默

reply all(1)
淡淡烟草味

Take a breakpoint trace and check the memory snapshot to see if there is any problem with your data and whether it is consistent with the version of excel you opened

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template