Java easypoi导出excel

该文章详细介绍了如何在Java后端使用Easypoi库导出Excel。首先,准备模板并定义数据结构,然后引入ApacheCommonsLang3和Easypoi的依赖。接着,展示了Controller、Service和ServiceImpl中的关键代码,包括从数据库获取数据,构建Excel表格内容,最后将工作簿写入HTTP响应流中供下载。整个过程涉及数据转换、列表处理和模板填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 准备模板
{{$fe: list itemName }},$fe是遍历,list为数据集合,t代表每个子项(默认t,可以不写)。
在这里插入图片描述

2 引入依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.2.0</version>
</dependency>

3 核心代码
3.1 controller

@PostMapping("/export")
public void export(String sheetIds ,HttpServletResponse response) throws Exception {
    sheetService.export(sheetIds, response);
}

3.2 service

void export(String sheetIds , HttpServletResponse response) throws Exception;

3.3 serviceImpl

@Override
public void export(String sheetIds , HttpServletResponse response) throws Exception {
    List<StockInSheetDTO> stockInSheetDTOS = new ArrayList<>();
    String[] sheetIdArray = sheetIds.split(",");
    for (String sheetId : sheetIdArray) {
        StockInSheetDTO stockInSheetDTO = baseMapper.getById(sheetId);
        if (stockInSheetDTO != null) {
            stockInSheetDTO.setStockInSheetDetailDTOs(stockInSheetDetailMapper.getList(sheetId));
            stockInSheetDTOS.add(stockInSheetDTO);
        }
    }

    int sequence = 0;
    BigDecimal totalAmount = new BigDecimal(0);
    List<Map<String, Object>> exportData = new ArrayList<>();
    for (StockInSheetDTO stockInSheetDTO : stockInSheetDTOS) {
        for (StockInSheetDetailDTO stockInSheetDetailDTO : stockInSheetDTO.getStockInSheetDetailDTOs()) {
            Map<String, Object> rowData = new HashMap<>();
            rowData.put("sequence", sequence = sequence + 1);
            rowData.put("itemName", stockInSheetDetailDTO.getItemName());
            rowData.put("specification", stockInSheetDetailDTO.getSpecification());
            rowData.put("unitName", stockInSheetDetailDTO.getUnitName());
            rowData.put("quantity", stockInSheetDetailDTO.getQuantity());
            rowData.put("price", stockInSheetDetailDTO.getPrice());
            rowData.put("totalPrice", stockInSheetDetailDTO.getTotalPrice());
            rowData.put("buyerName", stockInSheetDTO.getBuyerName());
            rowData.put("userName", stockInSheetDTO.getUserName());
            rowData.put("createrName", stockInSheetDTO.getCreaterName());
            rowData.put("createTime", DateUtil.formatDate("yyyy-MM-dd HH:mm:ss", stockInSheetDTO.getCreateTime()));
            rowData.put("note", "-");
            exportData.add(rowData);
            totalAmount = totalAmount.add(stockInSheetDetailDTO.getTotalPrice());
        }
    }

    Map<String, Object> exportMap = new HashMap<>();
    exportMap.put("list", exportData);
    exportMap.put("totalAmount", totalAmount);
    TemplateExportParams exportParams = new TemplateExportParams("static/poi/入库单.xlsx",true);

    Workbook workbook = ExcelExportUtil.exportExcel(exportParams, exportMap);
    ServletOutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.close();
}

参考博客
http://easypoi.mydoc.io/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值