日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Spring利用zouzhiy-excel實現自定義表尾導出

作者:zouzhiy 更新時間: 2022-07-20 編程語言
日常開發中,難免遇到需要導入導出的業務場景,如果需要再表格的尾部插入一些自定義的統計項,傳統的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

欄目分類
最近更新