springboot 实现导入excel,解析excel数据并向数据库插入insert

导入excel文件,大致如下:
在这里插入图片描述

controller层:此处file文件写死,自行修改

@RestController
@RequestMapping("/import")
public class ImportExcelController {

    @Autowired
    private ImportExcelService importExcelService;

    @RequestMapping(value = "/excel",method = RequestMethod.POST)
    public String importExcelDB(){
        //装载流
        XSSFWorkbook workbook = null;
        File file = new File("D:\\副本系统所需资料港汇店.xlsx");

        try {
            workbook = new XSSFWorkbook(file.getPath());
        }catch (IOException e) {
            e.printStackTrace();
        }

        List<TestExcelEntity> list = new ArrayList<>();
        // 获取一个工作表,下标从0开始
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        // 通过循环,逐行取出表中每行数据
        for(int i=0;i<=lastRowNum;i++){//跳过第一行和第二行
            if(i==0 || i==1){
                continue;
            }

            // 获取行
            XSSFRow row = sheet.getRow(i);

            TestExcelEntity excelEntity = new TestExcelEntity();

            excelEntity.setId(Double.valueOf(row.getCell(0).getNumericCellValue()).longValue()+"");
            excelEntity.setCardno(Double.valueOf(row.getCell(1).getNumericCellValue()).longValue()+"");
            excelEntity.setSaleTime(row.getCell(2).toString());
            excelEntity.setSaleSoure(row.getCell(3).toString());
            excelEntity.setSalePeople(row.getCell(4).toString());
            excelEntity.setRemark(row.getCell(5).toString());
            excelEntity.setCardCeateTime(row.getCell(6).toString());
            excelEntity.setEndTime(row.getCell(7).toString());
            excelEntity.setIsExperClass(row.getCell(8).toString());
            excelEntity.setClassName(row.getCell(9).toString());
            excelEntity.setClassTime(Double.valueOf(row.getCell(10).getNumericCellValue()).longValue()+"");
            excelEntity.setClassPrice(Double.valueOf(row.getCell(11).getNumericCellValue()).longValue()+"");
            excelEntity.setAdvisePrice(Double.valueOf(row.getCell(12).getNumericCellValue()).longValue()+"");
            excelEntity.setSalePrice(Double.valueOf(row.getCell(13).getNumericCellValue()).longValue()+"");
            excelEntity.setGiveClass(row.getCell(14).toString());
            excelEntity.setParentName(row.getCell(15).toString());
            excelEntity.setPhone(Double.valueOf(row.getCell(16).getNumericCellValue()).longValue()+"");
            excelEntity.setBabyName(row.getCell(17).toString());
            excelEntity.setBabyAge(Double.valueOf(row.getCell(18).getNumericCellValue()).longValue()+"");
            excelEntity.setBabySex(row.getCell(19).toString());
            excelEntity.setBabyBrith(row.getCell(20).toString());

            list.add(excelEntity);
        }

        for (TestExcelEntity excelEntity : list) {
            System.out.println(excelEntity);
        }
        importExcelService.importExcelDB(list);

        return new HashMap<String,String>().put("msg","OK!!");
    }
}

Service层:

@Service
public class ImportExcelServiceImpl implements ImportExcelService {
    @Autowired
    private ImportExcelMapper importExcelMapper;
    @Override
    public void importExcelDB(List<TestExcelEntity> list) {
        importExcelMapper.importExcelDB(list);
    }
}

DAO层:

@Mapper
public interface ImportExcelMapper {
    void importExcelDB(@Param("list") List<TestExcelEntity> list);
}

xml:

<insert id="importExcelDB" parameterType="java.util.List" useGeneratedKeys="false" >
      insert into test_entity (id,CARDNO,SALETIME,SALESOURE,SALEPEOPLE,REMARK,CARDCEATETIME,ENDTIME,ISEXPERCLASS,CLASSNAME,CLASSTIME,
        CLASSPRICE,ADVISEPRICE,SALEPRICE,GIVECLASS,PARENTNAME,PHONE,BABYNAME,BABYAGE,BABYSEX,BABYBRITH)
      <foreach collection="list" item="item" index="index" separator="UNION ALL" >
          ( select #{item.id},
          #{item.cardno},
          #{item.saleTime},
          #{item.saleSoure},
          #{item.salePeople},
          #{item.remark},
          #{item.cardCeateTime},
          #{item.endTime},
          #{item.isExperClass},
          #{item.className},
          #{item.classTime},
          #{item.classPrice},
          #{item.advisePrice},
          #{item.salePrice},
          #{item.giveClass},
          #{item.parentName},
          #{item.phone},
          #{item.babyName},
          #{item.babyAge},
          #{item.babySex},
          #{item.babyBrith} from dual)
      </foreach>
    </insert>

就可以直接导入到数据库中了,此处数据库是Oracle。
一点毛病也没有~

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot可以使用EasyExcel实现Excel导入数据。EasyExcel是阿里基于poi开源的一个项目,它可以帮助我们更方便地实现Excel导入与导出功能。在Spring Boot中使用EasyExcel,我们可以综合应用各种Spring知识,代码量并不大。首先,在Controller层,我们可以提供一个访问接口,通过POST请求方式传入Excel文件。在请求中,我们需要将文件一同传入,并获取文件名用于后续判断是否为Excel文件。然后,我们可以调用Service层的batchImport方法进行业务逻辑处理。在Service层,我们可以使用EasyExcel提供的API来读取Excel文件中的数据,并进行相应的处理。最后,我们可以返回处理结果给前端。这样,就可以实现Spring Boot中的Excel导入数据功能。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [SpringBoot 注解 + 反射优雅的实现Excel导入导出,通用加强版!](https://blog.csdn.net/afreon/article/details/126756870)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot提供接口实现Excel导入数据并存储到数据库中](https://blog.csdn.net/m0_51197424/article/details/124454553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值