SpringMvc+POI如何处理excel表数据导入
对于Spring MVC来说,经常需要对Excel表进行数据导入,而POI是一个非常方便的Java类库,可以在Java中读取、写入和操作各种类型的电子文档,比如Excel、Word和PowerPoint等。在Spring MVC中使用POI来处理Excel表数据导入是非常常见的。在本文中,我们将介绍一下如何使用POI来处理Excel表数据导入。
1. Maven配置
首先,我们需要在Maven中添加POI依赖项。在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上传
首先,我们需要编写一个jsp页面来上传Excel表。在页面中添加一个文件选择框和一个上传按钮。如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Excel Upload</title>
</head>
<body>
<h1>Excel Upload</h1>
<form method="post" action="uploadExcel" enctype="multipart/form-data">
<input type="file" name="file" /><br/><br/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
这里使用了“multipart/form-data”数据格式,并将表单提交到一个名为“uploadExcel”的处理器方法。
3. 处理Excel文件
在处理器方法中,我们需要读取上传的Excel文件,并将其转换为Java对象。将Excel表中的每一行转换为一个Java对象,并将这些对象插入数据库或返回到jsp页面。
@PostMapping("/uploadExcel")
public String uploadExcel(@RequestParam("file") MultipartFile file, Model model) {
try {
Workbook workbook = new XSSFWorkbook(file.getInputStream());
Sheet sheet = workbook.getSheet("Sheet1");
Iterator<Row> rows = sheet.iterator();
List<Employee> employees = new ArrayList<Employee>();
while (rows.hasNext()) {
Row currentRow = rows.next();
if (currentRow.getRowNum() == 0) {
continue;
}
Iterator<Cell> cellsInRow = currentRow.iterator();
Employee employee = new Employee();
while (cellsInRow.hasNext()) {
Cell currentCell = cellsInRow.next();
int columnIndex = currentCell.getColumnIndex();
switch (columnIndex) {
case 0:
employee.setId((long) currentCell.getNumericCellValue());
break;
case 1:
employee.setName(currentCell.getStringCellValue());
break;
case 2:
employee.setAge((int) currentCell.getNumericCellValue());
break;
case 3:
employee.setSalary((float) currentCell.getNumericCellValue());
break;
}
}
employees.add(employee);
}
employeeRepository.saveAll(employees);
model.addAttribute("employees", employees);
} catch (IOException e) {
e.printStackTrace();
}
return "employeeList";
}
在这个处理器方法中,首先使用POI的Workbook类创建一个新的工作簿。然后使用getSheet方法获取工作簿中的表单。接下来,我们使用Sheet的iterator方法获取每一行。由于Excel表的 行通常是标题行,所以我们需要跳过它。
接着,我们使用Row的iterator方法获取每一行中的单元格。然后根据单元格索引将单元格值读取到Java对象中。在本例中,我们使用一个Employee对象来保存每一行的值。Employee对象需要包含id、name、age和salary四个属性。
4. JSP页面显示
最后,我们需要显示导入的数据。在jsp页面中,我们可以使用以下代码循环遍历每个Employee对象并显示其属性。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Employee List</title>
</head>
<body>
<h1>Employee List</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Salary</th>
</tr>
<c:forEach var="employee" items="${employees}">
<tr>
<td>${employee.id}</td>
<td>${employee.name}</td>
<td>${employee.age}</td>
<td>${employee.salary}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
在这个jsp页面中,我们使用了JSTL标签库。使用<c:forEach>标签循环遍历每个Employee对象,并显示其属性。我们只需要将Employee对象列表添加到模型中,并将其传递给视图即可。
这就是如何在Spring MVC中使用POI来处理Excel表数据导入的方法。有了POI,处理Excel表数据导入变得非常简单。
