Springboot以Post方式导出excel文件

场景:

导出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;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值