首先在項(xiàng)目的resources下面建一個(gè)template包,之后在下面創(chuàng)建一個(gè)模版,模版格式如下:
名稱為 financeReportBillStandardTemplateExcel.xlsx:
{.fee}類型的屬性值,是下面實(shí)體類的屬性,要注意這里面的格式,不能錯(cuò),還需要注意就是駝峰,例如:{.stockMv},要跟實(shí)體類的屬性名保持一致,否則在導(dǎo)入的時(shí)候就會(huì)出現(xiàn)null之類的問題。
要是需要給屬性設(shè)置格式之類的,例如:我們這里設(shè)置千分符并且保留兩位小數(shù),可進(jìn)行如下圖所示的操作,先選中所要設(shè)置的單元格,然后右鍵點(diǎn)擊,選中設(shè)置單元格格式,就會(huì)彈出下圖的操作,可以進(jìn)行配置格式
接下來是實(shí)體類
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;
//資產(chǎn)市值
private BigDecimal assetMv;
//非上市股票市值
private BigDecimal unlistedMv;
//當(dāng)日國債逆回購發(fā)生額
private BigDecimal debtReverseRepurchase;
//出入金凈額
private BigDecimal netCash;
//其他資金變動(dòng)
private BigDecimal otherCash;
//總資金變動(dòng)
private BigDecimal totalCashInout;
//交易費(fèi)用
private BigDecimal fee;
//利息收入
private BigDecimal interestIncome;
//權(quán)利金收支
private BigDecimal netPremium;
//執(zhí)行實(shí)收資金
private BigDecimal realReceiveCash;
//執(zhí)行實(shí)付資金
private BigDecimal realPaymentCash;
//現(xiàn)金替代實(shí)收資金
private BigDecimal realOffsetCashIn;
//現(xiàn)金替代實(shí)付資金
private BigDecimal realOffsetCashOut;
//執(zhí)行凍結(jié)資金
private BigDecimal frozenCash;
//市值權(quán)益
private BigDecimal totalMv;
//股票市值
private BigDecimal stockMv;
//買券金額
private BigDecimal buySecuritiesAmount;
//做市商交易經(jīng)手費(fèi)優(yōu)惠
private BigDecimal marketDiscount;
//累計(jì)平倉盈虧
private BigDecimal closePnl;
//累計(jì)浮動(dòng)盈虧
private BigDecimal floatPnl;
//其他交易費(fèi)用
private BigDecimal otherFee;
}
這里只展示部分字段。文章來源:http://www.zghlxwxcb.cn/news/detail-731380.html
?接下來就直接在controller層中進(jìn)行導(dǎo)出文章來源地址http://www.zghlxwxcb.cn/news/detail-731380.html
@ApiOperation(value = "交易賬戶基礎(chǔ)數(shù)據(jù)查詢表")
@PostMapping("/exportBill")
public void exportBill(@RequestBody @Validated TraAccBillReq req ,HttpServletResponse response) throws Exception {
response.setContentType("application/octet-stream");
String fileName = URLEncoder.encode("交易賬戶基礎(chǔ)數(shù)據(jù)查詢表" + DateUtils.formatStr(req.getTradingDay()), "utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//查詢數(shù)據(jù)庫的數(shù)據(jù)
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("未查詢到賬單信息");
}
}
到了這里,關(guān)于java用easyexcel按模版導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!