springboot+mybatis-plus通过excel表导入数据到数据库

本文介绍了如何使用SpringBoot结合Mybatis-Plus,通过Excel表格将数据导入到数据库中,详细讲解了相关的Controller、Service及实现类的代码实现。
摘要由CSDN通过智能技术生成

通过excel表导入数据到数据库

以下为具体代码:

controller类代码

   

    @Autowired
    private SzyPanoramaService szyPanoramaService;

    @Autowired
    private ImportExcelService excelService;


    /**
     * 通过excel导入数据库
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload")
    public ResponseUtils excelupload(@RequestParam(value = "file", required = true) MultipartFile file) throws Exception{
   

        //获取文件名
        String fileName = file.getOriginalFilename();
        //获取输入流
        InputStream inputStream = file.getInputStream();
        List<Map<String,String>> mapList = excelService.importExcel(inputStream, fileName);
        szyPanoramaService.savePanorama(mapList);

        return ResponseUtils.successmsg("返回数据成功",null);
    }

新建一个ImportExcelService类

@Service
public class ImportExcelService {
   

    public List<Map<String,String>> importExcel(InputStream inputStream, String fileName) throws Exception {
   

        List<Map<String,String>> list = new ArrayList<>();
        boolean notNull = false;
        List<SzyPanorama> userList = new ArrayList<SzyPanorama>();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
   
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值