使用hutool工具导出excel,使用jodd工具压缩流,使用oss上传流
最近写了一个业务,用到一些比较实用的工具记录下。
List<RobotCard> robotCardList = null; if (BizRouteEnum.ROUTE_1.getRouteCode().equals(param.getExportModel())){ //1按条件导出,否则按给定数据导出 RobotCard robotCardParam = BeanUtil.toBean(map, RobotCard.class); robotCardList = this.searchList(robotCardParam); } else { List<Long> ids = param.getIds(); RobotAssert.notNull(ids, "请选择需要导出的数据。"); robotCardList = this.selectBatchIds(ids); } RobotAssert.notNull(robotCardList, "无有效数据。"); ArrayList<Map<String, Object>> rows = new ArrayList<>(); robotCardList.forEach(rc -> { Map<String, Object> col = new LinkedHashMap<>(); col.put("编号", rc.getId().toString()); col.put("金额", rc.getCardAmount()); col.put("密码", rc.getPayPasswd()); rows.add(col); }); ExcelWriter excelWriter = ExcelUtil.getWriter(); excelWriter.setColumnWidth(0, 30); excelWriter.write(rows); ByteArrayOutputStream baos = new ByteArrayOutputStream(); excelWriter.flush(baos); excelWriter.close(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(bos, Charset.defaultCharset()); //添加xls try { jodd.io.ZipUtil.addToZip(zos, baos.toByteArray(), "制卡数据.xls", "制卡数据"); } catch (Exception e1) { LOGGER.error("文件压缩失败。", e1); } robotCardList.forEach(card -> { //生产二维码 QrConfig config = new QrConfig(300, 300); config.setCharset(Charset.forName("utf-8")); String cardId = card.getId().toString(); byte[] data = QrCodeUtil.generatePng(cardId, config); //压缩 try { jodd.io.ZipUtil.addToZip(zos, data, cardId.concat(CommonConstant.PNG), cardId); } catch (Exception e) { LOGGER.error("文件压缩失败。", e); } }); try { zos.finish(); zos.close(); } catch (Exception e) { LOGGER.error("压缩流关闭失败。", e); } //上传 ByteArrayInputStream in = new ByteArrayInputStream(bos.toByteArray()); String url = oSSClientUtil.putObjectForInputStream( OssConstant.PAYMENT_TEMP.concat(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN)).concat(CommonConstant.PNG), in);