前端|js vue base64 byte 转 为文件格式 (以excel为例)

1.后端组装数据

byte[] bytesExcel; String base64Excel; try { Workbook workbook = FileUtil.getWorkbookByTemplateAndData("design-template.xlsx", mapExcel); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); workbook.write(byteArrayOutputStream); bytesExcel = byteArrayOutputStream.toByteArray(); base64Excel = DatatypeConverter.printBase64Binary(bytesExcel); byteArrayOutputStream.close(); workbook.close(); } catch (Exception e) { e.printStackTrace(); msg = String.format("初始化模板错误:", String.valueOf(e.getMessage())); jsonObject.put("errorMsg", msg); this.SendMessage(jsonObject); return; } this.SendData(base64Excel, Message.MessageType.MAINTENANCE_DESIGN.name);

2.前端接收数据
//base64byte[]转为 excel文件格式 展示 luckyexcel 使用 export function changeToLuckyExcelFile(data, filename){ let bstr = window.atob(data); let n = bstr.length; let u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], filename, {type:'application/vnd.ms-excel'}); }

3.文件前端下载导出
// 导出到本地 downloadExcel() { // console.log(luckysheet.getAllSheets()) // exportExcel(luckysheet.getAllSheets(), "养护设计样图"); // console.log("excelDataCurrent",this.excelDataCurrent) FileSaver.saveAs(this.excelDataCurrent, '养护设计样图.xlsx') },

【前端|js vue base64 byte 转 为文件格式 (以excel为例)】

    推荐阅读