基于SpringBoot框架 导入Excel文档

本文介绍了如何在SpringBoot项目中实现Excel文档的导入,包括所需的jar包导入、Controller层和Service层的代码示例,以及最终的效果展示。
摘要由CSDN通过智能技术生成

基于SpringBoot框架 导入Excel文档

前言:

关于SpringBoot中Excel文档的上传解析

提示:以下是本篇文章正文内容,下面案例可供参考

使用步骤:

1.导入jar包

代码如下(示例):

<!-- xls,xlsx文件 解析jar包 -->
      <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
      </dependency>
      <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
      </dependency>
      <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
      </dependency>

2.编写Contriller层

代码如下(示例):

@PostMapping("/importuserinfo")
    public CommonResult importUserInfo(@RequestParam("file") MultipartFile file , @RequestParam("schoolId")String schoolId , @RequestParam("areaCode")String areaCode ) {
   //参数:文件,学校id,地区代码

        CommonResult result = new CommonResult();//这个是我自己写的JSON传输对象

        Object resultTpye = null;//结果:true,false
        String fileName = file.getOriginalFilename();//获取文件名
        try {
   
            resultTpye = excelService.importUserInfo(fileName, file, schoolId, areaCode);
        } catch (Exception e) {
   
            e.printStackTrace();
        }

        if(!resultTpye.equals(false) || resultTpye != null){
   //插入成功
            result.setCode(CommonAPI.CODE_200000.getCode());
            result.setMessage(CommonAPI.CODE_200000.getMessage());
            result.setData(resultTpye);
        }else {
   
            result.setCode(CommonAPI.CODE_500012.getCode());
            result.setMessage(CommonAPI.CODE_500012.getMessage());
            result.setData(resultTpye);
        }

        return  result;
    }

3.编写Service业务实现类

代码如下(示例):这边我想导入的是学生信息,信息中包含多张表

/**
     * //excel文件解析 学生信息 插入数据库
     * @param fileName
     * @param file
     * @param schoolId
     * @param areaCode
     * @return
     * @throws Exception
     */
    @Override
    public Object importUserInfo(String fileName, MultipartFile file, String schoolId, String areaCode) throws Exception {
   

        boolean notNull = false;
        if (!fileName
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值