Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
brandkbs2
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
shenjunjie
brandkbs2
Commits
eae36092
Commit
eae36092
authored
Mar 31, 2023
by
shenjunjie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调整excel输出单元格字符串上限
parent
f56c6875
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
5 deletions
+25
-5
src/main/java/com/zhiwei/brandkbs2/easyexcel/EasyExcelUtil.java
+25
-0
src/main/java/com/zhiwei/brandkbs2/pojo/dto/ExportSearchWholeDTO.java
+0
-5
No files found.
src/main/java/com/zhiwei/brandkbs2/easyexcel/EasyExcelUtil.java
View file @
eae36092
...
@@ -14,6 +14,7 @@ import org.apache.logging.log4j.Logger;
...
@@ -14,6 +14,7 @@ import org.apache.logging.log4j.Logger;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.lang.reflect.Field
;
import
java.net.URLEncoder
;
import
java.net.URLEncoder
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
...
@@ -153,6 +154,7 @@ public class EasyExcelUtil {
...
@@ -153,6 +154,7 @@ public class EasyExcelUtil {
*/
*/
public
static
<
T
>
void
download
(
String
fileName
,
String
sheetName
,
Class
<
T
>
clazz
,
List
<
T
>
datas
,
HttpServletResponse
response
)
{
public
static
<
T
>
void
download
(
String
fileName
,
String
sheetName
,
Class
<
T
>
clazz
,
List
<
T
>
datas
,
HttpServletResponse
response
)
{
try
{
try
{
formatExcelExports
(
clazz
,
datas
);
response
.
setContentType
(
"application/vnd.ms-excel"
);
response
.
setContentType
(
"application/vnd.ms-excel"
);
response
.
setCharacterEncoding
(
"utf-8"
);
response
.
setCharacterEncoding
(
"utf-8"
);
// 这里URLEncoder.encode可以防止中文乱码
// 这里URLEncoder.encode可以防止中文乱码
...
@@ -187,4 +189,27 @@ public class EasyExcelUtil {
...
@@ -187,4 +189,27 @@ public class EasyExcelUtil {
log
.
error
(
"file:{},download error:"
,
fileName
,
e
);
log
.
error
(
"file:{},download error:"
,
fileName
,
e
);
}
}
}
}
private
static
<
T
>
void
formatExcelExports
(
Class
<
T
>
clazz
,
List
<
T
>
datas
){
List
<
Field
>
stringFields
=
new
ArrayList
<>();
// 记录需要设置的部分
for
(
Field
field
:
clazz
.
getDeclaredFields
())
{
// String格式截取前32767位
if
(
field
.
getGenericType
().
toString
().
equals
(
String
.
class
.
toString
()))
{
field
.
setAccessible
(
true
);
stringFields
.
add
(
field
);
}
}
datas
.
forEach
(
data
->
{
for
(
Field
field
:
stringFields
)
{
try
{
// 截取为excel单元格允许的最大长度
field
.
set
(
data
,
StringUtils
.
substring
((
String
)
(
field
.
get
(
data
)),
0
,
32767
));
}
catch
(
IllegalAccessException
e
)
{
log
.
error
(
e
);
}
}
});
}
}
}
src/main/java/com/zhiwei/brandkbs2/pojo/dto/ExportSearchWholeDTO.java
View file @
eae36092
...
@@ -5,7 +5,6 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth;
...
@@ -5,7 +5,6 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.ToString
;
import
lombok.ToString
;
import
org.apache.commons.lang3.StringUtils
;
import
java.util.Date
;
import
java.util.Date
;
...
@@ -44,10 +43,6 @@ public class ExportSearchWholeDTO {
...
@@ -44,10 +43,6 @@ public class ExportSearchWholeDTO {
dto
.
setTime
(
new
Date
((
Long
)
jsonObject
.
get
(
"time"
)));
dto
.
setTime
(
new
Date
((
Long
)
jsonObject
.
get
(
"time"
)));
dto
.
setPlatform
(
jsonObject
.
getString
(
"platform"
));
dto
.
setPlatform
(
jsonObject
.
getString
(
"platform"
));
dto
.
setChannel
(
jsonObject
.
getString
(
"channel"
));
dto
.
setChannel
(
jsonObject
.
getString
(
"channel"
));
// 截取为excel单元格允许的最大长度
dto
.
setTitle
(
StringUtils
.
substring
(
jsonObject
.
getString
(
"title"
),
0
,
32767
));
dto
.
setContent
(
StringUtils
.
substring
(
jsonObject
.
getString
(
"content"
),
0
,
32767
));
dto
.
setUrl
(
StringUtils
.
substring
(
jsonObject
.
getString
(
"url"
),
0
,
32767
));
return
dto
;
return
dto
;
}
}
}
}
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