controller 调用代码:

//注意:保费字段 premium :指每1000保额对应的费率
public BaseResult<String> importExcel(@RequestParam("file") MultipartFile file,String riskId,int coreUserId,String type) {BaseResult<String> baseResult = new BaseResult<String>();try {BaseResult<List<ExportPtRate>> baseResult1 = ExcelUtil.readExcel(file);             if(list==null || list.size()==0){BaseResult<String> result = new BaseResult<>();result.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());result.setReturnMsg("导入的数据不能为空");return result;}logger.info("从Excel导入数据一共 {} 行 ", list.size());Map<String,Object> map = new HashMap<>();map.put("riskId",riskId);map.put("coreUserId",coreUserId);map.put("list",list);baseResult = riskService.exportFeilvExcel(map);}catch (Exception e1) {logger.error("导入失败:{}", e1.getMessage());baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg(e1.getMessage());}return  baseResult;
}
ExcelUtil 工具类:
package com.ssish.saas.eoms.util;import com.ssish.saas.constant.common.BaseResult;
import com.ssish.saas.constant.currency.ReturnMsg;
import com.ssish.saas.utils.model.ExportPtRate;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;public class ExcelUtil {private final static Logger log = LoggerFactory.getLogger(ExcelUtil.class);private final static String EXCEL2003 = "xls";private final static String EXCEL2007 = "xlsx";public static <T> List<T> readExcel(Class<T> cls, MultipartFile file){String fileName = file.getOriginalFilename();if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {log.error("上传文件格式不正确");}List<T> dataList = new ArrayList<>();Workbook workbook = null;try {InputStream is = file.getInputStream();if (fileName.endsWith(EXCEL2007)) {workbook = new XSSFWorkbook(is);}if (fileName.endsWith(EXCEL2003)) {workbook = new HSSFWorkbook(is);}if (workbook != null) {//类映射  注解 value-->bean columnsMap<String, Integer> classMap = new HashMap<>();List<Field> fields = Stream.of(cls.getDeclaredFields()).collect(Collectors.toList());fields.forEach(field -> {ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);if (annotation != null) {String value = annotation.value();if (StringUtils.isBlank(value)) {return;//return起到的作用和continue是相同的 语法}if (!classMap.containsKey(value)) {classMap.put(value, annotation.col());}}});Method[] methods = cls.getMethods();//默认读取第一个sheetSheet sheet = workbook.getSheetAt(0);for (int i = sheet.getFirstRowNum()+1; i <= sheet.getLastRowNum(); i++) {Row row = sheet.getRow(i);//忽略空白行if (row == null) {continue;}T t = cls.newInstance();Set<Map.Entry<String, Integer>> set = classMap.entrySet();for (Method me : methods) {for (Map.Entry<String, Integer> entry : set) {if (me.getName().startsWith("set") && me.getName().endsWith(upperCase(entry.getKey()))) {Cell cell = row.getCell(entry.getValue());String cellValue = getCellValue(cell);me.invoke(t, cellValue);}}}dataList.add(t);}}} catch (Exception e) {log.error(String.format("parse excel exception!"), e);} finally {if (workbook != null) {try {workbook.close();} catch (Exception e) {log.error(String.format("parse excel exception!"), e);}}}return dataList;}/*** 将首字母转为大写* @param str* @return*/private static String upperCase(String str) {char[] ch = str.toCharArray();if (ch[0] >= 'a' && ch[0] <= 'z') {ch[0] = (char) (ch[0] - 32);}return new String(ch);}private static <T> void handleField(T t, String value, Field field) throws Exception {Class<?> type = field.getType();if (type == null || type == void.class || StringUtils.isBlank(value)) {return;}if (type == Object.class) {field.set(t, value);//数字类型} else if (type.getSuperclass() == null || type.getSuperclass() == Number.class) {if (type == int.class || type == Integer.class) {field.set(t, NumberUtils.toInt(value));} else if (type == long.class || type == Long.class) {field.set(t, NumberUtils.toLong(value));} else if (type == byte.class || type == Byte.class) {field.set(t, NumberUtils.toByte(value));} else if (type == short.class || type == Short.class) {field.set(t, NumberUtils.toShort(value));} else if (type == double.class || type == Double.class) {field.set(t, NumberUtils.toDouble(value));} else if (type == float.class || type == Float.class) {field.set(t, NumberUtils.toFloat(value));} else if (type == char.class || type == Character.class) {field.set(t, CharUtils.toChar(value));} else if (type == boolean.class) {field.set(t, BooleanUtils.toBoolean(value));} else if (type == BigDecimal.class) {field.set(t, new BigDecimal(value));}} else if (type == Boolean.class) {field.set(t, BooleanUtils.toBoolean(value));} else if (type == Date.class) {//field.set(t, value);} else if (type == String.class) {field.set(t, value);} else {Constructor<?> constructor = type.getConstructor(String.class);field.set(t, constructor.newInstance(value));}}private static String getCellValue(Cell cell) {if (cell == null) {return "";}if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {if (DateUtil.isCellDateFormatted(cell)) {return HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();} else {return new BigDecimal(cell.getNumericCellValue()).toString();}} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {return StringUtils.trimToEmpty(cell.getStringCellValue());} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {return StringUtils.trimToEmpty(cell.getCellFormula());} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {return "";} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {return String.valueOf(cell.getBooleanCellValue());} else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {return "ERROR";} else {return cell.toString().trim();}}public static <T> void writeExcel(HttpServletResponse response, List<T> dataList, Class<T> cls){Field[] fields = cls.getDeclaredFields();List<Field> fieldList = Arrays.stream(fields).filter(field -> {ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);if (annotation != null && annotation.col() > 0) {field.setAccessible(true);return true;}return false;}).sorted(Comparator.comparing(field -> {int col = 0;ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);if (annotation != null) {col = annotation.col();}return col;})).collect(Collectors.toList());Workbook wb = new XSSFWorkbook();Sheet sheet = wb.createSheet("Sheet1");AtomicInteger ai = new AtomicInteger();{Row row = sheet.createRow(ai.getAndIncrement());AtomicInteger aj = new AtomicInteger();//写入头部fieldList.forEach(field -> {ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);String columnName = "";if (annotation != null) {columnName = annotation.value();}Cell cell = row.createCell(aj.getAndIncrement());CellStyle cellStyle = wb.createCellStyle();cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());Font font = wb.createFont();cellStyle.setFont(font);cell.setCellStyle(cellStyle);cell.setCellValue(columnName);});}if (CollectionUtils.isNotEmpty(dataList)) {dataList.forEach(t -> {Row row1 = sheet.createRow(ai.getAndIncrement());AtomicInteger aj = new AtomicInteger();fieldList.forEach(field -> {Class<?> type = field.getType();Object value = "";try {value = field.get(t);} catch (Exception e) {e.printStackTrace();}Cell cell = row1.createCell(aj.getAndIncrement());if (value != null) {if (type == Date.class) {cell.setCellValue(value.toString());} else {cell.setCellValue(value.toString());}cell.setCellValue(value.toString());}});});}//冻结窗格wb.getSheet("Sheet1").createFreezePane(0, 1, 0, 1);//浏览器下载excelbuildExcelDocument("abbot.xlsx",wb,response);//生成excel文件
//        buildExcelFile(".\\default.xlsx",wb);}/*** 浏览器下载excel* @param fileName* @param wb* @param response*/private static  void  buildExcelDocument(String fileName, Workbook wb, HttpServletResponse response){try {response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8"));response.flushBuffer();wb.write(response.getOutputStream());} catch (IOException e) {e.printStackTrace();}}/*** 生成excel文件* @param path 生成excel路径* @param wb*/private static  void  buildExcelFile(String path, Workbook wb){File file = new File(path);if (file.exists()) {file.delete();}try {wb.write(new FileOutputStream(file));} catch (Exception e) {e.printStackTrace();}}/***  判断整行是否为空*  @param row  excel得行对象*  @param  maxRow 有效值得最大列数*/private static boolean CheckRowNull(Row row, int maxRow) {int num = 0;for (int j = 0; j < maxRow; j++) {Cell cell=row.getCell(j);if (cell==null||cell.equals("")||cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {num++;}}if(maxRow==num) return true;return false;}public static BaseResult<List<ExportPtRate>> readExcel(MultipartFile file){BaseResult<List<ExportPtRate>> baseResult = new BaseResult<>();baseResult.setReturnCode(ReturnMsg.SUCCESS.getCode());String fileName = file.getOriginalFilename();if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {log.error("上传文件格式不正确");baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg("上传文件格式不正确");return baseResult;}List<ExportPtRate> dataList = new ArrayList<>();Workbook workbook = null;try {InputStream is = file.getInputStream();if (fileName.endsWith(EXCEL2003)) {workbook = new HSSFWorkbook(is);}if (fileName.endsWith(EXCEL2007)) {workbook = new XSSFWorkbook(is);}if (workbook != null) {//默认读取第一个sheetSheet sheet = workbook.getSheetAt(0);int rowNum = sheet.getLastRowNum() ;//有多少行Row rowAmount = sheet.getRow(0);//第一行 保额Row rowInsure = sheet.getRow(1);//第二行 保障期限Row rowPayear = sheet.getRow(2);//第三行 缴费期限Row rowGender = sheet.getRow(3);//第四行 性别Map<Integer,String> insureMap = new HashMap<>();Map<Integer,String> payearMap = new HashMap<>();Map<Integer,String> genderMap = new HashMap<>();String amout = "";if (rowAmount.getCell(1)==null||rowAmount.getCell(1).equals("")||rowAmount.getCell(1).getCellType() == HSSFCell.CELL_TYPE_BLANK) {log.error("保额数据为空");baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg("保额数据为空");return baseResult;}else{amout = getCellValue(rowAmount.getCell(1)); //保额}//过滤空行for(int ij = 1; ij <rowInsure.getLastCellNum(); ij++){Cell cell=rowInsure.getCell(ij);if (cell==null||cell.equals("")||cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {int n=ij+1;log.error("保障期限第"+n+"列数据为空");baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg("保障期限第"+n+"列数据为空");return baseResult;}else{String insure = getCellValue(rowInsure.getCell(ij));insureMap.put(ij,insure);}}for(int pj = 1; pj <rowPayear.getLastCellNum(); pj++){Cell cell=rowPayear.getCell(pj);if (cell==null||cell.equals("")||cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {int n=pj+1;log.error("缴费期限第"+n+"列数据为空");baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg("缴费期限第"+n+"列数据为空");return baseResult;}else{String payear = getCellValue(rowPayear.getCell(pj));payearMap.put(pj,payear);}}for(int gj = 1; gj <rowGender.getLastCellNum(); gj++){Cell cell=rowGender.getCell(gj);if (cell==null||cell.equals("")||cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {int n=gj+1;log.error("性别第"+n+"列数据为空");baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg("性别第"+n+"列数据为空");return baseResult;}else{String gender = getCellValue(rowGender.getCell(gj));genderMap.put(gj,gender);}}for (int i = 4; i <= sheet.getLastRowNum(); i++) { //第五行开始是年龄Row row = sheet.getRow(i); //第i行// int colCount = row.getLastCellNum(); //每行开始有多少列//过滤空行if (row == null || CheckRowNull(row,row.getLastCellNum())) {break;}int colCount = row.getLastCellNum(); //每行开始有多少列//第一列是年龄,从第二列开始时费率String age = "";Cell cell0 = row.getCell(0);if (cell0==null||cell0.equals("")||cell0.getCellType() == HSSFCell.CELL_TYPE_BLANK) {int m = i+1;log.error("第"+m+"行年龄数据为空");baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg("第"+m+"行年龄数据为空");return baseResult;}else{age = getCellValue(cell0);}for(int j=1;j<colCount;j++){//第j列ExportPtRate ptRate = new ExportPtRate();Cell cell = row.getCell(j);if (cell==null||cell.equals("")||cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {continue;}String premium = getCellValue(cell);BigDecimal bigDecimal = new BigDecimal(premium).multiply(new BigDecimal(amout)).divide(new BigDecimal(1000)).setScale(0,BigDecimal.ROUND_HALF_UP);ptRate.setAge(age);ptRate.setInsuyearFactor(insureMap.get(j));ptRate.setPayendyearFactor(payearMap.get(j));ptRate.setGender(genderMap.get(j));ptRate.setPremium(bigDecimal.toString());dataList.add(ptRate);}}baseResult.setContent(dataList);}} catch (Exception e) {log.error(String.format("parse excel exception!"), e);baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg(e.getMessage());} finally {if (workbook != null) {try {workbook.close();} catch (Exception e) {log.error(String.format("parse excel exception!"), e);baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode());baseResult.setReturnMsg(e.getMessage());}}}return baseResult;}/*** 得到Workbook对象* @param file* @return* @throws IOException*/public static Workbook getWorkBook(MultipartFile file) throws IOException {//这样写  excel 能兼容03和07InputStream is = file.getInputStream();Workbook hssfWorkbook = null;try {hssfWorkbook = new HSSFWorkbook(is);} catch (Exception ex) {is =file.getInputStream();hssfWorkbook = new XSSFWorkbook(is);}return hssfWorkbook;}/**** @param response* @param wb* @param showFileName* @throws IOException*/public static void downloadExcel(HttpServletResponse response, Workbook wb, String showFileName) throws Exception {// 判断数据if(wb == null) {throw new Exception("内容为空");}// 设置excel的文件名称String excelName = "人事信息" ;// 重置响应对象response.reset();// 当前日期,用于导出文件名称SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");String dateStr = excelName+sdf.format(new Date())+".xls";// 指定下载的文件名--设置响应头response.addHeader("Content-Disposition", "attachment;filename=" + new String(dateStr.getBytes("gb2312"), "ISO8859-1"));response.setContentType("application/octet-stream;charset=UTF-8");response.setHeader("Pragma", "no-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);// 写出数据输出流到页面try {OutputStream output = response.getOutputStream();BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);wb.write(bufferedOutPut);bufferedOutPut.flush();bufferedOutPut.close();output.close();} catch (IOException e) {e.printStackTrace();}}}
ExcelColumn.java
package com.ssish.saas.eoms.util;import java.lang.annotation.*;@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExcelColumn {String value() default "";int col() default 0;
} 

上传的excel截图

poi解析不固定列excel相关推荐

  1. java使用poi读取跨行跨列excel

    java使用poi读取跨行跨列excel 1.需求背景 2.实现思路分析 3.重要代码片码说明 4.完整的代码类如下: 5.完整的demo代码提供如下 6.demo执行结果 1.需求背景 最近有一个工 ...

  2. poi解析excel读取日期为数字的问题

    Apache poi 版本:3.12 今天在用poi解析excel文件时,碰到一个蛋疼的问题. 在我的excel文件中有一列是日期类型,例如有以下这么一行数据(日期中月份前面的0会自动去掉): 在读取 ...

  3. Java面试poi中excel版本大小_java 中 poi解析Excel文件版本问题解决办法

    java 中 poi解析Excel文件版本问题解决办法 发布时间:2020-10-02 03:46:15 来源:脚本之家 阅读:91 作者:程诺 poi解析Excel文件版本问题解决办法 poi解析E ...

  4. POI解析Excel表格

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 这里实现poi解析Excel表格的例子,导入Exc ...

  5. POI解析1997-2003、2007版本excel

    本文就poi解析1997-2003/2007版本进行总结,走统一接口. maven引入: sheet存储模型 import java.util.HashMap; import java.util.Ma ...

  6. 【poi-3.8】poi解析excel插入数据库详解

    文章目录 poi解析excel&插入数据库详解 说明 一.目的 二.准备工作 三.思路分析 四.流程图分析 五.核心代码 1:上传文件 2:获取输入流 3:初始化excel表 4:遍历行和列 ...

  7. poi解析Excel文件版本问题

    poi解析Excel文件时有两种格式: HSSFWorkbook格式用来解析Excel2003(xls)的文件 XSSFWorkbook格式用来解析Excel2007(xlsx)的文件 如果用HSSF ...

  8. springboot + poi 解析 excel

    Apache POI 是用 Java 编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对 Microsoft Office 格式档案读和写的功能. 本文使用 s ...

  9. ssh excel 导入 mysql_ssh poi解析excel并将数据存入数据库

    做完了一个报表导入数据库 功能 遇到几个问题:一 .Poi解析excel 的两种格式 xls xlsx. 二.form表单上传使用ajax 三.excel 单元格中有公式存在普通的读取方式读到的是公式 ...

最新文章

  1. 使用jenkins自动化打包部署Vue项目。详细教程。
  2. 使用POI导出百万级数据到excel的解决方案
  3. hdu 3863 No Gambling (不会证明,但是是对的,,)
  4. Android开发入门解答
  5. Android项目实战(三):实现第一次进入软件的引导页
  6. PyTorch:加载自己的数据集
  7. 信号与槽QVariant传递结构体指针
  8. Linux系统下Postgresql数据库安装
  9. python实例方法不可以用类调用_为什么python静态/类方法不可调用?
  10. 王者荣耀美化包制作教程(一;进阶)
  11. HAIKUO-SOFT 作品推荐 四叶草 よつのは 攻略
  12. 给初学者推荐一个摆脱变量命名纠结的神器
  13. JavaEE 企业级分布式高级架构师(十七)ElasticSearch全文检索(1)
  14. 留学生论文essay写作字数不够怎么办?
  15. 企业微信—JS-SDK使用(例:预览文件接口)
  16. 怎么html让元素脱离文档流,子元素设置绝对定位之后脱离文档流!
  17. 换分币c语言程序,编写程序输出用一元人民币兑换成1分、2分和5分硬币的不同兑换方法...
  18. Excel 2019:二级级联下拉框设置
  19. 传输接口的对比 USB、Thunderbolt、HDMI、DP、type-c
  20. v-for与v-if为什么不能同时用?

热门文章

  1. 长时间看电脑的朋友,怎么保护您的眼睛
  2. 计算机excel操作教程,Excel操作教程 -电脑资料
  3. 面对这个缓慢、脆弱、健忘的互联网,IPFS协议势在必行!
  4. 简单发送QQ邮件教程
  5. 【IT圈内事】2019互联网企业100强
  6. mini2440----keil for AMR之IIC读写EEPROM(AT24C08)
  7. 【计算机网络】网络层精选习题1(含联考真题)
  8. /usr/bin/ld: cannot find -lxxx 的解决办法
  9. AcWing 2041. 干草堆
  10. 面试突击51:为什么单例一定要加 volatile?