当前位置:   article > 正文

Springboot以Post方式导出excel文件_post请求 导出文件

post请求 导出文件

场景:

导出excel文件,但是需要传入参数,get方法传参懂的都懂,所以改成post方式

少废话,上代码:

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;
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/536815
推荐阅读
相关标签
  

闽ICP备14008679号