Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fgqyxxlr
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
yaru
fgqyxxlr
Commits
9ad3cc0b
Commit
9ad3cc0b
authored
Jun 23, 2020
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Excel导出支持字典类型
parent
7e259cd1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
115 additions
and
23 deletions
+115
-23
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
+6
-1
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+55
-22
ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java
+54
-0
No files found.
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
View file @
9ad3cc0b
...
...
@@ -25,6 +25,11 @@ public @interface Excel
public
String
dateFormat
()
default
""
;
/**
* 如果是字典类型,请设置字典的type值
*/
public
String
dictType
()
default
""
;
/**
* 读取内容转表达式 (如: 0=男,1=女,2=未知)
*/
public
String
readConverterExp
()
default
""
;
...
...
@@ -33,7 +38,7 @@ public @interface Excel
* 分隔符,读取字符串组内容
*/
public
String
separator
()
default
","
;
/**
* 导出类型(0数字 1字符串)
*/
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
View file @
9ad3cc0b
...
...
@@ -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,40 +682,63 @@ 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
)
{
String
[]
convertSource
=
converterExp
.
split
(
",
"
);
for
(
String
item
:
convertSource
)
String
[]
itemArray
=
item
.
split
(
"=
"
);
if
(
StringUtils
.
containsAny
(
separator
,
propertyValue
)
)
{
String
[]
itemArray
=
item
.
split
(
"="
);
if
(
StringUtils
.
containsAny
(
separator
,
propertyValue
))
for
(
String
value
:
propertyValue
.
split
(
separator
))
{
for
(
String
value
:
propertyValue
.
split
(
separator
))
if
(
itemArray
[
1
].
equals
(
value
))
{
if
(
itemArray
[
1
].
equals
(
value
))
{
propertyString
.
append
(
itemArray
[
0
]
+
separator
);
break
;
}
propertyString
.
append
(
itemArray
[
0
]
+
separator
);
break
;
}
}
else
}
else
{
if
(
itemArray
[
1
].
equals
(
propertyValue
))
{
if
(
itemArray
[
1
].
equals
(
propertyValue
))
{
return
itemArray
[
0
];
}
return
itemArray
[
0
];
}
}
}
catch
(
Exception
e
)
{
throw
e
;
}
return
StringUtils
.
stripEnd
(
propertyString
.
toString
(),
separator
);
}
/**
* 解析字典值
*
* @param dictType 字典类型
* @param dictValue 字典值
* @return 字典标签
*/
public
static
String
convertDictByExp
(
String
dictType
,
String
dictValue
)
throws
Exception
{
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
));
}
/**
* 反向解析值字典值
*
* @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
));
}
/**
* 编码文件名
*/
public
String
encodingFilename
(
String
filename
)
...
...
ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java
View file @
9ad3cc0b
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
{
/**
...
...
@@ -42,6 +44,58 @@ public class DictUtils
}
/**
* 根据字典类型和字典值获取字典标签
*
* @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
;
}
/**
* 清空字典缓存
*/
public
static
void
clearDictCache
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment