您的位置:首页 > 娱乐 > 八卦 > 解析Excel文件数据,切分成多张excel并上传minio

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

2024/10/6 14:35:24 来源:https://blog.csdn.net/csdnxyy/article/details/141033620  浏览:    关键词:解析Excel文件数据,切分成多张excel并上传minio

后台代码:

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

		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数量

//统计excel个数int productNo = getCountNum(list);

3.循坏遍历解析数据

		//原文件名称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到minioOssClient 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表格方法

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;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com