javaarraysexcelspringjxl

how to convert byte array to excel file in spring boot


I want to create an excel file from byte[]. and I'm using jxl library in spring boot. I need a file with XLS format, convert to Base64 and return that.

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(20000000);
WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

...

String resultBase64 = reportService.fetchReportExcel(...);
byte[] excel = base64.decode( resultBase64 );

I have many byte arrays and I need to write btye arrays in many sheets of an excel file too, if it's possible. thanks


Solution

  • I write it

    my code:

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(20000000);
    
    String resultBase64 = reportApplicationService.fetchReportExcel(...);
    byte[] excel = base64.decode( resultBase64 );
    
    outputStream.write(excel);
    outputStream.flush();
    
    response.setHeader("Content-Disposition", "attachment; filename=name.xls");
    response.setContentType("application/vnd.ms-excel");
    response.setContentLength((int) outputStream.size());
    StreamUtils.copy(new ByteArrayInputStream(outputStream.toByteArray()), response.getOutputStream());