網站首頁 編程語言 正文
日常開發中,難免遇到需要導入導出的業務場景,如果需要再表格的尾部插入一些自定義的統計項,傳統的excel工具并不能提供很好的支持。
今天在這里給大家推薦一款非常好用的 Excel 導入導出工具工具: zouzhiy-excel 。可以實現自定義的表尾導出!
zouzhiy-excel 簡介
zouzhiy-excel 是一款 Excel 導入導出的輕量級工具。對 POI 的接口做了一層封裝,使導入導出更加簡便快捷。
Spring 環境下集成
<dependency>
<groupId>io.github.zouzhiy</groupId>
<artifactId>zouzhiy-excel-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
開始使用
- 首先創建一對多的數據對象
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ExcelClass(rowFootWrite = CustomRowFootWrite.class)
public class FootVO {
@ExcelField(title = "姓名")
private String username;
@ExcelField(title = "年齡")
private Integer age;
@ExcelField(title = "分數")
private BigDecimal score;
}
rowFootWrite 指定自定義表尾寫入的實現
- 實現我們自定義的表尾寫入邏輯
@Component
public class CustomRowFootWrite implements RowFootWrite {
@Override
public int write(SheetContext sheetContext, List<?> dataList) {
BigDecimal totalScore = BigDecimal.ZERO;
for (Object item : dataList){
FootVO footVO = (FootVO) item;
BigDecimal score= footVO.getScore();
if (score != null){
totalScore = totalScore.add(score);
}
}
BigDecimal averageScore = totalScore.divide(new BigDecimal(dataList.size()), RoundingMode.HALF_DOWN);
Sheet sheet = sheetContext.getSheet();
int rowNum = sheet.getLastRowNum();
int totalRowNum = rowNum +1;
int averageRowNum = rowNum + 2;
Row totalRow = sheet.createRow(totalRowNum);
Cell totalCellHead = totalRow.createCell(1);
totalCellHead.setCellValue("合計");
Cell totalCellValue = totalRow.createCell(2);
totalCellValue.setCellValue(totalScore.doubleValue());
Row averageRow = sheet.createRow(averageRowNum);
Cell averageCellHead = averageRow.createCell(1);
averageCellHead.setCellValue("平均值");
Cell averageCellValue = averageRow.createCell(2);
averageCellValue.setCellValue(averageScore.doubleValue());
return 2;
}
}
- 接下來我們在 Controller 中添加一個接口,用于導出列表Excel,具體代碼如下:
@RestController
@RequestMapping("foot")
public class FootExportContrller {
@Resource
private ZouzhiyExcelFactory zouzhiyExcelFactory;
@GetMapping("list/export")
public void exportUserList(HttpServletResponse response) {
List<FootVO> voList = this.listVo();
response.addHeader("Content-Disposition"
, "attachment; filename*=utf-8''" + URLEncoder.encode("列表導出(表尾).xlsx", StandardCharsets.UTF_8.name()));
zouzhiyExcelFactory
.write(response.getOutputStream())
.sheet()
.title("列表導出(表尾)")
.titleRowStartIndex(0)
.dataRowStartIndex(2)
.write(voList, FootVO.class);
}
private List<FootVO> listVo() {
Random random = new Random(System.currentTimeMillis());
List<FootVO> voList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
FootVO vo = FootVO.builder()
.username("姓名-" + i)
.age(random.nextInt(30))
.score(BigDecimal.valueOf(random.nextDouble()))
.build();
voList.add(vo);
}
return voList;
}
}
- 通過瀏覽器訪問: http://localhost:8080/foot/list/export。下載附件。導出結果如下:
復雜的功能通過簡單的幾行代碼就實現了。
原文鏈接:https://blog.csdn.net/a516048500/article/details/125857172
相關推薦
- 2023-04-07 Android開發基礎簡化Toast調用方法詳解_Android
- 2023-08-13 Fastadmin后臺頁面添加頂部按鈕
- 2022-04-05 vxe-table中vxe-grid的使用
- 2023-07-02 Python?中的裝飾器實現函數的緩存(場景分析)_python
- 2022-03-21 C++名稱空間介紹_C 語言
- 2022-11-17 Android顯式Intent與隱式Intent的使用詳解_Android
- 2024-02-28 UNI-APP,實現給字符串中部分字符、子串添加樣式(顏色,字號等)
- 2022-08-18 Redis7.0部署集群的實現步驟_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支