• JAVA每日小知识(关于excel下载时插入和stream流遍历优化)


    1、windows系统下启动rocketmq操作:
    在bin目录下使用cmd
    分别输入
    start mqnamesrv.cmd
    start mqbroker.cmd -n 127.0.0.1:9876 autoCreateTopicEnable=true

    2、在stream流中需要new对象时,可能会出现new很多对象堆积在堆中,这是需要用try,finally在finally中将new的对象为null,并用gc收集
    如下:

    1. List<ChildModelParamDTO> childModelParamDTOS = childModels.stream().map(childModel -> {
    2.                 ChildModelParamDTO childModelParamDTO=new ChildModelParamDTO();
    3.            try {
    4.                LambdaQueryWrapper<PredictionModelParam> eq1 = new QueryWrapper<PredictionModelParam>().lambda()
    5.                        .eq(PredictionModelParam::getPredictionModelId, childModel.getId());
    6.                PredictionModelParam predictionModelParam = predictionModelParamMapper.selectOne(eq1);
    7.                childModelParamDTO.setCode(childModel.getCode());
    8.                childModelParamDTO.setName(childModel.getName());
    9.                childModelParamDTO.setParameterWeight(predictionModelParam.getParameterWeight());
    10.                return childModelParamDTO;
    11.            }
    12.            finally {
    13.                childModelParamDTO=null;
    14.                System.gc();
    15.            }
    16.         }).collect(Collectors.toList());


    3、在使用easyexcel时,由于监听器无法被spring管理,所以无法在监听器内部使用mp,此时可以用重写的方式将
    excel中数据添加进数据库中,如:
     

    1. EasyExcel.read(file, PredictionParamExcelDTO.class, new ReadListener<PredictionParamExcelDTO>() {
    2.             @Override
    3.             public void invoke(PredictionParamExcelDTO predictionParamExcelDTO, AnalysisContext analysisContext) {
    4.                 PredictionParam predictionParam = null;
    5.                 try {
    6.                     predictionParam = new PredictionParam();
    7.                     predictionParam.setMaterialCode(predictionParamExcelDTO.getMaterialCode());
    8.                     predictionParam.setMaterialName(predictionParamExcelDTO.getMaterialName());
    9.                     predictionParam.setProductionLine(predictionParamExcelDTO.getProductionLine());
    10.                     predictionParam.setCapacity(Integer.valueOf(predictionParamExcelDTO.getCapacity()));
    11.                     predictionParamService.save(predictionParam);
    12.                 } catch (Exception e) {
    13.                     e.printStackTrace();
    14.                     log.error("模板上传失败");
    15.                     throw e;
    16.                 }
    17.             }
    18.             @Override
    19.             public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    20.             }
    21.         }).sheet().doRead();


    4、下载excel的同时,将数据写入excel中:
     

    1. try {
    2.           httpServletResponse.setContentType("application/vnd.ms-excel");
    3.           String fileName = URLEncoder.encode("D:/idea/idea_projects/mwlc-dcm-requirement-backend/predictionParamExcel.xlsx", "UTF-8");
    4.           httpServletResponse.setHeader("content-disposition", "attachment;filename="+fileName);
    5.           ServletOutputStream outputStream;
    6.           outputStream=httpServletResponse.getOutputStream();
    7.           EasyExcel.write(outputStream)
    8.                   .head(PredictionParamExcelDTO.class)
    9.                   .excelType(ExcelTypeEnum.XLSX)
    10.                   .sheet("日基准产能表模板")
    11.                   .doWrite(这里放需要写入excel的数据);
    12.                    
    13.           outputStream.close();
    14.       }catch (IOException o){
    15.           throw new RuntimeException(o);
    16.       }

  • 相关阅读:
    为什么改变进制传输系统码长不变
    功能测试进阶自动化测试如何摸清学习方向,少走弯路呢?
    高校物联网实训室-实验室建设方案
    [iOS]-UIKit
    网络安全笔记-网络设备专场(路由器、交换机、防火墙)
    java基于springboot+vue+nodejs大学生租房网站系统-maven
    TCPIP网络编程第一章踩坑过程 bind() error connect() error
    HBase理论与实践-基操与实践
    怎样将word默认Microsoft Office,而不是WPS
    Nacos安装
  • 原文地址:https://blog.csdn.net/m0_72960906/article/details/132693352