Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
traffic-signal-platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
signal
traffic-signal-platform
Commits
5a2028bd
Commit
5a2028bd
authored
Jun 13, 2024
by
duanruiming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[update] 微观大数据平台-优化文件导出工具类
parent
4789af10
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
44 deletions
+63
-44
ExcelExportUtils.java
.../src/main/java/net/wanji/opt/common/ExcelExportUtils.java
+46
-0
TrendServiceImpl.java
...ain/java/net/wanji/opt/service/impl/TrendServiceImpl.java
+17
-44
No files found.
signal-optimize-service/src/main/java/net/wanji/opt/common/ExcelExportUtils.java
0 → 100644
View file @
5a2028bd
package
net
.
wanji
.
opt
.
common
;
import
com.alibaba.excel.EasyExcel
;
import
com.alibaba.excel.ExcelWriter
;
import
com.alibaba.excel.write.metadata.WriteSheet
;
import
lombok.extern.slf4j.Slf4j
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletResponse
;
import
java.net.URLEncoder
;
import
java.util.List
;
/**
* @author duanruiming
* @date 2024/06/13 9:35
*/
@Slf4j
public
class
ExcelExportUtils
{
public
static
<
T
>
void
exportExcel
(
HttpServletResponse
response
,
String
startStr
,
String
endStr
,
List
<
T
>
dataList
,
String
sheetName
,
Class
<
T
>
clazz
)
throws
Exception
{
// 文件名 sheet名称添加日期
String
fileName
=
sheetName
.
concat
(
startStr
).
concat
(
"~"
).
concat
(
endStr
)
.
replaceAll
(
":"
,
" "
).
replaceAll
(
" "
,
"-"
).
concat
(
".xlsx"
);
// 注意文件扩展名应为.xlsx,因为你设置了xlsx的MIME类型
// 设置响应头
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setContentType
(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename="
+
URLEncoder
.
encode
(
fileName
,
"UTF-8"
).
replaceAll
(
"\\+"
,
"%20"
));
// 使用EasyExcel进行导出
try
(
ServletOutputStream
outputStream
=
response
.
getOutputStream
())
{
ExcelWriter
excelWriter
=
EasyExcel
.
write
(
outputStream
,
clazz
).
build
();
WriteSheet
writeSheet
=
EasyExcel
.
writerSheet
(
sheetName
).
build
();
// 填充数据
excelWriter
.
write
(
dataList
,
writeSheet
);
// 刷新并关闭流,不需要重复关闭outputStream
excelWriter
.
finish
();
}
catch
(
Exception
e
)
{
log
.
error
(
"{}导出excel异常:"
,
fileName
,
e
);
throw
new
Exception
(
"导出excel异常:"
+
e
.
getMessage
(),
e
);
}
}
}
signal-optimize-service/src/main/java/net/wanji/opt/service/impl/TrendServiceImpl.java
View file @
5a2028bd
...
...
@@ -5,7 +5,6 @@ import cn.hutool.core.date.DateTime;
import
cn.hutool.core.date.DateUnit
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
com.alibaba.excel.EasyExcel
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
...
...
@@ -22,6 +21,7 @@ import net.wanji.databus.po.*;
import
net.wanji.databus.vo.AbnormalCrossListVO
;
import
net.wanji.databus.vo.GreenwaveListVO
;
import
net.wanji.opt.bo.*
;
import
net.wanji.opt.common.ExcelExportUtils
;
import
net.wanji.opt.common.RedisUtils
;
import
net.wanji.opt.config.DirectionMappingsConfig
;
import
net.wanji.opt.dao.mapper.CrossSchemeOptLogMapper
;
...
...
@@ -56,7 +56,6 @@ import org.springframework.util.CollectionUtils;
import
javax.servlet.http.HttpServletResponse
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.net.URLEncoder
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.time.*
;
...
...
@@ -399,7 +398,7 @@ public class TrendServiceImpl implements TrendService {
abnormalCrossDetailVO
.
setCrossStopTimes
(
crossDataRealtimePO
.
getStopTimes
());
abnormalCrossDetailVO
.
setSpeed
(
crossDataRealtimePO
.
getSpeed
());
Double
sturation
=
crossDataRealtimePO
.
getSturation
();
abnormalCrossDetailVO
.
setSturation
((
double
)
Math
.
round
(
sturation
)
*
100
);
abnormalCrossDetailVO
.
setSturation
((
double
)
Math
.
round
(
sturation
)
*
100
);
String
serviceLevel
=
CrossUtil
.
getServiceLevel
(
sturation
);
abnormalCrossDetailVO
.
setServiceLevel
(
serviceLevel
);
abnormalCrossDetailVO
.
setHourFlow
(
crossDataRealtimePO
.
getFlow
()
*
12
);
...
...
@@ -1282,8 +1281,8 @@ public class TrendServiceImpl implements TrendService {
element
.
setSpeed
(
Math
.
round
(
dto
.
getMeanV
()
*
100.0
)
/
100.0
);
element
.
setHeadSpeed
(
Math
.
round
(
dto
.
getStartV
()
*
100.0
)
/
100.0
);
element
.
setMailSpeed
(
Math
.
round
(
dto
.
getEndV
()
*
100.0
)
/
100.0
);
element
.
setTeamHeadDistance
(
Math
.
round
(
dto
.
getTeamHeadDistance
()*
100.0
)
/
100.0
);
element
.
setTeamTailDistance
(
Math
.
round
(
dto
.
getTeamTailDistance
()*
100.0
)
/
100.0
);
element
.
setTeamHeadDistance
(
Math
.
round
(
dto
.
getTeamHeadDistance
()
*
100.0
)
/
100.0
);
element
.
setTeamTailDistance
(
Math
.
round
(
dto
.
getTeamTailDistance
()
*
100.0
)
/
100.0
);
element
.
setStdSpaceHeadway
(
Math
.
round
(
dto
.
getStdSpaceHeadway
()
*
100.0
)
/
100.0
);
res
.
add
(
element
);
}
...
...
@@ -2037,7 +2036,6 @@ public class TrendServiceImpl implements TrendService {
@Override
public
void
periodExcel
(
CommonCrossIdDateTimeVO
crossIdDateTimeVO
,
HttpServletResponse
response
)
throws
Exception
{
try
{
String
crossId
=
crossIdDateTimeVO
.
getCrossId
();
Date
startDate
=
crossIdDateTimeVO
.
getStart
();
Date
endDate
=
crossIdDateTimeVO
.
getEnd
();
...
...
@@ -2048,33 +2046,8 @@ public class TrendServiceImpl implements TrendService {
// 查询周期数据
List
<
CrossLaneDataHistPOExt
>
poExtList
=
crossLaneDataHistMapper
.
selectByCrossIdAndTimeSpan
(
crossId
,
start
,
end
);
List
<
TableQueryVO
.
CycleDataElement
>
cycleDataElements
=
buildCycleData
(
crossId
,
poExtList
);
String
sheet
=
"车道周期数据"
;
String
fileName
=
sheet
.
concat
(
startStr
).
concat
(
"~"
).
concat
(
endStr
)
.
replaceAll
(
":"
,
" "
).
replaceAll
(
" "
,
"-"
).
concat
(
".xls"
);
//String utf2GbkString = new String(fileName.getBytes(StandardCharsets.UTF_8),"GBK");
//String utf2Gbk2UtfString = new String(utf2GbkString.getBytes("GBK"), StandardCharsets.UTF_8);
////response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//response.setContentType("application/vnd.ms-excel;chartset=utf-8");
//// 设置响应头告诉浏览器这是一个需要下载的文件
//String headerValue = "attachment; filename=" + utf2Gbk2UtfString;
//response.setHeader("Content-Disposition", headerValue);
// 设置响应头
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setContentType
(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename="
+
URLEncoder
.
encode
(
fileName
,
"UTF-8"
).
replaceAll
(
"\\+"
,
"%20"
));
EasyExcel
.
write
(
response
.
getOutputStream
(),
TableQueryVO
.
CycleDataElement
.
class
)
.
useDefaultStyle
(
false
)
.
sheet
(
sheet
)
.
doWrite
(
cycleDataElements
);
}
catch
(
Exception
e
)
{
log
.
error
(
"车道周期数据导出excel异常:"
,
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
response
.
getOutputStream
().
close
();
}
List
<
TableQueryVO
.
CycleDataElement
>
dataList
=
buildCycleData
(
crossId
,
poExtList
);
ExcelExportUtils
.
exportExcel
(
response
,
startStr
,
endStr
,
dataList
,
"车道周期数据"
,
TableQueryVO
.
CycleDataElement
.
class
);
}
}
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