美文网首页
springboot批量导入excel数据

springboot批量导入excel数据

作者: 布尔bl | 来源:发表于2019-06-11 10:30 被阅读0次

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表示的是

VcMuid.png

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

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

相关文章

  • springboot批量导入excel数据

    1 背景 小白今天闲着没事,在公司摸鱼,以为今天有事无聊的一天,突然上头说小子,今天实现一下批量导入Excel数据...

  • js文件上传 、导入报表excel

    这里导入分两种 :1.导入excel数据到后台:批量导入2.导入excel数据到网页:单条导入 导入的excel模...

  • 多线程批量导入数据

    如何用多线程批量导入数据?最近的使用场景就是批量导入excel数据,数据量太大的话会导致超时、服务异常超时等问题。...

  • React实战(2)——Ant Design Pro项目导入导出

    一、Excel批量导入数据 1、实现代码 2、Excel原始数据处理 Excel中出生年月是Excel默认时间格式...

  • mysql运用

    Excel表格数据导入MySQL数据库 有时候项目需要将存在表格中的批量数据导入数据库,最近自己正好碰到了,总结一...

  • SpringBoot解析Excel

    现在很多web应用中,导入excel导出excel很常见,这篇文章就讲讲导入excel文件。 以批量导入课程为例 ...

  • Springboot中导入Excel

    有时候我们需要导入Excel文件,并读取里面的数据 在springboot中我们通常使用POI读取解析Excel文...

  • Vue+Element前端导入导出Excel

    1 前言 1.1 业务场景 由前台导入Excel表格,获取批量数据。 根据一个数组导出Excel表格。 2 实现原...

  • 5.1 批量导入订单

    商家通过excel批量导入运单,进行下单

  • java入门019~springboot批量导入excel数据到

    我们在前面的章节已经讲了如何用jpa或者mybatis来操作mysql数据库。这一节我们就来结合具体案例,来讲解下...

网友评论

      本文标题:springboot批量导入excel数据

      本文链接:https://www.haomeiwen.com/subject/jfsffctx.html