springboot批量导入excel数据

1 背景

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

2 介绍

2.1 框架

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

1. Apache POI

2. Java Excel API

3. easyexcel

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

2.2 excel字段介绍

1.sheet表示的是

excel底部的工作表.

对应的是POI的的XSSFSheet

扫描二维码关注公众号,回复: 6451323 查看本文章

2.row表示的是行

对应的是POI的的XSSFRow

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

对应的是POI的的Cell

3 源码

3.0 片段说明

1.上传文件使用springboot的MultipartFile 对应

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,但是累属性是整形的话,可以使用枚举类

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

转载于:https://juejin.im/post/5cff11916fb9a07eb051b7f0

猜你喜欢

转载自blog.csdn.net/weixin_34375251/article/details/91456907