最近寫了一個業務,用到一些比較實用的工具記錄下。
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);