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);
      }
相关推荐
SimonKing17 分钟前
Archery:开源、一站式的数据库 SQL 审核与运维平台
java·后端·程序员
皮皮林55112 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
卡尔特斯16 小时前
Android Kotlin 项目代理配置【详细步骤(可选)】
android·java·kotlin
白鲸开源16 小时前
Ubuntu 22 下 DolphinScheduler 3.x 伪集群部署实录
java·ubuntu·开源
ytadpole16 小时前
Java 25 新特性 更简洁、更高效、更现代
java·后端
纪莫16 小时前
A公司一面:类加载的过程是怎么样的? 双亲委派的优点和缺点? 产生fullGC的情况有哪些? spring的动态代理有哪些?区别是什么? 如何排查CPU使用率过高?
java·java面试⑧股
JavaGuide17 小时前
JDK 25(长期支持版) 发布,新特性解读!
java·后端
用户37215742613517 小时前
Java 轻松批量替换 Word 文档文字内容
java
白鲸开源17 小时前
教你数分钟内创建并运行一个 DolphinScheduler Workflow!
java
Java中文社群18 小时前
有点意思!Java8后最有用新特性排行榜!
java·后端·面试