Excel导出时在初始化表头时进修解析值的初始化,在创建单元格时使用HashMap直接获取值,大幅度节省时间复杂度上的开销

pull/354/head
xiaoyang 2 years ago
parent 78e61d89ba
commit cbce71bcdc

@ -257,7 +257,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param is * @param is
* @return * @return
*/ */
@ -282,7 +282,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param is * @param is
* @param titleNum * @param titleNum
* @return * @return
@ -294,7 +294,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param sheetName * @param sheetName
* @param titleNum * @param titleNum
* @param is * @param is
@ -503,7 +503,7 @@ public class ExcelUtil<T>
/** /**
* listexcel * listexcel
* *
* @return * @return
*/ */
public void exportExcel(HttpServletResponse response) public void exportExcel(HttpServletResponse response)
@ -533,7 +533,8 @@ public class ExcelUtil<T>
for (int index = 0; index < sheetNo; index++) for (int index = 0; index < sheetNo; index++)
{ {
createSheet(sheetNo, index); createSheet(sheetNo, index);
//字段转换列表<列下标,<待转值,转换值>>
Map<Integer, Map<String, String>> fieldConversionMap = new HashMap<>();
// 产生一行 // 产生一行
Row row = sheet.createRow(rownum); Row row = sheet.createRow(rownum);
int column = 0; int column = 0;
@ -547,17 +548,17 @@ public class ExcelUtil<T>
for (Field subField : subFields) for (Field subField : subFields)
{ {
Excel subExcel = subField.getAnnotation(Excel.class); Excel subExcel = subField.getAnnotation(Excel.class);
this.createHeadCell(subExcel, row, column++); this.createHeadCellAndInitFieldConversion(subExcel, row, column++,fieldConversionMap);
} }
} }
else else
{ {
this.createHeadCell(excel, row, column++); this.createHeadCellAndInitFieldConversion(excel, row, column++,fieldConversionMap);
} }
} }
if (Type.EXPORT.equals(type)) if (Type.EXPORT.equals(type))
{ {
fillExcelData(index, row); fillExcelData(index, row, fieldConversionMap);
addStatisticsRow(); addStatisticsRow();
} }
} }
@ -565,12 +566,13 @@ public class ExcelUtil<T>
/** /**
* excel * excel
* *
* @param index * @param index
* @param row * @param row
* @param fieldConversionMap Map
*/ */
@SuppressWarnings("unchecked") @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 startNo = index * sheetSize;
int endNo = Math.min(startNo + sheetSize, list.size()); int endNo = Math.min(startNo + sheetSize, list.size());
@ -618,7 +620,7 @@ public class ExcelUtil<T>
{ {
subField.setAccessible(true); subField.setAccessible(true);
Excel attr = subField.getAnnotation(Excel.class); 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++; subIndex++;
} }
@ -628,7 +630,7 @@ public class ExcelUtil<T>
} }
else else
{ {
this.addCell(excel, row, vo, field, column++); this.addCell(excel, row, vo, field, column++, fieldConversionMap);
} }
} }
} }
@ -636,7 +638,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param wb * @param wb
* @return * @return
*/ */
@ -689,7 +691,7 @@ public class ExcelUtil<T>
/** /**
* Excel * Excel
* *
* @param wb * @param wb
* @return * @return
*/ */
@ -722,7 +724,7 @@ public class ExcelUtil<T>
/** /**
* Excel * Excel
* *
* @param wb * @param wb
* @return * @return
*/ */
@ -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); Cell cell = row.createCell(column);
@ -770,6 +772,19 @@ public class ExcelUtil<T>
cell.setCellValue(attr.name()); cell.setCellValue(attr.name());
setDataValidation(attr, row, column); setDataValidation(attr, row, column);
cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); 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()) if (isSubList())
{ {
// 填充默认样式,防止合并单元格样式失效 // 填充默认样式,防止合并单元格样式失效
@ -784,7 +799,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param value * @param value
* @param attr * @param attr
* @param cell * @param cell
@ -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; Cell cell = null;
try try
@ -914,9 +936,9 @@ public class ExcelUtil<T>
{ {
cell.setCellValue(parseDateToStr(dateFormat, value)); 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()) else if (value instanceof BigDecimal && -1 != attr.scale())
{ {
@ -943,7 +965,7 @@ public class ExcelUtil<T>
/** /**
* POI XSSFSheet * POI XSSFSheet
* *
* @param sheet * @param sheet
* @param textlist * @param textlist
* @param promptContent * @param promptContent
@ -980,7 +1002,7 @@ public class ExcelUtil<T>
/** /**
* ,. * ,.
* *
* @param sheet sheet. * @param sheet sheet.
* @param textlist * @param textlist
* @param promptContent * @param promptContent
@ -1033,43 +1055,38 @@ public class ExcelUtil<T>
/** /**
* 0=,1=,2= * 0=,1=,2=
* *
* @param propertyValue * @param propertyValue
* @param converterExp * @param separator
* @param separator * @param conversionMap
* @return * @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(); List<String> afterConversionValueList=new ArrayList<>();
String[] convertSource = converterExp.split(","); //如果某个对象值为"1,2,3",则根据转换Map表全都进修转换
for (String item : convertSource) if (StringUtils.containsAny(propertyValue, separator))
{ {
String[] itemArray = item.split("="); for (String value : propertyValue.split(separator))
if (StringUtils.containsAny(propertyValue, separator))
{ {
for (String value : propertyValue.split(separator)) String conversionValue = conversionMap.get(value);
if(StringUtils.isNotEmpty(conversionValue))
{ {
if (itemArray[0].equals(value)) afterConversionValueList.add(conversionValue);
{
propertyString.append(itemArray[1] + separator);
break;
}
} }
} }
else }else
{
if(StringUtils.isNotEmpty(conversionMap.get(propertyValue)))
{ {
if (itemArray[0].equals(propertyValue)) afterConversionValueList.add(conversionMap.get(propertyValue));
{
return itemArray[1];
}
} }
} }
return StringUtils.stripEnd(propertyString.toString(), separator); return String.join(separator,afterConversionValueList);
} }
/** /**
* =0,=1,=2 * =0,=1,=2
* *
* @param propertyValue * @param propertyValue
* @param converterExp * @param converterExp
* @param separator * @param separator
@ -1311,7 +1328,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param sheetNo sheet * @param sheetNo sheet
* @param index * @param index
*/ */
@ -1328,7 +1345,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param row * @param row
* @param column * @param column
* @return * @return
@ -1388,7 +1405,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param row * @param row
* @return * @return
*/ */
@ -1411,7 +1428,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param dateFormat * @param dateFormat
* @param val * @param val
* @return * @return
@ -1477,7 +1494,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param name * @param name
* @param pojoClass * @param pojoClass
* @return * @return

Loading…
Cancel
Save