日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用EasyPoi导入Excel

發布時間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用EasyPoi导入Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上次分享了用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 = "數學成績")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 = {"數學_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.測試數據

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注解類里有一些屬性,用來輔助導入,具體怎么用可以查看源碼注釋根據需求定義。

總結

以上是生活随笔為你收集整理的用EasyPoi导入Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。