首先添加依赖
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
commons-io
commons-io
2.6
commons-fileupload
commons-fileupload
1.4
cn.afterturn
easypoi-base
3.2.0
cn.afterturn
easypoi-web
3.2.0
cn.afterturn
easypoi-annotation
3.2.0
新建 EasyPoiUtils 工具类
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.pigs.springbooteasypoipigs.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
/**
* @author PIGS
* @version 1.0
* @date 2020/4/25 14:16
* @effect :
* 表格数据工具类
*/
public final class EasyPoiUtils {
private EasyPoiUtils() {
}
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);
}
}
private static void defaultExport(List dataList, Class> clz, String fileName, HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, clz, dataList);
if (workbook != null) {
downLoadExcel(fileName, response, workbook);
}
}
public static void exportExcel(List dataList, String title, String sheetName, Class> clz, String fileName, boolean isCreateHeader, HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(dataList, clz, fileName, response, exportParams);
}
public static void exportExcel(List dataList, String title, String sheetName, Class> clz, String fileName, HttpServletResponse response) {
defaultExport(dataList, clz, fileName, response, new ExportParams(title, sheetName));
}
private static void defaultExport(List
新建一个实体类接收参数
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author PIGS
* @version 1.0
* @date 2020/4/25 14:16
* @effect :
* 用户实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {
@Excel(name = "用户ID", width = 15)
private Integer userId;
@Excel(name = "用户名", width = 15)
private String userName;
@Excel(name = "用户性别", width = 15)
private String userSex;
@Excel(name = "用户年龄", width = 15)
private Integer userAge;
}
新建一个前端控制器
import com.pigs.springbooteasypoipigs.entity.UserEntity;
import com.pigs.springbooteasypoipigs.utils.EasyPoiUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author PIGS
* @version 1.0
* @date 2020/4/25 14:22
* @effect :
* Excel 前端控制器
*/
@RestController
public class ExcelController {
/**
* 从表格插入数据
* 接收并返回前台
*
* @param file
* @return
* @throws IOException
*/
@RequestMapping("/uploadExcel")
public Map uploadExcel(@RequestParam("file") MultipartFile file) throws IOException {
List checkingIns = EasyPoiUtils.importExcel(file, UserEntity.class);
Map map = new HashMap<>();
map.put("code",200);
map.put("msg","ok");
map.put("data",checkingIns);
return map;
}
}
新建一个html页面
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传exceltitle>
head>
<body>
<h1>上传excel文件h1>
<form action="/uploadExcel" method="post" enctype="multipart/form-data">
<p>文件上传p>
<input type="file" name="file">
<p><input type="submit" value="提交">p>
form>
body>
html>
自制几条数据
这个字段名跟实体类 @Excel(name = "用户ID", width = 15) 需要一致不然会接收不到参数的
最后提交就完事了
demo 已经上传了 gitee了需要就去看吧
https://gitee.com/pig_farmer_x/springboot-easypoi-pigs