poi解析不固定列excel

本文介绍了一种使用Java实现的Excel数据导入与解析方法,详细解释了如何通过反射机制读取Excel文件,并将其转换为Java对象列表。同时,文章还讨论了如何处理不同类型的单元格数据,包括数字、字符串、日期等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 columns
                Map<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();
                //默认读取第一个sheet
                Sheet 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);
        //浏览器下载excel
        buildExcelDocument("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) {

                //默认读取第一个sheet
                Sheet 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和07
        InputStream 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截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值