Java中的Excel框架使用詳解
一、安裝依賴
推薦使用最新版本,可通過文章末尾官方文檔鏈接跳轉(zhuǎn)查看
<dependency> <groupId>cn.gjing</groupId> <artifactId>tools-excel</artifactId> <version>2021.12.3</version> </dependency>
二、Excel導(dǎo)出
1、單表頭
定義Excel映射實體, @Data是lombok的注解
/** * @author Gjing **/ @Data @Excel("單級表頭") public class SingleHead { @ExcelField("姓名") private String userName; @ExcelField(value = "年齡", format = "0") private Integer userAge; @ExcelField("性別") private Gender gender; @ExcelField("愛好") private String favorite; }
/** * @author Gjing **/ @RestController public class UserController { @GetMapping("/test_export") @ApiOperation("導(dǎo)出一級表頭") public void testExport(HttpServletResponse response) { //指定映射的實體為剛剛定義的 ExcelFactory.createWriter(SingleHead.class, response) .write(null) .flush(); } }
2、多級表頭
數(shù)組中的每個值代表著一級表頭
/** * @author Gjing **/ @Data @Excel("多級表頭") public class MultiHead { @ExcelField({"用戶名","用戶名"}) private String userName; @ExcelField({"年齡","年齡"}) private Integer age; @ExcelField({"形態(tài)","身高"}) private BigDecimal height; @ExcelField({"形態(tài)","體重"}) private BigDecimal weight; }
/** * @author Gjing **/ @RestController public class TestController { @GetMapping("/test_export") @ApiOperation("多級表頭") public void testExport(HttpServletResponse response) { ExcelFactory.createWriter(MultiHead.class, response) //需要在write前激活多級表頭,否則不會自動合并 .multiHead(true) .write(null) .flush(); } }
3、帶大標(biāo)題
大標(biāo)題的起始行是你要插入的sheet中最后一條數(shù)據(jù)的下一行,如果sheet中沒有數(shù)據(jù),就是第一行。你可以配置大標(biāo)題占用的行數(shù)和起始單元格下標(biāo)(默認(rèn)第一個單元格)和結(jié)束單元格下標(biāo)(默認(rèn)跟隨表頭的數(shù)量)
/** * @author Gjing **/ @RestController public class TestController { @GetMapping("/test") @ApiOperation("含大標(biāo)題") public void testExport(HttpServletResponse response) { ExcelFactory.createWriter(SingleHead.class, response) //大標(biāo)題占用兩行 .writeTitle(new BigTitle("我是大標(biāo)題")) .write(null) .flush(); } }
4、下拉框
單元格增加下拉框
- 注解方式
/** * @author Gjing **/ @Data @Excel("下拉框?qū)С?) public class SingleHead { @ExcelField("性別") @ExcelDropdownBox(combobox = {"男", "女"}) private Gender gender; @ExcelField("愛好") private String favorite; }
/** * @author Gjing **/ @RestController public class TestController { @GetMapping("/test_export") @ApiOperation("帶下拉框") public void testExport(HttpServletResponse response) { ExcelFactory.createWriter(SingleHead.class, response) //需要在write前激活校驗 .valid(true) .write(null) .flush(); } }
- 通過方法設(shè)置普通下拉框的選項
/** * @author Gjing **/ @RestController public class TestController { @GetMapping("/test_export") @ApiOperation("帶下拉框") public void testExport(HttpServletResponse response) { Map<String, String[]> genderMap = new HashMap<>(8); //key為實體類的字段名,使用方法進行設(shè)置時,實體字段的@ExcelDropdownBox注解不在需要指定combobox //如果指定了也會去覆蓋注解中的值 genderMap.put("gender", new String[]{"男", "女"}); ExcelFactory.createWriter(SingleHead.class, response) .valid(true) .write(null, genderMap) .flush(); } }
5、時間校驗
導(dǎo)出時給列表頭下方的單元格增加時間校驗
/** * @author Gjing **/ @Data @Excel public class SingleHead { @ExcelField("姓名") private String userName; @ExcelField(value = "年齡", format = "0") private Integer userAge; @ExcelField(value = "生日", format = "yyyy-MM-dd") @ExcelDateValid(expr1 = "2000-01-01", operatorType = LESS_OR_EQUAL, errorContent = "出生日期不能超過2000年") private Date birthday; }
/** * @author Gjing **/ @RestController public class TestController { @GetMapping("/test_export") @ApiOperation("帶時間校驗") public void testExport(HttpServletResponse response) { ExcelFactory.createWriter(SingleHead.class, response) //需要在write前激活校驗 .valid(true) .write(null) .flush(); } }
6、數(shù)字、文本校驗
導(dǎo)出時給列表頭下方的單元格增加數(shù)值校驗。可以對數(shù)字的大小,文本的長度進行校驗
/** * @author Gjing **/ @Data @Excel public class SingleHead { @ExcelField("姓名") //對輸入的名稱字?jǐn)?shù)進行校驗,字?jǐn)?shù)限制小于4 @ExcelNumericValid(validType = TEXT_LENGTH,operatorType = LESS_THAN,expr1 = "4",errorContent = "姓名字?jǐn)?shù)小于4") private String userName; }
/** * @author Gjing **/ @RestController public class TestController { @GetMapping("/test_export") @ApiOperation("帶數(shù)值校驗") public void testExport(HttpServletResponse response) { ExcelFactory.createWriter(SingleHead .class, response) //需要在write前激活校驗 .valid(true) .write(null) .flush(); } }
7、數(shù)據(jù)轉(zhuǎn)換
導(dǎo)出時對數(shù)據(jù)進行加工或者添加默認(rèn)值,支持注解方式和接口方式
- 注解方式
/** * @author Gjing **/ @Data @Excel public class SingleHead { @ExcelField("姓名") private String userName; @ExcelField(value = "年齡", format = "0") //對每個人的年齡乘以10 @ExcelDataConvert(expr1 = "#userAge * 10") private Integer userAge; }
- 接口方式
/** * @author Gjing **/ public class MyDataConvert implements DataConvert<SingleHead> { @Override public Object toEntityAttribute(Object o, Field field) { return null; } @Override public Object toExcelAttribute(SingleHead singleHead, Object value, Field field) { return (int) value * 10; } }
實現(xiàn)接口后需要在你需要轉(zhuǎn)換的字段上指定轉(zhuǎn)換器
/** * @author Gjing **/ @Data @Excel public class SingleHead { @ExcelField("姓名") private String userName; @ExcelField(value = "年齡", format = "0", convert = MyDataConvert.class) private Integer userAge; }
導(dǎo)出方法調(diào)用最后一定要使用flush()方法進行數(shù)據(jù)刷新到Excel文件中
三、導(dǎo)入
導(dǎo)入的實體類皆采用導(dǎo)出的實體類
1、單表頭
/** * @author Gjing **/ @RestController public class TestController { @Resource private UserService userService; @PostMapping("/user_import") @ApiOperation("導(dǎo)入單表頭") public void userImport(MultipartFile file) throws IOException { ExcelFactory.createReader(file, SingleHead.class) //在read()方法前通過訂閱方法增加一個結(jié)果監(jiān)聽器,該監(jiān)聽器會在每一次read()結(jié)束之后觸發(fā) .subscribe(e -> this.userService.saveUsers(e)) .read() .finish(); } }
2、多級表頭
前文有提到多級表頭時,最后一級為實際表頭,所以要在導(dǎo)入時指定實際表頭開始下標(biāo),由于導(dǎo)出的模板映射實體設(shè)置兩級表頭,因此這里的實際表頭為下標(biāo)為1(Excel行和列下標(biāo)都是默認(rèn)0開始的)
/** * @author Gjing **/ @RestController public class TestController { @Resource private UserService userService; @PostMapping("/user_import") @ApiOperation("導(dǎo)入單表頭") public void userImport(MultipartFile file) throws IOException { ExcelFactory.createReader(file, SingleHead.class) //在read()方法前通過訂閱方法增加一個結(jié)果監(jiān)聽器,該監(jiān)聽器會在每一次read()結(jié)束之后觸發(fā) //如果Excel中數(shù)據(jù)量太大,不建議使用結(jié)果監(jiān)聽器,會造成生成了過多的映射實體對象造成內(nèi)存溢出 .subscribe(e -> this.userService.saveUsers(e)) .read(1) .finish(); } }
在導(dǎo)入調(diào)用結(jié)束后,一定要在最后調(diào)用finish()方法對流進行關(guān)閉
到此這篇關(guān)于Java中的Excel框架使用詳解的文章就介紹到這了,更多相關(guān)Java中的Excel框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?jar打包成exe應(yīng)用程序的詳細(xì)步驟
本文主要介紹了Java?jar打包成exe應(yīng)用程序的詳細(xì)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01詳解Spring Boot的GenericApplicationContext使用教程
這篇教程展示了如何在Spring應(yīng)用程序中使用GenericApplicationContext 。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11- 數(shù)組和二維數(shù)組感覺用王者榮耀的裝備欄來舉例解釋,應(yīng)該更易懂一些。從基礎(chǔ)開始講,后續(xù)會講到JAVA高級,中間會穿插面試題和項目實戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
java程序員自己的圖片轉(zhuǎn)文字OCR識圖工具分享
這篇文章主要介紹了java程序員自己的圖片轉(zhuǎn)文字OCR識圖工具,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11最新評論