场景:
导出excel文件,但是需要传入参数,get方法传参懂的都懂,所以改成post方式文章来源:https://www.toymoban.com/news/detail-466791.html
少废话,上代码:文章来源地址https://www.toymoban.com/news/detail-466791.html
Controller:
@PostMapping(value = "/exportCustomMItemDataWithLine.iom") @ResponseBody @ApiOperation(value = "数据查询导出(自定义)", notes = "", produces = "application/json") public Mono<Void> exportCustomMItemDataWithLine(ServerHttpResponse response , @RequestBody JSONObject jsonObject) { DataBuffer dataBuffer = response.bufferFactory().allocateBuffer(); OutputStream outputStream = dataBuffer.asOutputStream(); MediaType application = new MediaType("application", "msword", Charset.forName("UTF-8")); File file = null; try { file = iomPrecisionManagementService.exportCustomMItemData(jsonObject); response.getHeaders().setContentType(application); response.getHeaders().set("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "utf-8")); InputStream bis = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[bis.available() + 1000]; int i = 0; while ((i = bis.read(b)) != -1) { outputStream.write(b, 0, i); } outputStream.flush(); outputStream.close(); if (file != null) { FileUtil.deleteFileLater(file); } } catch (Exception e) { e.printStackTrace(); } return response.writeWith(Mono.just(dataBuffer)); }
Service:
@Override public File exportCustomMItemData(JSONObject jsonObject) throws IOException { String fileName = "数据查询导出模板"; String fileType = ".xls"; File file = new File(fileName + "_" + System.currentTimeMillis() + fileType); List<String> nameList = new ArrayList<>(); List<String> codeList = new ArrayList<>(); if(!ToolsUtil.isEmpty(jsonObject.get("nameCode")) && !ToolsUtil.isEmpty(ToolsUtil.jsonObjectToEntityList(jsonObject.get("nameCode") , NameCode.class))){ List<NameCode> mapList = ToolsUtil.jsonObjectToEntityList(jsonObject.get("nameCode") , NameCode.class); nameList = mapList.stream().map(NameCode::getName).collect(Collectors.toList()); codeList = mapList.stream().map(NameCode::getCode).collect(Collectors.toList()); }else { nameList = Arrays.asList( "事业部名称" , "车间" , "产线" , "区域" , "标准名称", "标准值", "测量数值", "测量时间", "测量人", "测量方式", "总分", "周期开始时间", "周期结束时间", "备注", "超期标识", "完成标识" ); codeList = Arrays.asList( "businessName" , "dutyDepartmentName" , "productionLineName" , "productionLineAreaName" , "standardName" , "standardValue" , "measureValue" , "measureTime" , "measurer" , "measureType" , "totalScore" , "cycleStartTime" , "cycleEndTime" , "remark" , "isBeyond" , "isFinish" ); } Workbook wb = new HSSFWorkbook(); int rowSize = 1; Sheet sheet = wb.createSheet(); Row row = sheet.createRow(rowSize); for (int i = 0; i < nameList.size(); i++) { row.createCell(i + 1).setCellValue(nameList.get(i)); } //从第二行,第二列开始追加列 int start = 2; List<MItemDataExcelPojo> poJoList = matchMItemPojo(queryMItemList(new JSONObject())); //抽取 List<Integer> selectIDList = new ArrayList<>(); if(!ToolsUtil.isEmpty(jsonObject.get("selectIDs"))){ selectIDList = ToolsUtil.jsonObjectToEntityList(jsonObject.get("selectIDs") , Integer.class); List<MItemDataExcelPojo> tmpList = new ArrayList<>(selectIDList.size()); for(Integer selectID : selectIDList){ tmpList.add(poJoList.get(selectID)); } poJoList = tmpList; } try{ for (MItemDataExcelPojo item : poJoList) { row = sheet.getRow(start); if (row == null) { row = sheet.createRow(start); } for(int i = 0 ; i < codeList.size() ; i++){ String methodName = "get" + ToolsUtil.toUpperCaseFirstOne(codeList.get(i)); Method method = item.getClass().getMethod(methodName); row.createCell(i+1).setCellValue(ObjectUtils.filterEmptyData(method.invoke(item))); } start++; } }catch (Exception e){ } FileOutputStream out = null; try { out = new FileOutputStream(file); out.flush(); wb.write(out); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return file; }
到了这里,关于Springboot以Post方式导出excel文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!