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);
      }
相关推荐
用户37215742613519 分钟前
Java 裁切 PDF 页面:去除多余白边、批量裁剪与局部图片导出
java
野生技术架构师20 分钟前
高级 Java 工程师面试八股(2026 精选版)
java·开发语言·面试
小小-猿20 分钟前
用 Python 写昇腾算子:pyasc 是怎么回事
开发语言·python·昇腾·cann·pyasc
星夜离尘5623 分钟前
MATLAB:标定表局部倒退,为什么不能排序?
android·开发语言·matlab
智购科技智能售货柜27 分钟前
自动售货机步进电机细分驱动实战:力矩、噪声与堵转~YH
大数据·开发语言·人工智能·科技·嵌入式硬件
wuminyu35 分钟前
OpenJDK学习笔记:Native线程如何回调Java
java·linux·c语言·jvm·c++
骑着蜗牛撵大象3271 小时前
AgentScope 搭大脑、Spring AI 当手脚:企业智能体推理与执行分层落地
java·人工智能·spring·智能体·工具调用·agentscope·模型编排
程序员小杰@1 小时前
SpringBoot + SpringCloudAlibaba 日志 & 链路追踪 完整总结
java·spring boot·后端
杜 硕1 小时前
从零开始的C++生活——第十五讲
开发语言·数据结构·c++·生活
sunly_1 小时前
Go 切片详解
开发语言·oracle·golang