| | |
| | | response.setHeader("Content-Disposition", "attachment;filename=送货单.zip"); |
| | | response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
| | | |
| | | // 直接内存打包,抛弃临时文件 |
| | | try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { |
| | | |
| | | for (SalesLedger ledger : salesLedgerList) { |
| | |
| | | |
| | | // 自动序号 |
| | | for (int i = 0; i < productList.size(); i++) { |
| | | productList.get(i).setSerialNumber(i + 1); |
| | | SalesLedgerProduct product = productList.get(i); |
| | | if (product != null) { |
| | | product.setSerialNumber(i + 1); |
| | | product.setOrderNo(ledger.getSalesContractNo()); |
| | | // 安全获取物料编码 |
| | | Long productModelId = product.getProductModelId(); |
| | | if (productModelId != null) { |
| | | ProductModel productModel = productModelMapper.selectById(productModelId); |
| | | if (productModel != null) { |
| | | product.setMaterialCode(productModel.getMaterialCode()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | // 关联查询客户信息 |
| | | Customer customer = customerMapper.selectById(ledger.getCustomerId()); |
| | | |
| | | // 组装数据 |
| | | Map<String, Object> dataMap = new HashMap<>(); |
| | | dataMap.put("customerName", ledger.getCustomerName()); |
| | |
| | | dataMap.put("companyPhone", customer.getCompanyPhone()); |
| | | dataMap.put("salesContractNo", ledger.getSalesContractNo()); |
| | | dataMap.put("deliveryDate", formatDate(LocalDate.now())); |
| | | // 放入商品集合 |
| | | dataMap.put("products", productList); |
| | | BigDecimal totalQuantity = new BigDecimal(0); |
| | | for (SalesLedgerProduct product : productList) { |
| | | totalQuantity = totalQuantity.add(product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity()); |
| | | } |
| | | dataMap.put("totalQuantity", totalQuantity); |
| | | |
| | | // 每次循环全新读取模板,彻底解决流问题 |
| | | // 生成Excel |
| | | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| | | try (InputStream templateIs = this.getClass().getResourceAsStream("/static/shipping-note.xlsx")) { |
| | | if (templateIs == null) throw new BaseException("模板文件不存在"); |
| | | InputStream templateIs = this.getClass().getResourceAsStream("/static/shipping-note.xlsx"); |
| | | |
| | | // EasyExcel填充 |
| | | ExcelWriter writer = EasyExcel.write(bos).withTemplate(templateIs).build(); |
| | | WriteSheet sheet = EasyExcel.writerSheet().build(); |
| | | writer.fill(dataMap, sheet); |
| | | writer.finish(); |
| | | ExcelWriter writer = EasyExcel.write(bos).withTemplate(templateIs).build(); |
| | | WriteSheet sheet = EasyExcel.writerSheet().build(); |
| | | |
| | | writer.fill(dataMap, sheet); |
| | | |
| | | if (!productList.isEmpty()) { |
| | | writer.fill(productList, sheet); |
| | | } |
| | | |
| | | // 打包进ZIP |
| | | String excelName = String.format("送货单_%s.xlsx", |
| | | Optional.ofNullable(ledger.getSalesContractNo()).orElse(String.valueOf(ledger.getId()))); |
| | | writer.finish(); |
| | | templateIs.close(); |
| | | |
| | | // 写入ZIP |
| | | String excelName = String.format("送货单_%s.xlsx", Optional.ofNullable(ledger.getSalesContractNo()).orElse(String.valueOf(ledger.getId()))); |
| | | ZipEntry entry = new ZipEntry(excelName); |
| | | zos.putNextEntry(entry); |
| | | zos.write(bos.toByteArray()); |