用EasyPoi导入Excel
生活随笔
收集整理的這篇文章主要介紹了
用EasyPoi导入Excel
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
上次分享了用EasyPoi導出excel的代碼,這次分享一下用EasyPoi導入excel的代碼
1.設置導入模板
2.設置實體類
package com.mayi1203.myproject.entity;import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; import java.util.List;import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelCollection; import lombok.Data;/*** 學生類* @author mayi1203* @date 2020年5月8日*/ @Data public class Student implements Serializable {private static final long serialVersionUID = 2131321500629905052L;@Excel(name = "學生姓名")private String studentName;@Excel(name = "學生年齡")private Integer age;@Excel(name = "學生生日", importFormat = "yyyy/MM/dd")private Date birthday;@Excel(name = "語文成績")private BigDecimal chineseScore;@Excel(name = "數(shù)學成績")private BigDecimal mathScore;@ExcelCollection(name = "老師信息")private List<Teacher> teacherList;} package com.mayi1203.myproject.entity;import cn.afterturn.easypoi.excel.annotation.Excel; import lombok.Data;/*** 老師類* @author mayi1203* @date 2020年5月19日*/ @Data public class Teacher {@Excel(name = "老師姓名")private String name;@Excel(name = "科目", replace = {"數(shù)學_0", "英語_1"})private String subjuect; }3.測試控制層
package com.mayi1203.myproject.controller;import java.util.List;import org.springframework.web.bind.annotation.PostMapping; 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 com.mayi1203.myproject.entity.Student;import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ImportParams;@RestController @RequestMapping(value = "/v1/test") public class TestController {@PostMapping("importExcel")public void importExcel(@RequestParam("file")MultipartFile file) throws Exception {ImportParams importParas = new ImportParams();importParas.setHeadRows(3);List<Student> list = ExcelImportUtil.importExcel(file.getInputStream(), Student.class, importParas);list.stream().forEach(student -> System.out.println(student));} }4.測試數(shù)據(jù)
5.打印結果
Student(studentName=張三, age=11, birthday=Tue May 19 00:00:00 CST 2020, chineseScore=87, mathScore=88, teacherList=[Teacher(name=張三, subjuect=0), Teacher(name=里斯, subjuect=1)]) Student(studentName=王五, age=12, birthday=null, chineseScore=66, mathScore=87, teacherList=[Teacher(name=null, subjuect=null)]) Student(studentName=趙六, age=13, birthday=null, chineseScore=77, mathScore=98, teacherList=[Teacher(name=null, subjuect=null)])6.寫在最后
@Excel注解類里有一些屬性,用來輔助導入,具體怎么用可以查看源碼注釋根據(jù)需求定義。
總結
以上是生活随笔為你收集整理的用EasyPoi导入Excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++ 用eclipse建立一个类,并实
- 下一篇: 运维必知的23个经验教训,值得收藏!