Java中的Excel框架使用詳解
一、安裝依賴
推薦使用最新版本,可通過(guò)文章末尾官方文檔鏈接跳轉(zhuǎn)查看
<dependency>
<groupId>cn.gjing</groupId>
<artifactId>tools-excel</artifactId>
<version>2021.12.3</version>
</dependency>
二、Excel導(dǎo)出
1、單表頭
定義Excel映射實(shí)體, @Data是lombok的注解
/**
* @author Gjing
**/
@Data
@Excel("單級(jí)表頭")
public class SingleHead {
@ExcelField("姓名")
private String userName;
@ExcelField(value = "年齡", format = "0")
private Integer userAge;
@ExcelField("性別")
private Gender gender;
@ExcelField("愛(ài)好")
private String favorite;
}
/**
* @author Gjing
**/
@RestController
public class UserController {
@GetMapping("/test_export")
@ApiOperation("導(dǎo)出一級(jí)表頭")
public void testExport(HttpServletResponse response) {
//指定映射的實(shí)體為剛剛定義的
ExcelFactory.createWriter(SingleHead.class, response)
.write(null)
.flush();
}
}

2、多級(jí)表頭
數(shù)組中的每個(gè)值代表著一級(jí)表頭
/**
* @author Gjing
**/
@Data
@Excel("多級(jí)表頭")
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("多級(jí)表頭")
public void testExport(HttpServletResponse response) {
ExcelFactory.createWriter(MultiHead.class, response)
//需要在write前激活多級(jí)表頭,否則不會(huì)自動(dòng)合并
.multiHead(true)
.write(null)
.flush();
}
}

3、帶大標(biāo)題
大標(biāo)題的起始行是你要插入的sheet中最后一條數(shù)據(jù)的下一行,如果sheet中沒(méi)有數(shù)據(jù),就是第一行。你可以配置大標(biāo)題占用的行數(shù)和起始單元格下標(biāo)(默認(rèn)第一個(gè)單元格)和結(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("愛(ài)好")
private String favorite;
}
/**
* @author Gjing
**/
@RestController
public class TestController {
@GetMapping("/test_export")
@ApiOperation("帶下拉框")
public void testExport(HttpServletResponse response) {
ExcelFactory.createWriter(SingleHead.class, response)
//需要在write前激活校驗(yàn)
.valid(true)
.write(null)
.flush();
}
}
- 通過(guò)方法設(shè)置普通下拉框的選項(xiàng)
/**
* @author Gjing
**/
@RestController
public class TestController {
@GetMapping("/test_export")
@ApiOperation("帶下拉框")
public void testExport(HttpServletResponse response) {
Map<String, String[]> genderMap = new HashMap<>(8);
//key為實(shí)體類的字段名,使用方法進(jìn)行設(shè)置時(shí),實(shí)體字段的@ExcelDropdownBox注解不在需要指定combobox
//如果指定了也會(huì)去覆蓋注解中的值
genderMap.put("gender", new String[]{"男", "女"});
ExcelFactory.createWriter(SingleHead.class, response)
.valid(true)
.write(null, genderMap)
.flush();
}
}

5、時(shí)間校驗(yàn)
導(dǎo)出時(shí)給列表頭下方的單元格增加時(shí)間校驗(yàn)
/**
* @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 = "出生日期不能超過(guò)2000年")
private Date birthday;
}
/**
* @author Gjing
**/
@RestController
public class TestController {
@GetMapping("/test_export")
@ApiOperation("帶時(shí)間校驗(yàn)")
public void testExport(HttpServletResponse response) {
ExcelFactory.createWriter(SingleHead.class, response)
//需要在write前激活校驗(yàn)
.valid(true)
.write(null)
.flush();
}
}

6、數(shù)字、文本校驗(yàn)
導(dǎo)出時(shí)給列表頭下方的單元格增加數(shù)值校驗(yàn)??梢詫?duì)數(shù)字的大小,文本的長(zhǎng)度進(jìn)行校驗(yàn)
/**
* @author Gjing
**/
@Data
@Excel
public class SingleHead {
@ExcelField("姓名")
//對(duì)輸入的名稱字?jǐn)?shù)進(jìn)行校驗(yàn),字?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ù)值校驗(yàn)")
public void testExport(HttpServletResponse response) {
ExcelFactory.createWriter(SingleHead .class, response)
//需要在write前激活校驗(yàn)
.valid(true)
.write(null)
.flush();
}
}

