@ -280,18 +280,32 @@ public class ExcelUtil<T>
}
}
else
else
{
{
//再进行模糊匹配,模糊匹配再失败了,才是真的失败了
// 增强的模糊匹配逻辑
//或者在注解里添加模糊匹配规则
String attrName = attr . name ( ) ;
// attr 为java中的注解
String normalizedAttrName = normalizeString ( attrName ) ;
// 遍历所有表头,寻找最佳匹配
for ( Map . Entry < String , Integer > entry : cellMap . entrySet ( ) )
for ( Map . Entry < String , Integer > entry : cellMap . entrySet ( ) )
{
{
String name = entry . getKey ( ) ;
String name = entry . getKey ( ) ;
String attrName = attr . name ( ) ;
if ( name ! = null )
if ( name . contains ( attrName ) )
{
{
column = cellMap . get ( name ) ;
String normalizedName = normalizeString ( name ) ;
fieldsMap . put ( column , objects ) ;
break ;
// 检查表头是否包含注解名称(大小写不敏感,去除空格和特殊字符)
if ( normalizedName . contains ( normalizedAttrName ) )
{
column = entry . getValue ( ) ;
fieldsMap . put ( column , objects ) ;
break ;
}
// 检查注解名称是否包含表头(双向匹配)
else if ( normalizedAttrName . contains ( normalizedName ) )
{
column = entry . getValue ( ) ;
fieldsMap . put ( column , objects ) ;
break ;
}
}
}
}
}
}
}
@ -1191,7 +1205,7 @@ public class ExcelUtil<T>
if ( field . isAnnotationPresent ( Excel . class ) )
if ( field . isAnnotationPresent ( Excel . class ) )
{
{
Excel attr = field . getAnnotation ( Excel . class ) ;
Excel attr = field . getAnnotation ( Excel . class ) ;
if ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | attr . type ( ) = = type ) )
if ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | ( type ! = null & & attr . type ( ) = = type ) ) )
{
{
field . setAccessible ( true ) ;
field . setAccessible ( true ) ;
fields . add ( new Object [ ] { field , attr } ) ;
fields . add ( new Object [ ] { field , attr } ) ;
@ -1205,7 +1219,7 @@ public class ExcelUtil<T>
Excel [ ] excels = attrs . value ( ) ;
Excel [ ] excels = attrs . value ( ) ;
for ( Excel attr : excels )
for ( Excel attr : excels )
{
{
if ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | attr . type ( ) = = type ) )
if ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | ( type ! = null & & attr . type ( ) = = type ) ) )
{
{
field . setAccessible ( true ) ;
field . setAccessible ( true ) ;
fields . add ( new Object [ ] { field , attr } ) ;
fields . add ( new Object [ ] { field , attr } ) ;
@ -1230,6 +1244,26 @@ public class ExcelUtil<T>
return ( short ) ( maxHeight * 20 ) ;
return ( short ) ( maxHeight * 20 ) ;
}
}
/ * *
* 标 准 化 字 符 串 , 用 于 模 糊 匹 配
* @param str 原 始 字 符 串
* @return 标 准 化 后 的 字 符 串
* /
private String normalizeString ( String str )
{
if ( str = = null )
{
return "" ;
}
// 转换为小写
str = str . toLowerCase ( ) ;
// 去除空格
str = str . replaceAll ( "\\s+" , "" ) ;
// 去除特殊字符(使用更安全的正则表达式)
str = str . replaceAll ( "[^a-zA-Z0-9\\u4e00-\\u9fa5]" , "" ) ;
return str ;
}
/ * *
/ * *
* 创 建 一 个 工 作 簿
* 创 建 一 个 工 作 簿
* /
* /