Springboot 最简单的结合MYSQL数据实现EXCEL表格导出及数据导入

本文介绍了如何使用SpringBoot结合MySQL数据库,实现Excel表格的导出和数据导入功能。通过调用ExcelExportUtil和ExcelImportUtil工具类,实现了Excel的创建和解析。同时展示了UserMapper接口及其实现,用于查询和插入数据库中的用户信息。
摘要由CSDN通过智能技术生成

defaultExport(list, pojoClass, fileName, response, exportParams);

}

//导出

public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){

defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));

}

public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){

defaultExport(list, fileName, response);

}

private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {

Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);

if (workbook != null);

downLoadExcel(fileName, response, workbook);

}

private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {

try {

response.setCharacterEncoding(“UTF-8”);

response.setHeader(“content-Type”, “application/vnd.ms-excel”);

response.setHeader(“Content-Disposition”,

“attachment;filename=” + URLEncoder.encode(fileName, “UTF-8”));

workbook.write(response.getOutputStream());

} catch (IOException e) {

throw new RuntimeException(e.getMessage());

}

}

private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {

Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);

if (workbook != null);

downLoadExcel(fileName, response, workbook);

}

//导入

public static List importExcel(String filePath,Integer titleRows,Integer headerRows, Class pojoClass){

if (StringUtils.isBlank(filePath)){

return null;

}

ImportParams params = new ImportParams();

params.

实现Excel数据导入MySQL数据库,可以采用以下步骤: 1. 在Spring Boot项目中添加Apache POI和MySQL数据库的依赖。 2. 创建一个包含Excel数据的POJO类,例如Student类,用于存储Excel中的每一行数据。 3. 创建一个Excel文件上传的Controller类,实现文件上传功能,读取Excel数据并将数据存储在List<Student>中。 4. 创建一个Service类,实现将List<Student>中的数据插入到MySQL数据库的功能。 5. 在Service类中使用JDBC或MyBatis等框架,连接 MySQL 数据库,将Excel数据插入到数据库。 下面是一个简单实现: 1. 添加依赖 在pom.xml中添加Apache POI和MySQL数据库的依赖: ``` <!-- Apache POI --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> ``` 2. 创建POJO类 创建一个Student类,用于存储Excel中的每一行数据: ```java public class Student { private String name; private int age; private String gender; // 省略getter和setter方法 } ``` 3. 创建Controller类 创建一个Excel文件上传的Controller类,实现文件上传功能,读取Excel数据并将数据存储在List<Student>中: ```java @RestController @RequestMapping("/api/excel") public class ExcelController { @PostMapping("/upload") public String upload(@RequestParam("file") MultipartFile file) { try { List<Student> students = new ArrayList<>(); // 读取Excel文件 Workbook workbook = WorkbookFactory.create(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); // 遍历每一行数据 for (Row row : sheet) { Student student = new Student(); student.setName(row.getCell(0).getStringCellValue()); student.setAge((int)row.getCell(1).getNumericCellValue()); student.setGender(row.getCell(2).getStringCellValue()); students.add(student); } // 调用Service类将数据插入到MySQL数据库 studentService.insertStudents(students); return "上传成功!"; } catch (Exception e) { e.printStackTrace(); return "上传失败!"; } } } ``` 4. 创建Service类 创建一个Service类,实现将List<Student>中的数据插入到MySQL数据库的功能: ```java @Service public class StudentService { @Autowired private JdbcTemplate jdbcTemplate; public void insertStudents(List<Student> students) { String sql = "INSERT INTO student(name, age, gender) VALUES (?, ?, ?)"; for (Student student : students) { jdbcTemplate.update(sql, student.getName(), student.getAge(), student.getGender()); } } } ``` 5. 测试 启动Spring Boot应用程序,并使用Postman或其他工具向/upload端点上传Excel文件,可以看到数据已成功插入到MySQL数据库中。 以上就是Spring Boot实现Excel数据导入MySQL简单实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值