7、數(shù)據(jù)轉(zhuǎn)換
導(dǎo)出時(shí)對(duì)數(shù)據(jù)進(jìn)行加工或者添加默認(rèn)值,支持注解方式和接口方式
- 注解方式
/**
* @author Gjing
**/
@Data
@Excel
public class SingleHead {
@ExcelField("姓名")
private String userName;
@ExcelField(value = "年齡", format = "0")
//對(duì)每個(gè)人的年齡乘以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;
}
}
實(shí)現(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()方法進(jìn)行數(shù)據(jù)刷新到Excel文件中
三、導(dǎo)入
導(dǎo)入的實(shí)體類皆采用導(dǎo)出的實(shí)體類
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()方法前通過(guò)訂閱方法增加一個(gè)結(jié)果監(jiān)聽(tīng)器,該監(jiān)聽(tīng)器會(huì)在每一次read()結(jié)束之后觸發(fā)
.subscribe(e -> this.userService.saveUsers(e))
.read()
.finish();
}
}
2、多級(jí)表頭
前文有提到多級(jí)表頭時(shí),最后一級(jí)為實(shí)際表頭,所以要在導(dǎo)入時(shí)指定實(shí)際表頭開(kāi)始下標(biāo),由于導(dǎo)出的模板映射實(shí)體設(shè)置兩級(jí)表頭,因此這里的實(shí)際表頭為下標(biāo)為1(Excel行和列下標(biāo)都是默認(rèn)0開(kāi)始的)
/**
* @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()方法前通過(guò)訂閱方法增加一個(gè)結(jié)果監(jiān)聽(tīng)器,該監(jiān)聽(tīng)器會(huì)在每一次read()結(jié)束之后觸發(fā)
//如果Excel中數(shù)據(jù)量太大,不建議使用結(jié)果監(jiān)聽(tīng)器,會(huì)造成生成了過(guò)多的映射實(shí)體對(duì)象造成內(nèi)存溢出
.subscribe(e -> this.userService.saveUsers(e))
.read(1)
.finish();
}
}
在導(dǎo)入調(diào)用結(jié)束后,一定要在最后調(diào)用finish()方法對(duì)流進(jìn)行關(guān)閉
到此這篇關(guān)于Java中的Excel框架使用詳解的文章就介紹到這了,更多相關(guān)Java中的Excel框架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于jar包與war包的區(qū)別及說(shuō)明
這篇文章主要介紹了關(guān)于jar包與war包的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Java?jar打包成exe應(yīng)用程序的詳細(xì)步驟
本文主要介紹了Java?jar打包成exe應(yīng)用程序的詳細(xì)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
spring中前端明明傳了值后端卻接收不到問(wèn)題解決辦法
在學(xué)習(xí)Spring的時(shí)候遇到了一個(gè)問(wèn)題,后臺(tái)一直接收不到前臺(tái)傳遞過(guò)來(lái)的參數(shù),耽誤了好長(zhǎng)時(shí)間終于找到了原因,這篇文章主要給大家介紹了關(guān)于spring中前端明明傳了值后端卻接收不到問(wèn)題的解決辦法,需要的朋友可以參考下2024-05-05
SpringEvents與異步事件驅(qū)動(dòng)案例詳解
本文深入探討了SpringBoot中的事件驅(qū)動(dòng)架構(gòu),特別是通過(guò)Spring事件機(jī)制實(shí)現(xiàn)組件解耦和系統(tǒng)擴(kuò)展性增強(qiáng),介紹了事件的發(fā)布者、事件本身、事件監(jiān)聽(tīng)器和事件處理器的概念,感興趣的朋友跟隨小編一起看看吧2024-09-09
詳解Spring Boot的GenericApplicationContext使用教程
這篇教程展示了如何在Spring應(yīng)用程序中使用GenericApplicationContext 。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Java 實(shí)現(xiàn)將List平均分成若干個(gè)集合
這篇文章主要介紹了Java 實(shí)現(xiàn)將List平均分成若干個(gè)集合,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
使用java寫(xiě)的矩陣乘法實(shí)例(Strassen算法)
這篇文章主要給大家介紹了關(guān)于如何使用java寫(xiě)的矩陣乘法(Strassen算法)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
java程序員自己的圖片轉(zhuǎn)文字OCR識(shí)圖工具分享
這篇文章主要介紹了java程序員自己的圖片轉(zhuǎn)文字OCR識(shí)圖工具,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

