Commit b6b4e06d authored by yanzg's avatar yanzg

EXCEL自定义列处理

parent b53bbf1a
...@@ -28,6 +28,7 @@ public class StringHelper { ...@@ -28,6 +28,7 @@ public class StringHelper {
public static final String TYPE_DOUBLE = "double"; public static final String TYPE_DOUBLE = "double";
public static final String TYPE_INT = "int"; public static final String TYPE_INT = "int";
public static final String TYPE_LONG = "long"; public static final String TYPE_LONG = "long";
public static final String TYPE_SHORT = "short";
public static final String TYPE_BOOL = "boolean"; public static final String TYPE_BOOL = "boolean";
public static final String TYPE_OBJECT = "System.Object"; public static final String TYPE_OBJECT = "System.Object";
public static final String UNDER_FLAG = "_"; public static final String UNDER_FLAG = "_";
...@@ -410,6 +411,8 @@ public class StringHelper { ...@@ -410,6 +411,8 @@ public class StringHelper {
to = toBoolean(strValue); to = toBoolean(strValue);
} else if (isBaseType(TYPE_INT, Integer.class, fromCls, from)) { } else if (isBaseType(TYPE_INT, Integer.class, fromCls, from)) {
to = toInt(from); to = toInt(from);
} else if (isBaseType(TYPE_SHORT, Short.class, fromCls, from)) {
to = toShort(from);
} else if (isBaseType(TYPE_LONG, Long.class, fromCls, from)) { } else if (isBaseType(TYPE_LONG, Long.class, fromCls, from)) {
to = toLong(from); to = toLong(from);
} else if (isBaseType(TYPE_DOUBLE, Double.class, fromCls, from)) { } else if (isBaseType(TYPE_DOUBLE, Double.class, fromCls, from)) {
...@@ -465,6 +468,45 @@ public class StringHelper { ...@@ -465,6 +468,45 @@ public class StringHelper {
return isType(fromCls, cls); return isType(fromCls, cls);
} }
/**
* 是否是浮点数
*
* @param obj 对象
* @return 是否是浮点数
*/
public static boolean isInt(Object obj) {
Class<?> fromCls = obj != null ? obj.getClass() : Object.class;
boolean isInt = StringHelper.isBaseType(StringHelper.TYPE_INT, Integer.class, fromCls, obj);
boolean isShort = StringHelper.isBaseType(StringHelper.TYPE_SHORT, Short.class, fromCls, obj);
boolean isLong = StringHelper.isBaseType(StringHelper.TYPE_LONG, Long.class, fromCls, obj);
return isInt || isShort || isLong;
}
/**
* 是否是浮点数
*
* @param obj 对象
* @return 是否是浮点数
*/
public static boolean isDouble(Object obj) {
Class<?> fromCls = obj != null ? obj.getClass() : Object.class;
boolean isDouble = StringHelper.isBaseType(StringHelper.TYPE_DOUBLE, Double.class, fromCls, obj);
boolean isFloat = StringHelper.isBaseType(StringHelper.TYPE_FLOAT, Float.class, fromCls, obj);
return obj instanceof BigDecimal || isDouble || isFloat;
}
/**
* 是否是浮点数
*
* @param obj 对象
* @return 是否是浮点数
*/
public static boolean isNumber(Object obj) {
return isInt(obj) || isDouble(obj);
}
/** /**
* 将 object 转换为String * 将 object 转换为String
* *
...@@ -472,11 +514,7 @@ public class StringHelper { ...@@ -472,11 +514,7 @@ public class StringHelper {
* @return 转换成功的字符串 * @return 转换成功的字符串
*/ */
public static String toString(Object obj) { public static String toString(Object obj) {
Class<?> fromCls = obj != null ? obj.getClass() : Object.class; if (isDouble(obj)) {
boolean isDouble = StringHelper.isBaseType(StringHelper.TYPE_DOUBLE, Double.class, fromCls, obj);
boolean isFromDouble = obj instanceof BigDecimal || isDouble;
if (isFromDouble) {
// 格式化设置(显示所有整数部分,小数点保留2位) // 格式化设置(显示所有整数部分,小数点保留2位)
DecimalFormat decimalFormat = new DecimalFormat("#.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); DecimalFormat decimalFormat = new DecimalFormat("#.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
String to = decimalFormat.format(obj); String to = decimalFormat.format(obj);
......
...@@ -296,7 +296,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -296,7 +296,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
sheetRow.setHeight(getUnit(row.getHeight())); sheetRow.setHeight(getUnit(row.getHeight()));
for (ExcelDefineCell cell : row.getCells()) { for (ExcelDefineCell cell : row.getCells()) {
cell.cellStyle = createCellStyle(cell); cell.cellStyle = createCellStyle(cell);
cell.cell = createCell(sheetRow, cell.getColumn(), cell.getValue(), cell.cellStyle); Object value = cell.getValue();
if (value != null) {
cell.cell = createCell(sheetRow, cell.getColumn(), StringHelper.isNumber(value),
StringHelper.toString(value), cell.cellStyle);
}
// 合并单元格 // 合并单元格
if (cell.getWidthSize() > 1 || cell.getHeightSize() > 1) { if (cell.getWidthSize() > 1 || cell.getHeightSize() > 1) {
int rowStart = rowIndex; int rowStart = rowIndex;
...@@ -382,7 +386,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -382,7 +386,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 写入本行内容 // 写入本行内容
for (ExportColumn column : this.config.getColumns()) { for (ExportColumn column : this.config.getColumns()) {
String columnName = column.getName(); String columnName = column.getName();
String value = StringHelper.getFirst(rowHandle.get(t, columnName)); Object columnValue = rowHandle.get(t, columnName);
String value = StringHelper.getFirst(String.valueOf(columnValue));
// 合并組数据处理 // 合并組数据处理
if (column.isMerger()) { if (column.isMerger()) {
sb.append(value.replace(":", "::")); sb.append(value.replace(":", "::"));
...@@ -400,7 +405,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -400,7 +405,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 当前行行高 // 当前行行高
height = Math.max(nowCellHeight, height); height = Math.max(nowCellHeight, height);
// 当不需要合并历史记录时,则创建新的内容 // 当不需要合并历史记录时,则创建新的内容
createCell(row, column.columnIndex, value, column.cellStyle); createCell(row, column.columnIndex, StringHelper.isNumber(columnValue), value, column.cellStyle);
// 合并列 // 合并列
mergerColumn(column, column.columnIndex, false); mergerColumn(column, column.columnIndex, false);
} }
...@@ -563,9 +568,27 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -563,9 +568,27 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
* @return 单元格 * @return 单元格
*/ */
private Cell createCell(Row row, int column, String content, CellStyle cellStyle) { private Cell createCell(Row row, int column, String content, CellStyle cellStyle) {
return createCell(row, column, false, content, cellStyle);
}
/**
* 创建单元格
*
* @param row 行
* @param column 列
* @param isNumber 是否是数字
* @param content 内容
* @param cellStyle 设置默认样式
* @return 单元格
*/
private Cell createCell(Row row, int column, boolean isNumber, String content, CellStyle cellStyle) {
// 获取字节数、用于设置最大宽度 // 获取字节数、用于设置最大宽度
Cell cell = row.createCell(column); Cell cell = row.createCell(column);
cell.setCellValue(content); if (isNumber) {
cell.setCellValue(StringHelper.toDouble(content));
} else {
cell.setCellValue(content);
}
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
return cell; return cell;
} }
......
...@@ -39,7 +39,7 @@ public class ExcelDefineCell { ...@@ -39,7 +39,7 @@ public class ExcelDefineCell {
* 文字内容 * 文字内容
*/ */
@ApiModelProperty(notes = "文字内容") @ApiModelProperty(notes = "文字内容")
private String value; private Object value;
/** /**
* 公式,如:SUM(C2:C3),或者SUM({columnName}),公式优先.不能SUM(columnA/columnB)。 * 公式,如:SUM(C2:C3),或者SUM({columnName}),公式优先.不能SUM(columnA/columnB)。
* 数据列={columnName_COL},指的是数据列名称对应转换的Excel列名, * 数据列={columnName_COL},指的是数据列名称对应转换的Excel列名,
...@@ -104,11 +104,11 @@ public class ExcelDefineCell { ...@@ -104,11 +104,11 @@ public class ExcelDefineCell {
this.verticalAlignment = verticalAlignment; this.verticalAlignment = verticalAlignment;
} }
public String getValue() { public Object getValue() {
return value; return value;
} }
public void setValue(String value) { public void setValue(Object value) {
this.value = value; this.value = value;
} }
......
...@@ -2,6 +2,7 @@ package com.yanzuoguang.excel; ...@@ -2,6 +2,7 @@ package com.yanzuoguang.excel;
/** /**
* 获取行中的某列数据 * 获取行中的某列数据
*
* @param <T> * @param <T>
* @author 颜佐光 * @author 颜佐光
*/ */
...@@ -14,5 +15,5 @@ public interface ExcelRow<T> { ...@@ -14,5 +15,5 @@ public interface ExcelRow<T> {
* @param field  列 * @param field  列
* @return * @return
*/ */
String get(T row, String field); Object get(T row, String field);
} }
...@@ -5,6 +5,7 @@ import com.yanzuoguang.util.base.ObjectHelper; ...@@ -5,6 +5,7 @@ import com.yanzuoguang.util.base.ObjectHelper;
/** /**
* 行数据默认处理 * 行数据默认处理
*
* @author 颜佐光 * @author 颜佐光
*/ */
public class ExcelRowDefault<T> implements ExcelRow<T> { public class ExcelRowDefault<T> implements ExcelRow<T> {
...@@ -14,10 +15,10 @@ public class ExcelRowDefault<T> implements ExcelRow<T> { ...@@ -14,10 +15,10 @@ public class ExcelRowDefault<T> implements ExcelRow<T> {
* *
* @param row  行 * @param row  行
* @param field  列 * @param field  列
* @return * @return 列值
*/ */
@Override @Override
public String get(T row, String field) { public Object get(T row, String field) {
return ObjectHelper.getString(row, field); return ObjectHelper.get(row, field);
} }
} }
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