Java easypoi导出excel

1 准备模板
{{$fe: list itemName }},$fe是遍历,list为数据集合,t代表每个子项(默认t,可以不写)。
Java easypoi导出excel_第1张图片

2 引入依赖

<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-lang3artifactId>
    <version>3.8.1version>
dependency>

<dependency>
    <groupId>cn.afterturngroupId>
    <artifactId>easypoi-webartifactId>
    <version>3.2.0version>
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/

你可能感兴趣的:(java,excel,easypoi)