poi对excel进行读取

一、poi对excel进行读取背景:
在我们日常的开发中经常会有产品经理或者项目上有要求就是实现excel导入这个功能,今天花点时间做了这个功能。在以后工作中可以直接使用和持续完善。
二、实现代码:
1.公用实体-excel组件配置

public class Config {
   

    private int titleRow; //标题行

    private int headRow; //头部行

    private int startRow; //开始行

    private String title; //标题

    public int getTitleRow() {
   
        return titleRow;
    }

    public void setTitleRow(int titleRow) {
   
        this.titleRow = titleRow;
    }

    public int getHeadRow() {
   
        return headRow;
    }

    public void setHeadRow(int headRow) {
   
        this.headRow = headRow;
    }

    public int getStartRow() {
   
        return startRow;
    }

    public void setStartRow(int startRow) {
   
        this.startRow = startRow;
    }

    public String getTitle() {
   
        return title;
    }

    public void setTitle(String title) {
   
        this.title = title;
    }

}

2.公用实体-合并单元格结果对象

public class MergeCellResult {
   

    private int firstRow;

    private int lastRow;

    private int firstColumn;

    private int lastColumn;

    private boolean isMergeCell;

    public MergeCellResult() {
   
        super();
    }

    public MergeCellResult(int firstRow, int lastRow, int firstColumn, int lastColumn, boolean isMergeCell) {
   
        super();
        this.firstRow = firstRow;
        this.lastRow = lastRow;
        this.firstColumn = firstColumn;
        this.lastColumn = lastColumn;
        this.isMergeCell = isMergeCell;
    }

    public int getFirstRow() {
   
        return firstRow;
    }

    public void setFirstRow(int firstRow) {
   
        this.firstRow = firstRow;
    }

    public int getLastRow() {
   
        return lastRow;
    }

    public void setLastRow(int lastRow) {
   
        this.lastRow = lastRow;
    }

    public int getFirstColumn() {
   
        return firstColumn;
    }

    public void setFirstColumn(int firstColumn) {
   
        this.firstColumn = firstColumn;
    }

    public int getLastColumn() {
   
        return lastColumn;
    }

    public void setLastColumn(int lastColumn) {
   
        this.lastColumn = lastColumn;
    }

    public boolean isMergeCell() {
   
        return isMergeCell;
    }

    public void setMergeCell(boolean isMergeCell) {
   
        this.isMergeCell = isMergeCell;
    }

}

3.excel组件反射工具类

public class ExcelReflect {
   

    /**
      * 从object中获取execel注解字段的值
      * @param obj  object实体对象
      * @param excelFieldMap    excel字段集合
      * @return 值集合
     */
    public static Map<String, String> getFieldsValue(Object obj, Map<String, Object> excelFieldMap) {
   
        Map<String, String> valueMap = new HashedMap<String, String>();
        for (String fieldName : excelFieldMap.keySet()) {
   
            AppCloudExcel appCloudExcel = (AppCloudExcel) excelFieldMap.get(fieldName);
            valueMap.put(appCloudExcel.serial() + "", fieldExtValue(fieldName, obj).toString());
        }
        return valueMap;
    }

    /**
      * 获取class类中注解excel的字段集合
      * @param clazz    class类
      * @return excel字段集合
     */
    public static <T> Map<String, Object> getClassExcelFieldsList(Class<T> clazz) {
   
        Field[] fields = clazz.getDeclaredFields();
        Map<String, Object> excelFieldMap = new HashedMap<String, Object>();
        for (int j = 0; j < fields.length; j++) {
   
            AppCloudExcel appCloudExcel = fields[j].getAnnotation(AppCloudExcel.class);
            if (null != appCloudExcel) {
   
                excelFieldMap.put(fields[j].getName(), fields[j].getAnnotation(AppCloudExcel.class));
            }
        }
        return excelFieldMap;

    }

    /**
      * list的map集合转换为list的object集合
      * @param listMap list的map集合
      * @param clazz    class类
      * @return list的object集合
     */
    @SuppressWarnings({
    "unchecked" })
    public static <T> List<T> listMapToListObj(List<Map<String, String>> listMap, Class<T> clazz) {
   
        List
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值