Excel导出时在初始化表头时进修解析值的初始化,在创建单元格时使用HashMap直接获取值,大幅度节省时间复杂度上的开销
This commit is contained in:
parent
78e61d89ba
commit
cbce71bcdc
|
|
@ -533,7 +533,8 @@ public class ExcelUtil<T>
|
|||
for (int index = 0; index < sheetNo; index++)
|
||||
{
|
||||
createSheet(sheetNo, index);
|
||||
|
||||
//字段转换列表<列下标,<待转值,转换值>>
|
||||
Map<Integer, Map<String, String>> fieldConversionMap = new HashMap<>();
|
||||
// 产生一行
|
||||
Row row = sheet.createRow(rownum);
|
||||
int column = 0;
|
||||
|
|
@ -547,17 +548,17 @@ public class ExcelUtil<T>
|
|||
for (Field subField : subFields)
|
||||
{
|
||||
Excel subExcel = subField.getAnnotation(Excel.class);
|
||||
this.createHeadCell(subExcel, row, column++);
|
||||
this.createHeadCellAndInitFieldConversion(subExcel, row, column++,fieldConversionMap);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.createHeadCell(excel, row, column++);
|
||||
this.createHeadCellAndInitFieldConversion(excel, row, column++,fieldConversionMap);
|
||||
}
|
||||
}
|
||||
if (Type.EXPORT.equals(type))
|
||||
{
|
||||
fillExcelData(index, row);
|
||||
fillExcelData(index, row, fieldConversionMap);
|
||||
addStatisticsRow();
|
||||
}
|
||||
}
|
||||
|
|
@ -568,9 +569,10 @@ public class ExcelUtil<T>
|
|||
*
|
||||
* @param index 序号
|
||||
* @param row 单元格行
|
||||
* @param fieldConversionMap 字段转换Map
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void fillExcelData(int index, Row row)
|
||||
public void fillExcelData(int index, Row row, Map<Integer, Map<String, String>> fieldConversionMap)
|
||||
{
|
||||
int startNo = index * sheetSize;
|
||||
int endNo = Math.min(startNo + sheetSize, list.size());
|
||||
|
|
@ -618,7 +620,7 @@ public class ExcelUtil<T>
|
|||
{
|
||||
subField.setAccessible(true);
|
||||
Excel attr = subField.getAnnotation(Excel.class);
|
||||
this.addCell(attr, row, (T) obj, subField, column + subIndex);
|
||||
this.addCell(attr, row, (T) obj, subField, column + subIndex, fieldConversionMap);
|
||||
}
|
||||
subIndex++;
|
||||
}
|
||||
|
|
@ -628,7 +630,7 @@ public class ExcelUtil<T>
|
|||
}
|
||||
else
|
||||
{
|
||||
this.addCell(excel, row, vo, field, column++);
|
||||
this.addCell(excel, row, vo, field, column++, fieldConversionMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -760,9 +762,9 @@ public class ExcelUtil<T>
|
|||
}
|
||||
|
||||
/**
|
||||
* 创建单元格
|
||||
* 创建首行单元格,并且初始化字段的转换
|
||||
*/
|
||||
public Cell createHeadCell(Excel attr, Row row, int column)
|
||||
public Cell createHeadCellAndInitFieldConversion(Excel attr, Row row, int column, Map<Integer, Map<String, String>> fieldConversionMap)
|
||||
{
|
||||
// 创建列
|
||||
Cell cell = row.createCell(column);
|
||||
|
|
@ -770,6 +772,19 @@ public class ExcelUtil<T>
|
|||
cell.setCellValue(attr.name());
|
||||
setDataValidation(attr, row, column);
|
||||
cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
|
||||
//字段转换初始化(通过readConverterExp字符转换)
|
||||
if (StringUtils.isNotEmpty(attr.readConverterExp()))
|
||||
{
|
||||
HashMap<String, String> conversionMap = new HashMap<>();
|
||||
String allList = attr.readConverterExp();
|
||||
String[] split = allList.split(",");
|
||||
for (String dictTagItem : split)
|
||||
{
|
||||
String[] split1 = dictTagItem.split("=");
|
||||
conversionMap.put(split1[0], split1[1]);
|
||||
}
|
||||
fieldConversionMap.put(column, conversionMap);
|
||||
}
|
||||
if (isSubList())
|
||||
{
|
||||
// 填充默认样式,防止合并单元格样式失效
|
||||
|
|
@ -885,8 +900,15 @@ public class ExcelUtil<T>
|
|||
|
||||
/**
|
||||
* 添加单元格
|
||||
* @param attr 当前单元格表注解
|
||||
* @param row 当前单元格归属行
|
||||
* @param vo 单元格内容
|
||||
* @param field 单元格字段
|
||||
* @param column 单元格的列号
|
||||
* @param fieldConversionMap 所有单元格的解析值字典
|
||||
* @return 生成后的单元格
|
||||
*/
|
||||
public Cell addCell(Excel attr, Row row, T vo, Field field, int column)
|
||||
public Cell addCell(Excel attr, Row row, T vo, Field field, int column,Map<Integer, Map<String, String>> fieldConversionMap)
|
||||
{
|
||||
Cell cell = null;
|
||||
try
|
||||
|
|
@ -914,9 +936,9 @@ public class ExcelUtil<T>
|
|||
{
|
||||
cell.setCellValue(parseDateToStr(dateFormat, value));
|
||||
}
|
||||
else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
|
||||
else if (fieldConversionMap.get(column)!=null && StringUtils.isNotNull(value))
|
||||
{
|
||||
cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
|
||||
cell.setCellValue(convertByExpMap(Convert.toStr(value), separator, fieldConversionMap.get(column)));
|
||||
}
|
||||
else if (value instanceof BigDecimal && -1 != attr.scale())
|
||||
{
|
||||
|
|
@ -1033,38 +1055,33 @@ public class ExcelUtil<T>
|
|||
/**
|
||||
* 解析导出值 0=男,1=女,2=未知
|
||||
*
|
||||
* @param propertyValue 参数值
|
||||
* @param converterExp 翻译注解
|
||||
* @param propertyValue 解析前的值
|
||||
* @param separator 分隔符
|
||||
* @param conversionMap 解析字典
|
||||
* @return 解析后值
|
||||
*/
|
||||
public static String convertByExp(String propertyValue, String converterExp, String separator)
|
||||
public static String convertByExpMap(String propertyValue,String separator,Map<String,String> conversionMap)
|
||||
{
|
||||
StringBuilder propertyString = new StringBuilder();
|
||||
String[] convertSource = converterExp.split(",");
|
||||
for (String item : convertSource)
|
||||
{
|
||||
String[] itemArray = item.split("=");
|
||||
List<String> afterConversionValueList=new ArrayList<>();
|
||||
//如果某个对象值为"1,2,3",则根据转换Map表全都进修转换
|
||||
if (StringUtils.containsAny(propertyValue, separator))
|
||||
{
|
||||
for (String value : propertyValue.split(separator))
|
||||
{
|
||||
if (itemArray[0].equals(value))
|
||||
String conversionValue = conversionMap.get(value);
|
||||
if(StringUtils.isNotEmpty(conversionValue))
|
||||
{
|
||||
propertyString.append(itemArray[1] + separator);
|
||||
break;
|
||||
afterConversionValueList.add(conversionValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
}else
|
||||
{
|
||||
if (itemArray[0].equals(propertyValue))
|
||||
if(StringUtils.isNotEmpty(conversionMap.get(propertyValue)))
|
||||
{
|
||||
return itemArray[1];
|
||||
afterConversionValueList.add(conversionMap.get(propertyValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
return StringUtils.stripEnd(propertyString.toString(), separator);
|
||||
return String.join(separator,afterConversionValueList);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue