Excel导出支持字典类型

master
RuoYi 4 years ago committed by Limy
parent 627e124548
commit 21be7c7736

@ -24,6 +24,11 @@ public @interface Excel
*/
public String dateFormat() default "";
/**
* type
*/
public String dictType() default "";
/**
* (: 0=,1=,2=)
*/

@ -50,6 +50,7 @@ import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
/**
* Excel
@ -276,7 +277,11 @@ public class ExcelUtil<T>
}
else if (StringUtils.isNotEmpty(attr.readConverterExp()))
{
val = reverseByExp(String.valueOf(val), attr.readConverterExp(), attr.separator());
val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
}
else if (StringUtils.isNotEmpty(attr.dictType()))
{
val = reverseDictByExp(attr.dictType(), Convert.toStr(val));
}
ReflectUtils.invokeSetter(entity, propertyName, val);
}
@ -536,13 +541,18 @@ public class ExcelUtil<T>
String dateFormat = attr.dateFormat();
String readConverterExp = attr.readConverterExp();
String separator = attr.separator();
String dictType = attr.dictType();
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
{
cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
}
else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
{
cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp, separator));
cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
}
else if (StringUtils.isNotEmpty(dictType))
{
cell.setCellValue(convertDictByExp(dictType, Convert.toStr(value)));
}
else
{
@ -672,8 +682,6 @@ public class ExcelUtil<T>
public static String reverseByExp(String propertyValue, String converterExp, String separator) throws Exception
{
StringBuilder propertyString = new StringBuilder();
try
{
String[] convertSource = converterExp.split(",");
for (String item : convertSource)
{
@ -697,12 +705,37 @@ public class ExcelUtil<T>
}
}
}
return StringUtils.stripEnd(propertyString.toString(), separator);
}
catch (Exception e)
/**
*
*
* @param dictType
* @param dictValue
* @return
*/
public static String convertDictByExp(String dictType, String dictValue) throws Exception
{
throw e;
Object bean = SpringUtils.getBean("dictUtils");
String methodName = "getDictLabel";
Method method = bean.getClass().getDeclaredMethod(methodName, String.class, String.class);
return Convert.toStr(method.invoke(bean, dictType, dictValue));
}
return StringUtils.stripEnd(propertyString.toString(), separator);
/**
*
*
* @param dictType
* @param dictValue
* @return
*/
public static String reverseDictByExp(String dictType, String dictLabel) throws Exception
{
Object bean = SpringUtils.getBean("dictUtils");
String methodName = "getDictValue";
Method method = bean.getClass().getDeclaredMethod(methodName, String.class, String.class);
return Convert.toStr(method.invoke(bean, dictType, dictLabel));
}
/**

@ -1,6 +1,7 @@
package com.ruoyi.system.utils;
import java.util.List;
import org.springframework.stereotype.Component;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.StringUtils;
@ -11,6 +12,7 @@ import com.ruoyi.system.domain.SysDictData;
*
* @author ruoyi
*/
@Component
public class DictUtils
{
/**
@ -41,6 +43,58 @@ public class DictUtils
return null;
}
/**
*
*
* @param dictType
* @param dictValue
* @return
*/
public static String getDictLabel(String dictType, String dictValue)
{
if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictValue))
{
List<SysDictData> datas = getDictCache(dictType);
if (StringUtils.isNotEmpty(datas))
{
for (SysDictData dict : datas)
{
if (dictValue.equals(dict.getDictValue()))
{
return dict.getDictLabel();
}
}
}
}
return dictValue;
}
/**
*
*
* @param dictType
* @param dictLabel
* @return
*/
public static String getDictValue(String dictType, String dictLabel)
{
if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictLabel))
{
List<SysDictData> datas = getDictCache(dictType);
if (StringUtils.isNotEmpty(datas))
{
for (SysDictData dict : datas)
{
if (dictLabel.equals(dict.getDictLabel()))
{
return dict.getDictValue();
}
}
}
}
return dictLabel;
}
/**
*
*/

Loading…
Cancel
Save