CSV导出
1.依赖jar包
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
2.导出实现
public void export(QmsInspectionTaskReqDTO reqDTO, HttpServletRequest request, HttpServletResponse response) { QmsAssert.notNull(reqDTO,"参数为空"); QmsResponseResult< Map<Long,List<ExportTaskResDTO>>> responseResult = taskRpcService.selectList(reqDTO); long startTime = System.currentTimeMillis(); if (responseResult.getCode() == 999) { throw new QmsException(999,"导出条数超过限制"); } if (responseResult.getCode() != 200) { throw new QmsParamException("查询失败"); } //导出 String destFileName = "入库质检任务"; // CSV写对象 BufferedWriter buffCvsWriter = null; CsvWriter csvWriter=null; ServletOutputStream out=null; response.setContentType("application/vnd.ms-excel;charset=utf-8"); try{ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(destFileName,"UTF8") + "-" + DateUtil.getCurrTimestamp() + ".csv"); out = response.getOutputStream(); buffCvsWriter = new BufferedWriter(new OutputStreamWriter(out, "GBK")); csvWriter = new CsvWriter(buffCvsWriter, ','); // 表头 String[] headers = { "质检任务ID", "仓库", "质检环节", "采购单号", "采购单行号", "采购类型","下单量","到货量","抽检量","物理单位","计价单位","入库单号","入库单单行号","质检时间","供应商","质检商品","采销一级分类","采销二级分类","采销三级分类","生产日期","保质期(天)","质检员","操作(处理动作)","质检任务状态","质检结果","综合货损%","明细","备注"}; csvWriter.writeRecord(headers,true); // 第一层for循环 按照主表来分类 Map<Long,List<ExportTaskResDTO>> resultMap= responseResult.getData(); for(Map.Entry<Long,List<ExportTaskResDTO>> entry : resultMap.entrySet()){ ListIterator iterator= entry.getValue().listIterator(); StringBuilder sb = new StringBuilder(""); ExportTaskResDTO one = null; boolean hasDone = false; int count = 1; while (iterator.hasNext()){ ExportTaskResDTO item = null; if (!hasDone) { one = (ExportTaskResDTO) iterator.next(); item = one; hasDone = true; /** * 共有数据处理 */ // 任务ID one.setIdString(one.getId().toString()); // 采购单号 one.setPurchaseOrderString(one.getPurchaseOrder().toString()); // 采购单行号 one.setPurchaseOrderItemString(one.getPurchaseOrderItem().toString()); // 入库单行号 one.setInstockOrderItemString(one.getInstockOrderItem().toString()); // 质检环节 one.setInspectionLinkName(InspectionLinkEnum.getName(one.getInspectionLink())); // 任务状态 one.setTaskStatusName(TaskStatusEnum.getName(one.getTaskStatus())); // 质检时间 if (one.getInspectionStartTime() != null) { one.setInspectionTime(DateUtil.format(one.getInspectionStartTime(),DateUtil.YYYY_MM_DD)); } // 质检结果 one.setInspectionResultName(ActResultEnum.getName(one.getInspectionResult())); // 操作 one.setDealStatusName(Event.getName(one.getDealStatus())); // 综合货损 one.setMultipleDamageValString(one.getMultipleDamageVal().toString()+"%"); // 采购类型 one.setTypeString(PurchaseTypeEnum.getNameByIndex(one.getType())); // TODO: 2019/10/31 暂缺 入库类型,渠道 }else { item = (ExportTaskResDTO) iterator.next(); } /** * 非共有数据 需要抽出到主表 */ // 生产日期 不一定是第一条数据,所以在下面判断 if (item.getArgType().equals(InspectionArgTypeEnum.DATE.getEnumValue()) && item.getInspectionItemsResult() !=null && !item.getInspectionItemsResult().isEmpty()) { one.setProductionDate(DateUtil.format(new Date(Long.parseLong(item.getInspectionItemsResult())*1000),DateUtil.YYYY_MM_DD)); } if (!item.getInspectionResult().equals(ActResultEnum.UN_INSPECTION.getEnumValue())){ sb.append(item.formatDetail(count)); } count++; } one.setDetails(sb.toString()); String[] content = { "\t"+one.getIdString()+"\t", "\t"+one.getWarehouseName()+"\t", "\t"+one.getInspectionLinkName()+"\t", "\t"+one.getPurchaseOrderString()+"\t", "\t"+one.getPurchaseOrderItemString()+"\t", "\t"+one.getTypeString()+"\t", "\t"+one.getExpectNum()+"\t", "\t"+one.getProductTotalNum()+"\t", "\t"+one.getInspectionProductNum()+"\t", "\t"+one.getPhysicalUnit()+"\t", "\t"+one.getPriceUnit()+"\t", "\t"+one.getInstockCode()+"\t", "\t"+one.getInstockOrderItemString()+"\t", "\t"+one.getInspectionTime()+"\t", "\t"+one.getSupplierName()+"\t", "\t"+one.getSkuName()+"\t", "\t"+one.getPurchaseC1Name()+"\t", "\t"+one.getPurchaseC2Name()+"\t", "\t"+one.getPurchaseC3Name()+"\t", "\t"+one.getProductionDate()+"\t", "\t"+one.getShelfLife()+"\t", "\t"+one.getInspectioner()+"\t", "\t"+one.getDealStatusName()+"\t", "\t"+one.getTaskStatusName()+"\t", "\t"+one.getInspectionResultName()+"\t", "\t"+one.getMultipleDamageValString()+"\t", "\t"+one.getDetails()+"\t", "\t"+one.getMark()+"\t", }; csvWriter.writeRecord(content,true); } if(null != csvWriter) { csvWriter.close(); } }catch (Exception e){ log.error(e.getMessage(), e); try { response.getWriter().write("data exception"); } catch (IOException e1) { e1.printStackTrace(); } return; }finally { if(null != csvWriter) { csvWriter.close(); } try { if (null != buffCvsWriter) { buffCvsWriter.close(); } if (null != out) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } log.info("导出任务-明细处理+流处理花费时间="+(System.currentTimeMillis()-startTime)+"ms"); }