解析Excel文件数据,切分成多张excel并上传minio

后台代码:

1.首页解析excel数据为实体类,

java 复制代码
		List<DataCMMInfoExcel> list = new ArrayList<>();
        try {
            DefaultExcelListener readListener = new DefaultExcelListener(false);
            EasyExcel.read(file.getInputStream(), DataCMMInfoExcel.class, readListener).sheet().headRowNumber(14).doRead();
            list = readListener.getExcelResult().getList();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("读取excel文件出现异常");
        }

2.使用方法计算要切分成的excel数量

java 复制代码
//统计excel个数
        int productNo = getCountNum(list);

3.循坏遍历解析数据

java 复制代码
		//原文件名称
        String originalFileName = file.getOriginalFilename();

        //数据解析-遍历产品
        for (int i = 0; i < productNo; i++) {
            String oldFileName = StringUtils.substring(originalFileName, 0, originalFileName.lastIndexOf("."));
            String suffix = StringUtils.substring(originalFileName, originalFileName.lastIndexOf("."), originalFileName.length());
            //当前产品的文件名
            String fileName = oldFileName + "_" + (i+1) + suffix;
            String fileFullPath =  fileName;
            // 生成excel文件
            OutputStream outputSteam = null;
            try{
                File newFile = new File(fileFullPath);
                if (!newFile .getParentFile().exists()) {
                    newFile .getParentFile().mkdirs();
                }
                if(!newFile .exists()) {
                    newFile .createNewFile();
                }
                outputSteam = new FileOutputStream(newFile);
                //报表
                String[] fields = {"检测项", "标准值", "测量值"};
                SXSSFWorkbook wb = exportExcel(list,fields,i+1);
                wb.write(outputSteam);
                outputSteam.flush();
                outputSteam.close();

                //上传excel到minio
                OssClient ossClient = OssFactory.instance();
                UploadResult uploadResult;
                try {
                    uploadResult = ossClient.uploadSuffix(FileUtil.readBytes(newFile), FileUtil.getSuffix(fileName), FileUtil.getType(newFile));
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new ServiceException("上传minio出错");
                }
                
                //删除文件
                if (newFile.exists() && newFile.isFile()) {
                    if (newFile.delete()) {
                        log.info("删除excel文件名称:{}", fileName);
                    }
                }

            }catch (Exception e){
                throw new ServiceException("生成表格出现异常!");
            }
        }

4.生成excel表格方法

java 复制代码
private SXSSFWorkbook exportExcel(List<DataInfoExcel> excelList, String[] fields, int productNo) {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sheet = wb.createSheet("Data");

        // 生成一个样式
        CellStyle style = wb.createCellStyle();
        //设置边框居中
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置字体
        Font font = wb.createFont();
        font.setFontName("仿宋_GB2312");
        font.setColor(IndexedColors.OLIVE_GREEN.index);
        // 把字体应用到当前的样式
        style.setFont(font);

        //生成第一行
        Row row = sheet.createRow(0);
        for (int k = 0; k < fields.length; k++) {
            Cell cell1 = row.createCell(k);
            cell1.setCellValue(fields[k]);
            cell1.setCellStyle(style);
            sheet.setColumnWidth(k, fields[k].getBytes().length * 2 * 256);
        }

        //数据列表展示
        for (int i = 0; i < excelList.size(); i++) {
            DataInfoExcel infoExcel = excelList.get(i);
            //index表示起始写入位置
            row = sheet.createRow(1 + i);
            //检测项
            row.createCell(0).setCellValue(infoExcel.getName());
            //标准值
            row.createCell(1).setCellValue(infoExcel.getStand());
            //测量值
            if(productNo == 1){
                row.createCell(2).setCellValue(infoExcel.getProduct1());
            }else if(productNo == 2){
                row.createCell(2).setCellValue(infoExcel.getProduct2());
            }else if(productNo == 3){
                row.createCell(2).setCellValue(infoExcel.getProduct3());
            }
        }
        return wb;
    }
相关推荐
huangdong_2 小时前
1688商品图片采集技术解析:登录态处理与SKU图自动分类
开发语言
马士兵教育2 小时前
Java还有前景吗?Java+AI大模型学习路线及项目?
java·人工智能·python·学习·机器学习
chase_my_dream2 小时前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
snow@li2 小时前
Java:理解 Gradle / 后端项目的管家 / 打包SpringBoot 应用 / 完成编译、下载依赖、运行测试、打包 JAR/WAR / 速查表
java
Cloud_Shy6182 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
云烟成雨TD2 小时前
Spring AI 1.x 系列【57】动态工具发现:Tool Search Tool
java·人工智能·spring
zfoo-framework3 小时前
[修改代码使用]codex官方app中使用中转(不需要cc-switch) 1.config.toml 2.sk方式登录
java
天佑木枫3 小时前
15天Python入门系列 · 序
开发语言·python
逍遥德3 小时前
MQTT教程详解-05.SpringBoot集成mqtt client 性能分析
java·spring boot·spring·mt
云烟成雨TD3 小时前
Spring AI 1.x 系列【54】Retry 机制分析
java·人工智能·spring