前言
這段時間的項目中需要導(dǎo)出動態(tài)表格。
根據(jù)所選的參數(shù)導(dǎo)出對應(yīng)的字段內(nèi)容
下圖所示選擇下面幾個tab頁就需要導(dǎo)出對應(yīng)的表頭字段
下面為具體實現(xiàn)的效果。表頭樣式可以通過EasyExcel
提供的方法自定義。
具體實現(xiàn)
主要是通過 傳入 exportItem
這個條件來決定導(dǎo)出的事項。
下附實現(xiàn)代碼
public boolean export(QueryBO queryBo) throws CustomException {
List<Integer> exportItem = queryBo.getExportItem();
List<BankAccountOverviewVO> bankAccountOverviewList =
accountOverviewList(queryBo, new PageInfoVo());
// 動態(tài)創(chuàng)建表頭
List<List<String>> headList = new ArrayList<>();
// 組裝數(shù)據(jù)
List<List<Object>> dataList = new ArrayList<>();
// 表頭合并的邏輯
List<CellRangeAddress> cellRangeAddressList = new ArrayList<>();
// 組裝賬戶信息總覽 導(dǎo)出數(shù)據(jù)
builidAccountOverviewExportData(
exportItem, bankAccountOverviewList, headList, dataList, cellRangeAddressList);
String fileName = "信息總覽.xlsx";
// 調(diào)用EasyExcel 方法輸出文件流
try {
HttpServletResponse response = ServletUtils.getResponse();
response.setContentType("application/msexcel;charset=utf-8");
response.setHeader(
"content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8"));
OutputStream out = response.getOutputStream();
EasyExcel.write(out)
.head(headList) // 設(shè)置表頭數(shù)據(jù)
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(25)) // excel 樣式設(shè)置
.autoCloseStream(Boolean.TRUE) // 自動關(guān)閉文件流
.sheet("信息總覽")
.doWrite(dataList); // 數(shù)據(jù)list
// 開始導(dǎo)出
} catch (Exception e) {
LoggerUtils.error(e.getMessage());
return false;
}
return true;
}
數(shù)據(jù)組裝方法
builidAccountOverviewExportData
主要實現(xiàn)的原理是通過List<Integer> exportItem
來判斷需要導(dǎo)出的tab塊。
其中使用了 getApiModelName(OverviewBankBookOcceVO.class);
方法映射對象的字段名進(jìn)行快捷組裝。
private void builidAccountOverviewExportData(
List<Integer> exportItem,
List<BankAccountOverviewVO> bankAccountOverviewList,
List<List<String>> headList,
List<List<Object>> dataList,
List<CellRangeAddress> cellRangeAddressList) {
// --------------表頭拼接-------------
// 賬戶信息
List<String> bankBookHead = new ArrayList<>();
bankBookHead.add("企業(yè)主體編碼");
bankBookHead.add("企業(yè)主體名稱");
for (String head : bankBookHead) {
List<String> bankBookHeadTitle = new ArrayList<>();
bankBookHeadTitle.add(head);
headList.add(bankBookHeadTitle);
}
// 字段下標(biāo)?。。。。。。。。。?/span>
int index = 13;
if (CollectionUtils.isNotEmpty(exportItem)) {
// 賬面發(fā)生額
if (exportItem.contains(BankAccountOverviewExportTypeEnum.BANK_BOOK_OCCE.getValue())) {
List<String> apiModelNameList = getApiModelName(OverviewBankBookOcceVO.class);
for (String apiModelName : apiModelNameList) {
List<String> bankBookOccHeadTitle = new ArrayList<>();
bankBookOccHeadTitle.add("賬面發(fā)生額");
bankBookOccHeadTitle.add(apiModelName);
headList.add(bankBookOccHeadTitle);
}
// // 需要合并的表頭位置 (起始行,結(jié)束行,起始列,結(jié)束列)
// int size = apiModelNameList.size();
// CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, index, index + size -
// 1);
// cellRangeAddressList.add(cellRangeAddress);
// index += size;
}
// 發(fā)生額調(diào)節(jié)表
if (exportItem.contains(BankAccountOverviewExportTypeEnum.OCC_ADJUSTMENT.getValue())) {
List<String> apiModelNameList = getApiModelName(OverviewOccAdjustmentVO.class);
for (String apiModelName : apiModelNameList) {
List<String> occAdjustmentInfoHeadTitle = new ArrayList<>();
occAdjustmentInfoHeadTitle.add("發(fā)生額調(diào)節(jié)表");
occAdjustmentInfoHeadTitle.add(apiModelName);
headList.add(occAdjustmentInfoHeadTitle);
}
}
// 網(wǎng)銀流水
if (exportItem.contains(BankAccountOverviewExportTypeEnum.EBANK_FLOW.getValue())) {
List<String> apiModelNameList = getApiModelName(OverviewEbankFlowVO.class);
for (String apiModelName : apiModelNameList) {
List<String> ebankFlowHeadTitle = new ArrayList<>();
ebankFlowHeadTitle.add("網(wǎng)銀流水");
ebankFlowHeadTitle.add(apiModelName);
headList.add(ebankFlowHeadTitle);
}
}
// 核對余額
if (exportItem.contains(BankAccountOverviewExportTypeEnum.RECONCILE_BALANCE.getValue())) {
List<String> apiModelNameList = getApiModelName(OverviewReconcileBalanceVO.class);
for (String apiModelName : apiModelNameList) {
List<String> reconcileBalancesHeadTitle = new ArrayList<>();
reconcileBalancesHeadTitle.add("核對余額");
reconcileBalancesHeadTitle.add(apiModelName);
headList.add(reconcileBalancesHeadTitle);
}
// 需要合并的表頭位置 (起始行,結(jié)束行,起始列,結(jié)束列)
int size = apiModelNameList.size();
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, index, index + size - 1);
cellRangeAddressList.add(cellRangeAddress);
index += size;
}
// 雙向核對結(jié)果
if (exportItem.contains(BankAccountOverviewExportTypeEnum.CHECK_RESULT.getValue())) {
List<String> apiModelNameList = getApiModelName(OverviewCheckResultVO.class);
for (String apiModelName : apiModelNameList) {
List<String> checkResultHeadTitle = new ArrayList<>();
checkResultHeadTitle.add("雙向核對結(jié)果");
checkResultHeadTitle.add(apiModelName);
headList.add(checkResultHeadTitle);
}
}
}
// ----------------組裝數(shù)據(jù)-------------
for (BankAccountOverviewVO bankAccountOverview : bankAccountOverviewList) {
List<Object> data = new ArrayList<>();
// 賬戶信息總覽
data.add(bankAccountOverview.getCompanyId());
data.add(bankAccountOverview.getCompanyName());
String checkPlanFlag = bankAccountOverview.getCheckPlanFlag();
if (StringUtils.isNotEmpty(checkPlanFlag)) {
checkPlanFlag = checkPlanFlag.replaceAll("0", "是");
checkPlanFlag = checkPlanFlag.replaceAll("1", "否");
}
data.add(checkPlanFlag);
data.add(bankAccountOverview.getNoCheckReason());
// -------動態(tài)表格數(shù)據(jù)組裝-----
if (CollectionUtils.isNotEmpty(exportItem)) {
// 賬面發(fā)生額
if (exportItem.contains(BankAccountOverviewExportTypeEnum.BANK_BOOK_OCCE.getValue())) {
data.add(bankAccountOverview.getBankBookOccInfo().getOpenBalance());
data.add(bankAccountOverview.getBankBookOccInfo().getCurrentIssueAdd());
data.add(bankAccountOverview.getBankBookOccInfo().getCurrentIssueReduce());
data.add(bankAccountOverview.getBankBookOccInfo().getBalance());
data.add(bankAccountOverview.getBankBookOccInfo().getOpenBalanceF());
data.add(bankAccountOverview.getBankBookOccInfo().getCurrentIssueAddF());
data.add(bankAccountOverview.getBankBookOccInfo().getCurrentIssueReduceF());
data.add(bankAccountOverview.getBankBookOccInfo().getBalanceF());
}
// 發(fā)生額調(diào)節(jié)表
if (exportItem.contains(BankAccountOverviewExportTypeEnum.OCC_ADJUSTMENT.getValue())) {
data.add(bankAccountOverview.getOccAdjustmentInfo().getCurrentIssueAdd());
data.add(bankAccountOverview.getOccAdjustmentInfo().getCurrentIssueReduce());
}
// 網(wǎng)銀流水
if (exportItem.contains(BankAccountOverviewExportTypeEnum.EBANK_FLOW.getValue())) {
data.add(bankAccountOverview.getEbankFlowInfo().getOpenBalance());
data.add(bankAccountOverview.getEbankFlowInfo().getCurrentIssueAdd());
data.add(bankAccountOverview.getEbankFlowInfo().getCurrentIssueReduce());
data.add(bankAccountOverview.getEbankFlowInfo().getBalance());
}
// 核對余額
if (exportItem.contains(BankAccountOverviewExportTypeEnum.RECONCILE_BALANCE.getValue())) {
data.add(bankAccountOverview.getReconcileBalances().getBalanceVerify());
data.add(bankAccountOverview.getReconcileBalances().getDebitOccFVerify());
data.add(bankAccountOverview.getReconcileBalances().getCreditOccFVerify());
data.add(bankAccountOverview.getReconcileBalances().getNetAmountVerify());
}
// 雙向核對結(jié)果
if (exportItem.contains(BankAccountOverviewExportTypeEnum.CHECK_RESULT.getValue())) {
data.add(bankAccountOverview.getCheckResult().getDebitOccF());
data.add(bankAccountOverview.getCheckResult().getCreditOccF());
data.add(bankAccountOverview.getCheckResult().getDebitRatio());
data.add(bankAccountOverview.getCheckResult().getCreditRatio());
data.add(bankAccountOverview.getCheckResult().getUntreatedProject());
data.add(bankAccountOverview.getCheckResult().getExceptionProject());
}
}
dataList.add(data);
}
}
getApiModelName
方法代碼如下,因為項目引入了 swagger
所以數(shù)據(jù)接口對象都會使用到 @ApiModelProperty
注解 如下圖所示。通過反射獲取實體類的對應(yīng)屬性的注解名稱list。文章來源:http://www.zghlxwxcb.cn/news/detail-479433.html
@Data
@ApiModel(value = "vo對象", description = "vo對象")
public class OverviewBankBookOcceVO {
@ApiModelProperty(value = "銀行賬號")
private String bankAccount;
@ApiModelProperty("期初余額(原幣)")
private BigDecimal openBalance;
}
/**
* @description 獲取對象中 api 注解的名稱
* @params [object]
* @return
*/
private List<String> getApiModelName(Object object) {
Class<T> aClass = (Class<T>) object;
Field[] fields = aClass.getDeclaredFields();
List<String> apiModelName = new ArrayList<>();
for (Field field : fields) {
ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
String value = annotation.value();
if (field.getName().equals("bankAccount")) {
continue;
}
apiModelName.add(value);
}
return apiModelName;
}
本文由 SoGeek_Studio 發(fā)布,有任何問題請留言評論,歡迎指正。文章來源地址http://www.zghlxwxcb.cn/news/detail-479433.html
到了這里,關(guān)于EasyExcel動態(tài)頭導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!