Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
cbaa49fb
Commit
cbaa49fb
authored
Dec 13, 2020
by
zhengjie
Browse files
[代码优化](v2.6):序列化优化,Excel 导出优化
parent
112c7b3e
Changes
2
Hide whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java
View file @
cbaa49fb
...
@@ -20,15 +20,11 @@ import cn.hutool.core.util.IdUtil;
...
@@ -20,15 +20,11 @@ import cn.hutool.core.util.IdUtil;
import
cn.hutool.poi.excel.BigExcelWriter
;
import
cn.hutool.poi.excel.BigExcelWriter
;
import
cn.hutool.poi.excel.ExcelUtil
;
import
cn.hutool.poi.excel.ExcelUtil
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.exception.BadRequestException
;
import
org.apache.poi.ss.usermodel.CellType
;
import
org.apache.poi.util.IOUtils
;
import
org.apache.poi.util.IOUtils
;
import
org.apache.poi.xssf.streaming.SXSSFCell
;
import
org.apache.poi.xssf.streaming.SXSSFRow
;
import
org.apache.poi.xssf.streaming.SXSSFSheet
;
import
org.apache.poi.xssf.streaming.SXSSFSheet
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
...
@@ -217,8 +213,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
...
@@ -217,8 +213,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
sheet
.
trackAllColumnsForAutoSizing
();
sheet
.
trackAllColumnsForAutoSizing
();
//列宽自适应
//列宽自适应
writer
.
autoSizeColumnAll
();
writer
.
autoSizeColumnAll
();
//列宽自适应支持中文单元格
sizeChineseColumn
(
sheet
,
writer
);
//response为HttpServletResponse对象
//response为HttpServletResponse对象
response
.
setContentType
(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
);
response
.
setContentType
(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
);
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
...
@@ -231,34 +225,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
...
@@ -231,34 +225,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
IoUtil
.
close
(
out
);
IoUtil
.
close
(
out
);
}
}
/**
* 自适应宽度(中文支持)
*/
private
static
void
sizeChineseColumn
(
SXSSFSheet
sheet
,
BigExcelWriter
writer
)
{
for
(
int
columnNum
=
0
;
columnNum
<
writer
.
getColumnCount
();
columnNum
++)
{
int
columnWidth
=
sheet
.
getColumnWidth
(
columnNum
)
/
256
;
for
(
int
rowNum
=
0
;
rowNum
<
sheet
.
getLastRowNum
();
rowNum
++)
{
SXSSFRow
currentRow
;
if
(
sheet
.
getRow
(
rowNum
)
==
null
)
{
currentRow
=
sheet
.
createRow
(
rowNum
);
}
else
{
currentRow
=
sheet
.
getRow
(
rowNum
);
}
if
(
currentRow
.
getCell
(
columnNum
)
!=
null
)
{
SXSSFCell
currentCell
=
currentRow
.
getCell
(
columnNum
);
if
(
currentCell
.
getCellTypeEnum
()
==
CellType
.
STRING
)
{
int
length
=
currentCell
.
getStringCellValue
().
getBytes
().
length
;
// 如果长度大于最大值 65280,那就取 length
if
(
columnWidth
<
length
||
(
columnWidth
*
256
>
65280
))
{
columnWidth
=
length
;
}
}
}
}
sheet
.
setColumnWidth
(
columnNum
,
columnWidth
*
256
);
}
}
public
static
String
getFileType
(
String
type
)
{
public
static
String
getFileType
(
String
type
)
{
String
documents
=
"txt doc pdf ppt pps xlsx xls docx"
;
String
documents
=
"txt doc pdf ppt pps xlsx xls docx"
;
String
music
=
"mp3 wav wma mpa ram ra aac aif m4a"
;
String
music
=
"mp3 wav wma mpa ram ra aac aif m4a"
;
...
...
eladmin-system/src/main/java/me/zhengjie/config/ConfigurerAdapter.java
View file @
cbaa49fb
...
@@ -81,15 +81,7 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
...
@@ -81,15 +81,7 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
config
.
setSerializerFeatures
(
config
.
setSerializerFeatures
(
SerializerFeature
.
DisableCircularReferenceDetect
,
SerializerFeature
.
DisableCircularReferenceDetect
,
//保留空的字段
//保留空的字段
SerializerFeature
.
WriteMapNullValue
,
SerializerFeature
.
WriteMapNullValue
);
//String null -> ""
SerializerFeature
.
WriteNullStringAsEmpty
,
//Number null -> 0
SerializerFeature
.
WriteNullNumberAsZero
,
//List null-> []
SerializerFeature
.
WriteNullListAsEmpty
,
//Boolean null -> false
SerializerFeature
.
WriteNullBooleanAsFalse
);
converter
.
setFastJsonConfig
(
config
);
converter
.
setFastJsonConfig
(
config
);
converter
.
setSupportedMediaTypes
(
supportMediaTypeList
);
converter
.
setSupportedMediaTypes
(
supportMediaTypeList
);
converter
.
setDefaultCharset
(
StandardCharsets
.
UTF_8
);
converter
.
setDefaultCharset
(
StandardCharsets
.
UTF_8
);
...
...
Write
Preview
Supports
Markdown
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