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收集

如下:

List<ChildModelParamDTO> childModelParamDTOS = childModels.stream().map(childModel -> {
                ChildModelParamDTO childModelParamDTO=new ChildModelParamDTO();
           try {
               LambdaQueryWrapper<PredictionModelParam> eq1 = new QueryWrapper<PredictionModelParam>().lambda()
                       .eq(PredictionModelParam::getPredictionModelId, childModel.getId());
               PredictionModelParam predictionModelParam = predictionModelParamMapper.selectOne(eq1);
               childModelParamDTO.setCode(childModel.getCode());
               childModelParamDTO.setName(childModel.getName());
               childModelParamDTO.setParameterWeight(predictionModelParam.getParameterWeight());
               return childModelParamDTO;
           }
           finally {
               childModelParamDTO=null;
               System.gc();
           }
        }).collect(Collectors.toList());

**3、**在使用easyexcel时,由于监听器无法被spring管理,所以无法在监听器内部使用mp,此时可以用重写的方式将

excel中数据添加进数据库中,如:

 EasyExcel.read(file, PredictionParamExcelDTO.class, new ReadListener<PredictionParamExcelDTO>() {
            @Override
            public void invoke(PredictionParamExcelDTO predictionParamExcelDTO, AnalysisContext analysisContext) {
                PredictionParam predictionParam = null;
                try {
                    predictionParam = new PredictionParam();
                    predictionParam.setMaterialCode(predictionParamExcelDTO.getMaterialCode());
                    predictionParam.setMaterialName(predictionParamExcelDTO.getMaterialName());
                    predictionParam.setProductionLine(predictionParamExcelDTO.getProductionLine());
                    predictionParam.setCapacity(Integer.valueOf(predictionParamExcelDTO.getCapacity()));
                    predictionParamService.save(predictionParam);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("模板上传失败");
                    throw e;
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }

        }).sheet().doRead();

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

try {
          httpServletResponse.setContentType("application/vnd.ms-excel");
          String fileName = URLEncoder.encode("D:/idea/idea_projects/mwlc-dcm-requirement-backend/predictionParamExcel.xlsx", "UTF-8");
          httpServletResponse.setHeader("content-disposition", "attachment;filename="+fileName);

          ServletOutputStream outputStream;
          outputStream=httpServletResponse.getOutputStream();
          EasyExcel.write(outputStream)
                  .head(PredictionParamExcelDTO.class)
                  .excelType(ExcelTypeEnum.XLSX)
                  .sheet("日基准产能表模板")
                  .doWrite(这里放需要写入excel的数据);
                   
          outputStream.close();


      }catch (IOException o){
          throw new RuntimeException(o);
      }
相关推荐
奔跑吧邓邓子12 分钟前
【Python爬虫(44)】分布式爬虫:筑牢安全防线,守护数据之旅
开发语言·分布式·爬虫·python·安全
非 白21 分钟前
【Java】单例模式
java·笔记·单例模式
C#Thread28 分钟前
C#上位机--流程控制(IF语句)
开发语言·javascript·ecmascript
IDRSolutions_CN35 分钟前
如何在 PDF 文件中嵌入自定义数据
java·经验分享·pdf·软件工程·团队开发
_风中无我。41 分钟前
Spring的过滤器获取请求体中JSON参数,同时解决Controller获取不到请求体参数的问题。
java·spring·json
bing_1581 小时前
Spring Boot 中为什么 需要限流、降级和熔断?
java
ccm031 小时前
高效开发助手:深入了解Hutool工具库
java·g工具库
牵牛老人1 小时前
Qt开发中出现中文乱码问题深度解析与解决方案
开发语言·qt
大脑经常闹风暴@小猿1 小时前
1.1 go环境搭建及基本使用
开发语言·后端·golang
雪落南城1 小时前
【Maven】maven加载不到包
java·maven