【一行代码搞定execl导入】springboot集成easypoi

springboot集成easypoi,execl文件导入,springboot execl导入导出

1.首先导入gradle扩展

compile group: 'cn.afterturn', name: 'easypoi-base', version: '3.0.3'
compile group: 'cn.afterturn', name: 'easypoi-web', version: '3.0.3'
compile group: 'cn.afterturn', name: 'easypoi-annotation', version: '3.0.3'

2.新建execlutil 工具类

public class ExcelUtil  {


public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass){
if (StringUtils.isBlank(filePath)){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
}catch (NoSuchElementException e){
throw new AppException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
throw new AppException(e.getMessage());
}
return list;
}

public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
if (file == null){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}catch (NoSuchElementException e){
throw new AppException("excel文件不能为空");
} catch (Exception e) {
throw new AppException(e.getMessage());
}
return list;
}
}

3.controller 调用


@PostMapping("/import/execl")
@ResponseBody
public JsonResponse importFile(@IngoreParam(value = true) @RequestParam("execlFile") MultipartFile execlFile, Model model){
List answerExeclDtoList = ExcelUtil.importExcel(execlFile,0,1,AnswerExeclDto.class);
answerService.importAnswerData(answerExeclDtoList);
logger.info("\n 本次导入题库一共【"+answerExeclDtoList.size()+"】条");
return WebUtil.successJsonResponse("导入成功");
}



4,实体类疯转:@Execl 这个注解是关键。

public class AnswerExeclDto implements Serializable {

@Excel(name = "题目大类",orderNum ="0")
private String parentCate;

@Excel(name = "二级分类",orderNum = "1")
private String childCate;

@Excel(name = "难度系数",orderNum = "2")
private String easyLevel;

@Excel(name = "题目类型",orderNum = "3")
private String answerType;

@Excel(name = "提问内容",orderNum = "4")
private String title;

@Excel(name = "标准答案",orderNum = "5")
private String trueAnswer;


/**答案A**/
@Excel(name = "选项A",orderNum = "6")
private String answerA;

/**答案B**/
@Excel(name = "选项B",orderNum = "7")
private String answerB;

/**答案C**/
@Excel(name = "选项C",orderNum = "8")
private String answerC;

/**答案D**/
@Excel(name = "选项D",orderNum = "9")
private String answerD;

/**答案E**/
@Excel(name = "选项E",orderNum = "10")
private String answerE;

/**答案F**/
@Excel(name = "选项F",orderNum = "11")
private String answerF;

public AnswerExeclDto() {
}

public AnswerExeclDto(String parentCate, String childCate, String easyLevel, String answerType, String title, String trueAnswer, String answerA, String answerB, String answerC, String answerD, String answerE, String answerF) {
this.parentCate = parentCate;
this.childCate = childCate;
this.easyLevel = easyLevel;
this.answerType = answerType;
this.title = title;
this.trueAnswer = trueAnswer;
this.answerA = answerA;
this.answerB = answerB;
this.answerC = answerC;
this.answerD = answerD;
this.answerE = answerE;
this.answerF = answerF;
}

public String getParentCate() {
return parentCate;
}

public void setParentCate(String parentCate) {
this.parentCate = parentCate;
}

public String getChildCate() {
return childCate;
}

public void setChildCate(String childCate) {
this.childCate = childCate;
}

public String getEasyLevel() {
return easyLevel;
}

public void setEasyLevel(String easyLevel) {
this.easyLevel = easyLevel;
}

public String getAnswerType() {
return answerType;
}

public void setAnswerType(String answerType) {
this.answerType = answerType;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getTrueAnswer() {
return trueAnswer;
}

public void setTrueAnswer(String trueAnswer) {
this.trueAnswer = trueAnswer;
}

public String getAnswerA() {
return answerA;
}

public void setAnswerA(String answerA) {
this.answerA = answerA;
}

public String getAnswerB() {
return answerB;
}

public void setAnswerB(String answerB) {
this.answerB = answerB;
}

public String getAnswerC() {
return answerC;
}

public void setAnswerC(String answerC) {
this.answerC = answerC;
}

public String getAnswerD() {
return answerD;
}

public void setAnswerD(String answerD) {
this.answerD = answerD;
}

public String getAnswerE() {
return answerE;
}

public void setAnswerE(String answerE) {
this.answerE = answerE;
}

public String getAnswerF() {
return answerF;
}

public void setAnswerF(String answerF) {
this.answerF = answerF;
}
}

至此!大功告成。

  • 发表于 2018-11-27 16:58
  • 阅读 ( 768 )
  • 分类:Java

0 条评论

请先 登录 后评论
不写代码的码农
威猛的小站长

105 篇文章

作家榜 »

  1. 威猛的小站长 105 文章
  2. Jonny 65 文章
  3. 江南烟雨 33 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 20 文章
  8. Alan 14 文章