在开发业务中,正常使用EasyExcel的write导出文件流给客户端,客户端就可自行下载。
但在偶尔业务中,可能也需要把write后的文件存储到服务器或者oss或者哪里,不直接把文件流给吐回去。所以我这里给大家提供我本人遇到的情况详情给大家讲解~(研究了好几天!!! 耐心看)
这是我导出的Xlsx文件内容:
步骤1、Controller层
步骤2、这就可以略过~
步骤3、重点来了
步骤4、底下都是步骤3中的的公共方法
/****************************************底下是代码*******************************************/
@Override public CommonResponse downloadTickets(HttpServletResponse response, Integer invoiceId) { try { //首先创建任意一个OutPutStream流 ByteArrayOutputStream bos = new ByteArrayOutputStream(); //组装header OutputStream outputStream = responseSetHeader(response); //获取票单模板 MultipartFile mockMultipartFile = getTemplate(); //创建Sheet页、填充格式设置 ExcelWriter excelWriter = EasyExcel.write(outputStream).file(bos).withTemplate(mockMultipartFile.getInputStream()).build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); InvoiceEntity invoiceEntity = invoiceService.getById(invoiceId); if (null == invoiceEntity) { throw InvoiceException.DATA_NULL_EXCEPTION.newInstance(); } //从oss获取订单文件转为对象 拼接订单数据用到 byte[] fileByte = ossUtil.getFile(invoiceEntity.getOrderDetailOssUrl()); if (!ArrayUtils.isNotEmpty(fileByte)) { throw InvoiceException.OSS_EXCEPTION.newInstance("订单文件未找到"); } //读取订单数据转List List<InvoiceOrderDetailData> invoiceOrderDetailDataList = getOssFileList(fileByte); //组装公司信息 Map<String, Object> map = getStringObjectMap(invoiceEntity); //开始读取 excelWriter.fill(invoiceOrderDetailDataList, fillConfig, writeSheet); excelWriter.fill(map, writeSheet); //拼接票单落款信息 List<List<String>> listList = getLists(invoiceEntity.getAmount().toString()); excelWriter.write(listList, writeSheet); excelWriter.finish(); return CommonResponse.success(getInvoiceTickets(bos)); } catch (IOException e) { log.error("下载票单异常:{}", e); throw InvoiceException.OSS_EXCEPTION.newInstance("下载票单异常"); } }
------------------------------------------------------------------------------------------------------------------------------
private OutputStream responseSetHeader(HttpServletResponse response) throws IOException { response.setCharacterEncoding("UTF-8"); response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel"); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=invoice.xls"); // xls // response.setContentType("application/vnd.ms-excel"); // xlsx // response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); return response.getOutputStream(); }
------------------------------------------------------------------------------------------------------------------------------
private MultipartFile getTemplate() throws IOException { String filePath = OssUtil.PATH_TEMPLATE_FILE + "INVOICE_TEMPLATE.xlsx"; byte[] fileByte = ossUtil.getFile(filePath); InputStream inputStream = new ByteArrayInputStream(fileByte); MultipartFile mockMultipartFile = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); return mockMultipartFile; }
------------------------------------------------------------------------------------------------------------------------------
private List<InvoiceOrderDetailData> getOssFileList(byte[] fileByte) throws IOException { InputStream inputStream = new ByteArrayInputStream(fileByte); MultipartFile mockMultipartFile = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); BufferedInputStream bufferedInputStream = new BufferedInputStream(mockMultipartFile.getInputStream()); ExcelListener sheetOneListener = new ExcelListener(); ExcelReader excelReader = EasyExcel.read(bufferedInputStream).build(); ReadSheet build0 = EasyExcel.readSheet(0).head(InvoiceOrderDetailData.class).headRowNumber(1).registerReadListener(sheetOneListener).build(); excelReader.read(build0); return sheetOneListener.getRows(); }
------------------------------------------------------------------------------------------------------------------------------
private String getInvoiceTickets(ByteArrayOutputStream bos) { //单独把票单存储到oss返回客户端路径 String invoiceTicketsUrl = ""; byte[] bytes = bos.toByteArray(); InputStream sbs = new ByteArrayInputStream(bytes); String path = OssUtil.PATH_TEMPLATE_FILE + "INVOICE_TICKETS.xlsx"; try { ossUtil.putObject(path, sbs); invoiceTicketsUrl = ossUtil.generatePresignedUrl(path); } catch (ApiException e) { log.error("票单存储oss错误:{}", e); throw InvoiceException.OSS_EXCEPTION.newInstance("票单存储oss错误"); } return invoiceTicketsUrl; }