ssm框架整合easypoi例子 不要jeecg
时间: 2023-07-09 17:17:56 浏览: 173
好的,以下是SSM框架整合EasyPoi的例子:
1. 在Maven项目中添加依赖:
```xml
<!-- EasyPoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.2.0</version>
</dependency>
```
2. 创建Excel导出工具类
```java
public class ExcelUtils {
/**
* 导出Excel
*
* @param list 数据列表
* @param fileName 文件名
* @param response 响应对象
*/
public static void exportExcel(List<?> list, String fileName, HttpServletResponse response) {
// 设置响应头
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// 创建Excel对象
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), list.get(0).getClass(), list);
// 输出Excel
try (OutputStream out = response.getOutputStream()) {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
3. 创建Excel导入工具类
```java
public class ExcelUtils {
/**
* 导入Excel
*
* @param file Excel文件
* @param clazz 实体类类型
* @param <T> 实体类
* @return 数据列表
*/
public static <T> List<T> importExcel(MultipartFile file, Class<T> clazz) {
try {
// 创建Excel对象
Workbook workbook = WorkbookFactory.create(file.getInputStream());
// 获取第一个Sheet
Sheet sheet = workbook.getSheetAt(0);
// 获取表头行
Row headerRow = sheet.getRow(0);
// 获取表头列名
List<String> columnNames = new ArrayList<>();
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
Cell cell = headerRow.getCell(i);
columnNames.add(cell.getStringCellValue());
}
// 获取数据行
List<T> dataList = new ArrayList<>();
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row dataRow = sheet.getRow(i);
// 创建实体对象
T data = clazz.newInstance();
// 设置实体属性值
for (int j = 0; j < dataRow.getLastCellNum(); j++) {
Cell cell = dataRow.getCell(j);
String columnName = columnNames.get(j);
Field field = ReflectionUtils.findField(clazz, columnName);
if (field != null) {
field.setAccessible(true);
if (field.getType() == String.class) {
field.set(data, cell.getStringCellValue());
} else if (field.getType() == Integer.class || field.getType() == int.class) {
field.set(data, (int) cell.getNumericCellValue());
} else if (field.getType() == Long.class || field.getType() == long.class) {
field.set(data, (long) cell.getNumericCellValue());
} else if (field.getType() == Double.class || field.getType() == double.class) {
field.set(data, cell.getNumericCellValue());
} else if (field.getType() == Date.class) {
field.set(data, cell.getDateCellValue());
}
}
}
dataList.add(data);
}
return dataList;
} catch (IOException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return Collections.emptyList();
}
}
```
4. 在Controller中使用Excel导出和导入
```java
@RestController
@RequestMapping("/excel")
public class ExcelController {
@GetMapping("/export")
public void export(HttpServletResponse response) {
List<User> userList = userService.list();
ExcelUtils.exportExcel(userList, "用户列表.xlsx", response);
}
@PostMapping("/import")
public void importExcel(@RequestParam("file") MultipartFile file) {
List<User> userList = ExcelUtils.importExcel(file, User.class);
userService.saveBatch(userList);
}
}
```
这就是SSM框架整合EasyPoi的例子,希望对你有所帮助!
阅读全文