简体   繁体   中英

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

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM