随笔 - 65  文章 - 0  评论 - 3  阅读 - 10万

SpringBoot导入Excel数据到MySQL数据库

package com.example.example1.Controller;

复制代码
 1 import com.example.example1.Entity.User;
 2 import com.example.example1.Service.UserRepository;
 3 import org.apache.poi.hssf.usermodel.*;
 4 import org.apache.poi.ss.usermodel.Cell;
 5 import org.apache.poi.ss.usermodel.Row;
 6 import org.apache.poi.ss.usermodel.Sheet;
 7 import org.apache.poi.ss.usermodel.Workbook;
 8 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.GetMapping;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.ResponseBody;
14 
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.IOException;
View Code
复制代码
复制代码
/**
 * Created by BLIT on 2018/11/30.
 */
@Controller
@RequestMapping(path = "/excel")
public class ImportController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping(path = "/import")
    @ResponseBody
    public void Import() throws IOException{
        File file = new File("C:\\Users\\zxg\\Desktop\\测试2003.xls"); //实际这个路径由前端传后台
        FileInputStream fis = new FileInputStream(file);
        Workbook wb = null;
        try {
            if(isExcel2003(file.getPath())){
                System.out.println("2003版本Excel: .xls结尾");
                wb = new HSSFWorkbook(fis); //创建2003版本Excel工作簿对象
            }else if (isExcel2007(file.getPath())){
                System.out.println("2007版本Excel: .xlsx结尾");
                wb = new XSSFWorkbook(fis); //创建2007版本Excel工作簿对象
            }else {
                System.out.println("未知版本的Excel !!!");
            }
            Sheet sheet = wb.getSheetAt(0); //获取第1个工作表
            for(int i=1;i<=sheet.getLastRowNum();i++){//循环Excel文件的i=1行开始
                User user = new User();
                Row row = sheet.getRow(i); //获取第i行
                Cell cell1 = row.getCell(0); //获取第1个单元格的数据
                cell1.setCellType(Cell.CELL_TYPE_STRING); //设置Cell的类型为String类型
                Cell cell2 = row.getCell(1); //获取第2个单元格的数据
                cell2.setCellType(Cell.CELL_TYPE_STRING);
                user.setName(cell1.getStringCellValue());
                user.setEmail(cell2.getStringCellValue());
                System.out.println("第一单元格: " + cell1.getStringCellValue());
                System.out.println("第二单元格: " + cell2.getStringCellValue());
                userRepository.save(user); //保存
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            fis.close();
        }
    }

    public static boolean isExcel2003(String filePath)
    {
        return filePath.matches("^.+\\.(?i)(xls)$");
    }

    public static boolean isExcel2007(String filePath)
    {
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }

}
复制代码

 

posted on   陌生街中吹起褪色故梦  阅读(4273)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示