springboot批量导入excel数据

14次阅读

共计 2657 个字符,预计需要花费 7 分钟才能阅读完成。

1 背景

小白今天闲着没事,在公司摸鱼,以为今天有事无聊的一天,突然上头说小子,今天实现一下批量导入 Excel 数据吧,当时我的内心是拒绝的,然后默默打开 idea。

2 介绍

2.1 框架

java 本身并不支持读取 excel,所有读取 excel 需要借助一些框架。目前有几种方式,

<font color=red>1. Apache POI</font>

<font color=red>2. Java Excel API</font>

<font color=red>3. easyexcel</font>

这里主要讲解的是 <font color=red> Apache POI</font>,Apache POI 支持 03 版以及 07 年版 区别是后缀不一样,03 版对应的是 <font color=red>xls</font> 07 版对应的是 xlsx<font color=red> xlsx</font>
这里主要讲解的是 07 版的

2.2 excel 字段介绍

1.sheet 表示的是

excel 底部的工作表.

对应的是 POI 的的 <font color=red>XSSFSheet</font>

2.row 表示的是行

对应的是 POI 的的 <font color=red>XSSFRow</font>

3.cell 表示的是每一行的单元格.

对应的是 POI 的的 <font color=red>Cell</font>

3 源码

3.0 片段说明

1. 上传文件使用 springboot 的 <font color=red>MultipartFile</font>
对应

MultipartFile file

2. 创建对象

XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);

3. 获取 sheet(默认第一个)

 XSSFSheet sheet = xssfWorkbook.getSheetAt(0);

3.1 控制层源码

@RequestMapping("/import")
public void importExcel(@RequestParam("file") MultipartFile file) throws Exception{InputStream inputStream = file.getInputStream();

    //07 年的 不兼容之前
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);

    XSSFSheet sheet = xssfWorkbook.getSheetAt(0);

    // 获取行数
    int lastRowNum = sheet.getLastRowNum();
    for (int i = 1; i <= lastRowNum; i++) {XSSFRow row = sheet.getRow(i);
        QuChannel quChannel = new QuChannel();
        if (row.getCell(0) != null){row.getCell(0).setCellType(XSSFCell.CELL_TYPE_STRING);
            quChannel.setChannel(row.getCell(0).getStringCellValue());
        }
        if (row.getCell(1) != null){row.getCell(1).setCellType(XSSFCell.CELL_TYPE_STRING);
            quChannel.setChannelName(row.getCell(1).getStringCellValue());
        }
        if (row.getCell(2) != null){row.getCell(2).setCellType(XSSFCell.CELL_TYPE_STRING);
            quChannel.setRemarks(row.getCell(2).getStringCellValue());
        }
        if (row.getCell(3) != null){quChannel.setChannelSource((int) row.getCell(3).getNumericCellValue());
        }
        if (row.getCell(4) != null){quChannel.setActivityType((int) row.getCell(4).getNumericCellValue());
        }
        if (row.getCell(5) != null){quChannel.setDeliveryTime(row.getCell(5).getDateCellValue());
        }
        if (row.getCell(6) != null){row.getCell(6).setCellType(XSSFCell.CELL_TYPE_STRING);
            quChannel.setOriginalLink(row.getCell(6).getStringCellValue());
        }
        if (row.getCell(7) != null){row.getCell(7).setCellType(XSSFCell.CELL_TYPE_STRING);
            quChannel.setSaLink(row.getCell(7).getStringCellValue());
        }
        if (row.getCell(8) != null){quChannel.setDeliveryMode((int) row.getCell(8).getNumericCellValue());
        }
        if (row.getCell(9) != null){quChannel.setCreateGroup((int) row.getCell(9).getNumericCellValue());
        }
        if (row.getCell(10) != null){row.getCell(10).setCellType(XSSFCell.CELL_TYPE_STRING);
            quChannel.setRemark(row.getCell(10).getStringCellValue());
        }
        quChannelMapper.insert(quChannel);

    }
}

3.2 review

1. 避免将 sql 写在 for 循环里面,改进的话可以创建一个列表 list,将对象 add 进去,然后在循环外面进行批量插入

2. 想要去重的话可以使用 set 的不能重复添加特性

3. 注意 excel 的字段与类属性的对应关系,如果 excel 字段是 string,但是累属性是整形的话,可以使用枚举类

暂时想到这么多 欢迎指教评论

正文完
 0