Commit 9be36a77 authored by yanzg's avatar yanzg

接口文档的支持

parent 5dd34078
......@@ -3,6 +3,7 @@ package com.yanzuoguang.excel;
import com.yanzuoguang.db.impl.DbRow;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.CheckerHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.table.TableHead;
import com.yanzuoguang.util.table.TableHeadHelper;
import com.yanzuoguang.util.table.TableHeadItem;
......@@ -17,7 +18,9 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
......@@ -51,10 +54,15 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/
private int rowIndex;
/**
* 合并列表組
*/
private Map<String, List<String>> mergerGroup;
/**
* 是否需要合并单元格
*/
private Map<Integer, ExcelMergerData> mergerData;
private Map<String, ExcelMergerData> mergerGroupData;
/**
* 控制台输出Excel
......@@ -161,7 +169,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
rowIndex += head.getTotalRow();
// 创建合并对象数据检测
mergerData = new HashMap<>(head.getTotalColumn());
mergerGroup = new HashMap<>();
mergerGroupData = new HashMap<>();
}
/**
......@@ -213,6 +222,18 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
sheet.addMergedRegion(region);
}
}
// 合并数据配置
for (ExportColumn column : this.config.getColumns()) {
// 获取需要合并的組,不能为null
String group = StringHelper.getFirst(column.getMegerGroup());
if (!mergerGroup.containsKey(group)) {
mergerGroup.put(group, new ArrayList<>());
mergerGroupData.put(group, new ExcelMergerData());
}
// 当前合并組中添加需要合并的列
mergerGroup.get(group).add(column.getName());
}
}
/**
......@@ -242,6 +263,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
Row row = sheet.createRow(rowIndex);
row.setHeight(getUnit(this.config.getRowHeight()));
// 写入本行内容
for (int columnPos = 0; columnPos < this.config.getColumns().size(); columnPos++) {
ExportColumn column = this.config.getColumns().get(columnPos);
......@@ -250,22 +272,16 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 判断列是否需要合并
if (column.isMerger()) {
// 获取合并历史记录配置
if (!mergerData.containsKey(columnPos)) {
mergerData.put(columnPos, new ExcelMergerData());
}
ExcelMergerData mergerColumn = mergerData.get(columnPos);
ExcelMergerData mergerData = mergerGroupData.get(column.getMegerGroup());
// 判断是否需要合并历史记录
if (mergerColumn.isMergerHistory(value)) {
if (mergerData.isMergerHistory(value)) {
// 合并历史记录单元格
mergerData(mergerColumn, columnPos);
mergerData(mergerData, columnPos);
} else {
// 当不需要合并历史记录时,则创建新的内容
Cell cell = row.createCell(columnPos);
cell.setCellValue(value);
}
// 更新合并内容
mergerColumn.updateMerger(rowIndex, value);
} else {
// 不合并时直接写入单元格内容
Cell cell = row.createCell(columnPos);
......@@ -273,6 +289,22 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
}
}
// 合并組数据处理
for (Map.Entry<String, List<String>> groupKvp : mergerGroup.entrySet()) {
// 将当前組生成值密钥
StringBuilder sb = new StringBuilder();
for (String columnName : groupKvp.getValue()) {
String value = rowHandle.get(t, columnName);
sb.append(value.replace(":", "::"));
sb.append(":");
}
String groupValue = StringHelper.md5(sb.toString());
// 更新合并内容
ExcelMergerData mergerData = mergerGroupData.get(groupKvp.getKey());
mergerData.updateMerger(rowIndex, groupValue);
}
rowIndex++;
}
......@@ -300,10 +332,10 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 写入最后的合并内容
for (int columnPos = 0; columnPos < this.config.getColumns().size(); columnPos++) {
ExportColumn column = this.config.getColumns().get(columnPos);
if (column.isMerger() && mergerData.containsKey(columnPos)) {
ExcelMergerData mergerColumn = mergerData.get(columnPos);
if (mergerColumn.isMergerHistory()) {
mergerData(mergerColumn, columnPos);
if (column.isMerger()) {
ExcelMergerData mergerData = mergerGroupData.get(column.getMegerGroup());
if (mergerData.isMergerHistory()) {
mergerData(mergerData, columnPos);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment