Commit 6c1f982f authored by yanzg's avatar yanzg

EXCEL自定义列处理

parent 5ea7d103
package com.yanzuoguang.excel;
/**
* 单元格引用工具类
*
* @author 颜佐光
*/
public class CellReferenceUtil {
/**
* Input Coordination: C<br/>
* Output Column Number: 2<br/>
*
* @param colName
* @return colIndex: 0 based index
*/
public static int getColIndex(String colName) {
colName = colName.toUpperCase();
int value = 0;
for (int i = 0; i < colName.length(); i++) {
int delta = colName.charAt(i) - 64;
value = value * 26 + delta;
}
int colIndex = value - 1;
return colIndex;
}
/**
* Input column index: 2 <br/>
* Output column name: C <br/>
*
* @param colIndex
* @return
*/
public static String getColName(int colIndex) {
int quotient = (colIndex) / 26;
if (quotient > 0) {
return getColName(quotient - 1) + (char) ((colIndex % 26) + 65);
} else {
return "" + (char) ((colIndex % 26) + 65);
}
}
/**
* Input coord: C8<br/>
* Output Row Number: 7<br/>
*
* @param rowName
* @return rowIndex starts with 0
*/
public static int getRowIndex(String rowName) {
int rowIndex = Integer.parseInt(rowName) - 1;
return rowIndex;
}
/**
* Input rowIndex: 7 <br/>
* Output rowName: 8 <br/>
*
* @param rowIndex
* @return
*/
public static String getRowName(int rowIndex) {
int rowName = rowIndex + 1;
return String.valueOf(rowName);
}
/**
* Input pos: col = 2, row = 7<br/>
* Output coord: C8<br/>
*
* @param colIndex
* @param rowIndex
* @return
*/
public static String getCoordName(int colIndex, int rowIndex) {
String colName = getColName(colIndex);
String rowName = getRowName(rowIndex);
return colName + rowName;
}
/**
* Input coordName: C8 <br/>
* Output colIndex: 2 <br/>
*
* @param coordName
* @return colIndex: Starts with 0
*/
public static int getColIndexByCoordName(String coordName) {
String[] colAndRowName = splitColAndRow(coordName);
String colName = colAndRowName[0];
return getColIndex(colName);
}
/**
* Input coordName: C8 <br/>
* Output rowIndex: 7 <br/>
*
* @param coordName
* @return rowIndex: 0 based index
*/
public static int getRowIndexByCoordName(String coordName) {
String[] colAndRowName = splitColAndRow(coordName);
String rowName = colAndRowName[1];
return getRowIndex(rowName);
}
/**
* Input coordName: C8 <br/>
* Output : String[]{C, 8} <br/>
*
* @param coordName
* @return
*/
private static String[] splitColAndRow(String coordName) {
int rowNumStartIndex = 0;
for (int i = 0; i < coordName.length(); i++) {
char ch = coordName.charAt(i);
if (Character.isDigit(ch)) {
rowNumStartIndex = i;
break;
}
}
String colName = coordName.substring(0, rowNumStartIndex);
String rowName = coordName.substring(rowNumStartIndex);
return new String[]
{colName, rowName};
}
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ package com.yanzuoguang.excel; ...@@ -2,6 +2,7 @@ package com.yanzuoguang.excel;
import com.yanzuoguang.db.impl.DbRow; import com.yanzuoguang.db.impl.DbRow;
import com.yanzuoguang.util.YzgError; import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.CheckerHelper; import com.yanzuoguang.util.helper.CheckerHelper;
import com.yanzuoguang.util.helper.FileHelper; import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
...@@ -18,7 +19,9 @@ import java.io.File; ...@@ -18,7 +19,9 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
...@@ -313,12 +316,36 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -313,12 +316,36 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
} }
private void writeDefineFormula(List<ExcelDefineRow> rows) { private void writeDefineFormula(List<ExcelDefineRow> rows) {
Map<String, Object> map = new HashMap<>(this.config.getColumns().size() * 2 + 3);
// 设置列别名
int columnPos = 0;
for (ExportColumn column : this.config.getColumns()) {
map.put(column.getName(), String.format("%s:%s",
CellReferenceUtil.getCoordName(columnPos, this.startDataRowIndex),
CellReferenceUtil.getCoordName(columnPos, this.endDataRowIndex)
));
map.put(column.getName() + "_COL", CellReferenceUtil.getColName(columnPos));
}
// 数据行开始行号
map.put("startDataRowIndex", CellReferenceUtil.getRowName(this.startDataRowIndex));
// 数据行结束行号
map.put("endDataRowIndex", CellReferenceUtil.getRowName(this.endDataRowIndex));
// 设置行别名
for (ExcelDefineRow row : rows) { for (ExcelDefineRow row : rows) {
// 当前行号
map.put("rowIndex", CellReferenceUtil.getRowName(row.rowIndex));
for (ExcelDefineCell cell : row.getCells()) { for (ExcelDefineCell cell : row.getCells()) {
if (StringHelper.isEmpty(cell.getFormula())) { String formula = cell.getFormula();
if (StringHelper.isEmpty(formula)) {
continue; continue;
} }
formula = StringHelper.getFormat(formula, StringHelper.EMPTY, (sb, group, fieldFull, field, command) -> {
String value = StringHelper.getFirst(ObjectHelper.getString(map, field));
sb.append(value);
});
// 设置公式
cell.cell.setCellFormula(formula);
} }
} }
} }
......
...@@ -41,9 +41,19 @@ public class ExcelDefineCell { ...@@ -41,9 +41,19 @@ public class ExcelDefineCell {
@ApiModelProperty(notes = "文字内容") @ApiModelProperty(notes = "文字内容")
private String value; private String value;
/** /**
* 公式,如:SUM(C2:C3),或者SUM({columnName}),columnName=数据列名称,公式优先 * 公式,如:SUM(C2:C3),或者SUM({columnName}),公式优先.不能SUM(columnA/columnB)。
* 数据列={columnName_COL},指的是数据列名称对应转换的Excel列名,
* 当前行={rowIndex},
* 数据开始行={startDataRowIndex},
* 数据结束行={endDataRowIndex},
* 数据所有单元格={columnName}={columnName_COL}{startDataRowIndex}:{columnName_COL}{endDataRowIndex}
*/ */
@ApiModelProperty(notes = " 公式,如:SUM(C2:C3),或者SUM({columnName}),columnName=数据列名称,公式优先.不能SUM(columnA/columnB),当前行={rowIndex}") @ApiModelProperty(notes = " 公式,如:SUM(C2:C3),或者SUM({columnName}),公式优先.不能SUM(columnA/columnB)。" +
"数据列={columnName_COL},指的是数据列名称对应转换的Excel列名," +
"当前行={rowIndex}," +
"数据开始行={startDataRowIndex}," +
"数据结束行={endDataRowIndex}" +
"数据所有单元格={columnName}={columnName_COL}{startDataRowIndex}:{columnName_COL}{endDataRowIndex}")
private String formula; private String formula;
/** /**
* 当前单元格样式 * 当前单元格样式
......
...@@ -10,11 +10,7 @@ import java.util.List; ...@@ -10,11 +10,7 @@ import java.util.List;
* *
* @author 颜佐光 * @author 颜佐光
*/ */
public class ExcelDefineRow { public class ExcelDefineRow extends ExcelRowIndex {
/**
* 行号
*/
int rowIndex;
/** /**
* 高度 * 高度
*/ */
......
package com.yanzuoguang.excel;
/**
* 单元格
*
* @author 颜佐光
*/
public class ExcelRowIndex {
/**
* 行号
*/
int rowIndex;
}
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