保存为poi apache创建的excel文件

时间:2018-04-10 14:05:32

标签: java excel rest apache-poi filesaver.js

我用poi apache创建了一个excel文件。 所以我在ExportController.java中使用了:

    @RestController
    @RequestMapping("/export")
    @Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    @Consumes({ "application/json" })

    public class ExportController {

    private static final Logger LOGGER = 
    LoggerFactory.getLogger(ExportController.class);

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @PreAuthorize(value = "hasRole('AK_M_ADMIN')")
    @ResponseBody
    public Response save(@RequestBody ResultatToExportDto resultToExport, HttpServletResponse response) throws ErosCommonsException {

    final String FILE_NAME = "PlanningExcel.xlsx";
    final XSSFWorkbook workbook = new XSSFWorkbook();
    /*
     *
     * Other code poi apache
     *
     */
    StreamingOutput stream = new StreamingOutput() {
            @Override
            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
                String home = System.getProperty("user.home");
                File excelFile = new File(home + "/Downloads/" + FILE_NAME);
                outputStream = new FileOutputStream(excelFile);
                Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream));
                writer.flush();
                writer.close();
            }
        };

        LOGGER.info("Export Excel Planning Done !!!");

        return Response.ok(stream).build();

    } catch (DefaultErosException e) {
        throwDefaultErosException("Error", e.getErrorResource().getDetailMessage(), HttpStatus.INTERNAL_SERVER_ERROR, e);
    }
    return Response.status(Response.Status.BAD_REQUEST).build();

之后我在javascript文件“planning.js”中使用了这个休息调用,以便保存在我的excel文件“PlanningExcel.xlsx”的文件夹中(或下载),使用库“Filesaver.js”的方法saveAs

      $http({
            method: 'POST',
            url: '/dockaWeb/api/rest/resources/export',
            data: resultat
          }).then(function successCallback(resp) {
            console.log("Call ExportController passed");
            var blob = new Blob([resp], {
                type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            });
            saveAs(blob, 'text.xlsx');
            return data;
          }, function errorCallback(response) {
            console.log("Call ExportController failed: " + response);
          });

但是当我尝试导出我的文件“PlanningExcel.xlsx”时,它会在浏览器中返回此错误: http://localhost:8080/dockaWeb/api/rest/resources/export 500(Erreur Interne de Servlet)

{“label”:“内部服务器错误”,“detailMessage”:“无法写入内容:找不到类com.afklm.docka.web.rest.controllers.ExportController $ 2的序列化程序,并且没有发现任何属性来创建BeanSerializer (为了避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过引用链:org.glassfish.jersey.message.internal.OutboundJaxrsResponse [\“context \”] - > org.glassfish.jersey.message.internal.OutboundMessageContext [\ “entity \”]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:找不到类com.afklm.eros.web.rest.controllers.ExportController $ 2的序列化器,并且没有发现创建BeanSerializer的属性(以避免异常) ,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过参考链:org.glassfish.jersey.message.internal.OutboundJaxrsResponse [\“context \”] - > org.glassfish.jersey.message.internal.OutboundMessageContext [\“entity \” ])”, “的StatusCode”:500}

我无法解决这个问题。 请帮忙!

0 个答案:

没有答案