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);
      }
相关推荐
太阳的后裔28 分钟前
随笔一些用C#封装的控件
开发语言·c#
tianyuanwo29 分钟前
Rust语言组件RPM包编译原理与Cargo工具详解
开发语言·网络·rust·rpm
float_六七3 小时前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
能摆一天是一天5 小时前
JAVA stream().flatMap()
java·windows
CodeCraft Studio5 小时前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
零点零一5 小时前
VS+QT的编程开发工作:关于QT VS tools的使用 qt的官方帮助
开发语言·qt
颜如玉5 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
程序员的世界你不懂7 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年7 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
lingchen19067 小时前
MATLAB的数值计算(三)曲线拟合与插值
开发语言·matlab