首先在项目的resources下面建一个template包,之后在下面创建一个模版,模版格式如下:
名称为 financeReportBillStandardTemplateExcel.xlsx:
{.fee}类型的属性值,是下面实体类的属性,要注意这里面的格式,不能错,还需要注意就是驼峰,例如:{.stockMv},要跟实体类的属性名保持一致,否则在导入的时候就会出现null之类的问题。
接下来是实体类
package com.citicsc.galaxy.finance.lq;import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.math.BigDecimal;
import java.util.Date;/*** @ClassName StandardBillFieldsDTO* @Description TODO* @Author houbing* @Date 2023/9/18 10:07*/@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StandardBillFieldsDTO {private String id;//利息收入(利息归本)private BigDecimal interest;//股息红利private BigDecimal dividend;//资金余额private BigDecimal availableCash;//资产市值private BigDecimal assetMv;//非上市股票市值private BigDecimal unlistedMv;//当日国债逆回购发生额private BigDecimal debtReverseRepurchase;//出入金净额private BigDecimal netCash;//其他资金变动private BigDecimal otherCash;//总资金变动private BigDecimal totalCashInout;//交易费用private BigDecimal fee;//利息收入private BigDecimal interestIncome;//权利金收支private BigDecimal netPremium;//执行实收资金private BigDecimal realReceiveCash;//执行实付资金private BigDecimal realPaymentCash;//现金替代实收资金private BigDecimal realOffsetCashIn;//现金替代实付资金private BigDecimal realOffsetCashOut;//执行冻结资金private BigDecimal frozenCash;//市值权益private BigDecimal totalMv;//股票市值private BigDecimal stockMv;//买券金额private BigDecimal buySecuritiesAmount;//做市商交易经手费优惠private BigDecimal marketDiscount;//累计平仓盈亏private BigDecimal closePnl;//累计浮动盈亏private BigDecimal floatPnl;//其他交易费用private BigDecimal otherFee;}
这里只展示部分字段。
接下来就直接在controller层中进行导出
@ApiOperation(value = "交易账户基础数据查询表")@PostMapping("/exportBill")public void exportBill(@RequestBody @Validated TraAccBillReq req ,HttpServletResponse response) throws Exception {response.setContentType("application/octet-stream");String fileName = URLEncoder.encode("交易账户基础数据查询表" + DateUtils.formatStr(req.getTradingDay()), "utf-8");response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");//查询数据库的数据List<StandardBillFieldsDTO> dtos = lqAppService.queryBillFieldList(req);if (CollectionUtils.isNotEmpty(dtos)) {FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();WriteSheet sheet = EasyExcel.writerSheet(0).build();ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(new ClassPathResource("template/financeReportBillStandardTemplateExcel.xlsx").getInputStream()).build();excelWriter.fill(dtos, fillConfig, sheet);excelWriter.finish();} else {throw new BizException("未查询到账单信息");}}