Spring Boot是一个非常流行的Java Web框架,它提供了很多实用的功能,其中之一就是通过POI库实现数据的导入和导出。本文将介绍如何使用Spring Boot和POI库来实现数据的导入和导出。
1.添加POI依赖
首先,我们需要在项目中添加POI的依赖。在Maven项目中,可以在pom.xml
文件中添加以下依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2.创建导出Excel的Controller
我们需要创建一个Controller来处理导出Excel的请求。在本例中,我们将导出一个简单的学生信息表格。首先,我们需要定义一个Student
类:
public class Student {
private String name;
private int age;
private String gender;
// 省略getter和setter方法
}
然后,我们创建一个ExportController
类,定义一个export()
方法来处理导出Excel的请求:
@RestController
@RequestMapping("/export")
public class ExportController {
@GetMapping("/students")
public void export(HttpServletResponse response) throws IOException {
// 1. 创建一个Excel文件
Workbook workbook = new XSSFWorkbook();
// 2. 创建一个工作表
Sheet sheet = workbook.createSheet("学生信息");
// 3. 创建表头
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("姓名");
header.createCell(1).setCellValue("年龄");
header.createCell(2).setCellValue("性别");
// 4. 添加数据
List<Student> students = getStudents();
int rowNum = 1;
for (Student student : students) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(student.getName());
row.createCell(1).setCellValue(student.getAge());
row.createCell(2).setCellValue(student.getGender());
}
// 5. 输出Excel文件
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-disposition", "attachment;filename=students.xlsx");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
// 获取学生列表
private List<Student> getStudents() {
List<Student> students = new ArrayList<>();
students.add(new Student("张三", 18, "男"));
students.add(new Student("李四", 20, "女"));
students.add(new Student("王五", 22, "男"));
return students;
}
}
在上面的代码中,我们首先创建一个Workbook
对象表示Excel文件,然后创建一个工作表和表头。接着,我们通过getStudents()
方法获取学生列表,并将列表中的数据添加到Excel文件中。最后,我们将Excel文件输出到响应中。
3.创建导入Excel的Controller
接下来,我们需要创建一个Controller来处理导入Excel的请求。在本例中,我们将导入一个学生信息表格。首先,我们需要定义一个ImportForm
类:
public class ImportForm {
private MultipartFile file;
// 省略getter和setter方法
}
然后,我们创建一个ImportController
类,定义一个import()
方法来处理导入Excel的请求:
@RestController @RequestMapping("/import")
public class ImportController {
@PostMapping("/students")
public List<Student> importStudents(@RequestParam("file") MultipartFile file) throws IOException {
// 1. 创建一个Workbook对象
Workbook workbook = new XSSFWorkbook(file.getInputStream());
// 2. 获取第一个工作表
Sheet sheet = workbook.getSheetAt(0);
List<Student> students = new ArrayList<>();
// 3. 遍历每一行
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue;
}
// 4. 读取每一列
String name = row.getCell(0).getStringCellValue();
int age = (int) row.getCell(1).getNumericCellValue();
String gender = row.getCell(2).getStringCellValue();
students.add(new Student(name, age, gender));
}
return students;
}
}
在上面的代码中,我们首先创建一个Workbook
对象,并获取第一个工作表。接着,我们遍历每一行,并读取每一列的数据。最后,我们将学生信息封装成Student
对象,并返回一个学生列表。
4.测试导出和导入功能
现在,我们可以测试导出和导入功能了。在浏览器中输入以下URL,即可下载一个Excel文件:
http://localhost:8080/export/students
在Excel文件中填写一些学生信息后,保存文件。然后,在浏览器中输入以下URL,即可上传Excel文件并返回学生列表:
http://localhost:8080/import/students
至此,我们已经成功实现了通过Spring Boot和POI库实现数据的导入和导出。
以上的案例是没有考虑ID的情况,如有需要请根据自己需求在导入的时候进行更改。