Commit d744bb15 authored by yanzg's avatar yanzg

接口文档的支持

parent 536e9672
...@@ -268,6 +268,22 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -268,6 +268,22 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
row.setHeight(getUnit(this.config.getRowHeight())); row.setHeight(getUnit(this.config.getRowHeight()));
// 合并組数据处理
for (Map.Entry<String, List<String>> groupKvp : mergerGroup.entrySet()) {
// 将当前組生成值密钥
StringBuilder sb = new StringBuilder();
for (String columnName : groupKvp.getValue()) {
String value = StringHelper.getFirst(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);
}
// 写入本行内容 // 写入本行内容
for (int columnPos = 0; columnPos < this.config.getColumns().size(); columnPos++) { for (int columnPos = 0; columnPos < this.config.getColumns().size(); columnPos++) {
ExportColumn column = this.config.getColumns().get(columnPos); ExportColumn column = this.config.getColumns().get(columnPos);
...@@ -276,16 +292,18 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -276,16 +292,18 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 判断列是否需要合并 // 判断列是否需要合并
if (column.isMerger()) { if (column.isMerger()) {
ExcelMergerData mergerData = mergerGroupData.get(column.getMegerGroup()); ExcelMergerData mergerData = mergerGroupData.get(column.getMegerGroup());
// 判断是否需要合并历史记录 // 判断是否需要合并历史记录
if (mergerData.isMergerHistory(value)) { if (mergerData.getRowIndexHistory() != mergerData.getRowIndex() - 1) {
// 合并历史记录单元格 // 合并历史记录单元格
mergerData(mergerData, columnPos); mergerData(mergerData, columnPos);
} else {
// 当不需要合并历史记录时,则创建新的内容
Cell cell = row.createCell(columnPos);
cell.setCellValue(value);
} }
// 当不需要合并历史记录时,则创建新的内容
Cell cell = row.createCell(columnPos);
cell.setCellValue(value);
} else { } else {
// 不合并时直接写入单元格内容 // 不合并时直接写入单元格内容
Cell cell = row.createCell(columnPos); Cell cell = row.createCell(columnPos);
...@@ -293,22 +311,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -293,22 +311,6 @@ 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 = StringHelper.getFirst(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++; rowIndex++;
} }
...@@ -319,8 +321,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -319,8 +321,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
* @param columnPos 合并的列位置 * @param columnPos 合并的列位置
*/ */
private void mergerData(ExcelMergerData mergerColumn, int columnPos) { private void mergerData(ExcelMergerData mergerColumn, int columnPos) {
int rowStart = mergerColumn.getRowIndex(); int rowStart = mergerColumn.getRowIndexHistory();
int rowEnd = rowStart + mergerColumn.getRowCell() - 1; int rowEnd = rowStart + mergerColumn.getRowCellHistory() - 1;
CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos); CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos);
sheet.addMergedRegion(region); sheet.addMergedRegion(region);
} }
...@@ -333,17 +335,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -333,17 +335,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
return this; return this;
} }
// 写入最后的合并内容
for (int columnPos = 0; columnPos < this.config.getColumns().size(); columnPos++) {
ExportColumn column = this.config.getColumns().get(columnPos);
if (column.isMerger()) {
ExcelMergerData mergerData = mergerGroupData.get(column.getMegerGroup());
if (mergerData.isMergerHistory()) {
mergerData(mergerData, columnPos);
}
}
}
try { try {
OutputStream out = new FileOutputStream(this.getFileName()); OutputStream out = new FileOutputStream(this.getFileName());
try { try {
......
...@@ -4,6 +4,7 @@ import com.yanzuoguang.util.helper.StringHelper; ...@@ -4,6 +4,7 @@ import com.yanzuoguang.util.helper.StringHelper;
/** /**
* 数据合并处理 * 数据合并处理
*
* @author 颜佐光 * @author 颜佐光
*/ */
public class ExcelMergerData { public class ExcelMergerData {
...@@ -21,7 +22,17 @@ public class ExcelMergerData { ...@@ -21,7 +22,17 @@ public class ExcelMergerData {
/** /**
* 数据合并行数 * 数据合并行数
*/ */
private int rowCell; private int rowCell = 1;
/**
* 上次合并行号
*/
private int rowIndexHistory = -1;
/**
* 上次合并单元格
*/
private int rowCellHistory = 1;
/** /**
* 写入新数据 * 写入新数据
...@@ -30,43 +41,57 @@ public class ExcelMergerData { ...@@ -30,43 +41,57 @@ public class ExcelMergerData {
* @param nowCellData 新数据 * @param nowCellData 新数据
*/ */
public void updateMerger(int rowIndex, String nowCellData) { public void updateMerger(int rowIndex, String nowCellData) {
if (this.rowIndex == -1 || !StringHelper.compare(this.data, nowCellData)) { boolean isMergerSuccess = this.rowIndex == -1 || !StringHelper.compare(this.data, nowCellData);
this.rowIndex = rowIndex; if (isMergerSuccess) {
this.rowIndexHistory = this.rowIndex;
this.rowCellHistory = this.rowCell;
this.data = nowCellData; this.data = nowCellData;
this.rowIndex = rowIndex;
this.rowCell = 1; this.rowCell = 1;
} else { } else {
this.rowCell++; this.rowCell++;
} }
} }
/**
* 写入数据,并且判断是否需要创建单元格
*
* @param nowCellData 需要写入的单元格的数据
* @return
*/
public boolean isMergerHistory(String nowCellData) {
return isMergerHistory() && !StringHelper.compare(this.data, nowCellData);
}
/**
* 是否合并单元格
*
* @return 合并标记
*/
public boolean isMergerHistory() {
return rowCell > 1 && rowIndex >= 0;
}
public String getData() { public String getData() {
return data; return data;
} }
public void setData(String data) {
this.data = data;
}
public int getRowIndex() { public int getRowIndex() {
return rowIndex; return rowIndex;
} }
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}
public int getRowCell() { public int getRowCell() {
return rowCell; return rowCell;
} }
public void setRowCell(int rowCell) {
this.rowCell = rowCell;
}
public int getRowIndexHistory() {
return rowIndexHistory;
}
public void setRowIndexHistory(int rowIndexHistory) {
this.rowIndexHistory = rowIndexHistory;
}
public int getRowCellHistory() {
return rowCellHistory;
}
public void setRowCellHistory(int rowCellHistory) {
this.rowCellHistory = rowCellHistory;
}
} }
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