Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
map-parent
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
常超
map-parent
Commits
1470bc27
Commit
1470bc27
authored
May 30, 2022
by
yangyang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
时空车轨导出提交
parent
fe059a03
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
179 additions
and
4 deletions
+179
-4
common/common_utils/src/main/java/com/founder/commonutils/util/ExportExcelUtil.java
+144
-0
service/publicapi/src/main/java/com/founder/publicapi/controller/SkDataOperation/SkTrailController.java
+35
-4
No files found.
common/common_utils/src/main/java/com/founder/commonutils/util/ExportExcelUtil.java
0 → 100644
View file @
1470bc27
package
com
.
founder
.
commonutils
.
util
;
import
org.apache.poi.hssf.usermodel.*
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.text.DecimalFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
/**
* 导出Excel工具类 admin 2018/10/11
* title 表名
* headersName 使用数组封装excel列名
* headersId 使用数组封装对应列名需要展示的列的属性名 (二者需要一一对应,顺序不能打乱)
* dtoList 对象结果集
* */
public
class
ExportExcelUtil
<
T
>
{
@SuppressWarnings
(
"deprecation"
)
public
void
exportExcel
(
String
title
,
String
[]
headersName
,
String
[]
headersId
,
List
<
T
>
dtoList
,
HttpServletResponse
response
,
String
name
)
throws
Exception
{
Map
<
Integer
,
String
>
headersNameMap
=
new
HashMap
<>();
int
key
=
0
;
for
(
int
i
=
0
;
i
<
headersName
.
length
;
i
++)
{
if
(!
headersName
[
i
].
equals
(
null
))
{
headersNameMap
.
put
(
key
,
headersName
[
i
]);
key
++;
}
}
Map
<
Integer
,
String
>
titleFieldMap
=
new
HashMap
<>();
int
value
=
0
;
for
(
int
i
=
0
;
i
<
headersId
.
length
;
i
++)
{
if
(!
headersId
[
i
].
equals
(
null
))
{
titleFieldMap
.
put
(
value
,
headersId
[
i
]);
value
++;
}
}
HSSFWorkbook
wb
=
new
HSSFWorkbook
();
HSSFSheet
sheet
=
wb
.
createSheet
(
title
);
sheet
.
setDefaultColumnWidth
((
short
)
15
);
// 生成一个样式
HSSFCellStyle
style
=
wb
.
createCellStyle
();
HSSFRow
row
=
sheet
.
createRow
(
0
);
style
.
setAlignment
(
HSSFCellStyle
.
ALIGN_CENTER
);
HSSFCell
cell
;
Collection
<
String
>
c
=
headersNameMap
.
values
();
// 拿到表格所有标题的value的集合
Iterator
<
String
>
it
=
c
.
iterator
();
// 表格标题的迭代器
// 根据选择的字段生成表头
short
size
=
0
;
while
(
it
.
hasNext
())
{
cell
=
row
.
createCell
(
size
);
cell
.
setCellValue
(
it
.
next
().
toString
());
cell
.
setCellStyle
(
style
);
size
++;
}
Collection
<
String
>
zdC
=
titleFieldMap
.
values
();
Iterator
<
T
>
labIt
=
new
ArrayList
<
T
>().
iterator
();
if
(
dtoList
!=
null
)
{
labIt
=
dtoList
.
iterator
();
}
int
zdRow
=
0
;
while
(
labIt
.
hasNext
())
{
// 记录的迭代器,遍历总记录
int
zdCell
=
0
;
zdRow
++;
row
=
sheet
.
createRow
(
zdRow
);
T
l
=
(
T
)
labIt
.
next
();
List
<
Field
>
fields
=
new
ArrayList
<>();
Class
<?>
tempClass
=
l
.
getClass
();
while
(
tempClass
!=
null
)
{
fields
.
addAll
(
Arrays
.
asList
(
tempClass
.
getDeclaredFields
()));
tempClass
=
tempClass
.
getSuperclass
();
}
Iterator
<
String
>
zdIt
=
zdC
.
iterator
();
while
(
zdIt
.
hasNext
())
{
// 遍历要导出的字段集合
String
next
=
zdIt
.
next
();
for
(
short
i
=
0
;
i
<
fields
.
size
();
i
++)
{
// 遍历属性,比对
Field
field
=
fields
.
get
(
i
);
String
fieldName
=
field
.
getName
();
// 属性名
if
(
next
.
equals
(
fieldName
))
{
String
getMethodName
=
"get"
+
fieldName
.
substring
(
0
,
1
).
toUpperCase
()
+
fieldName
.
substring
(
1
);
// 拿到属性的get方法
try
{
Class
<?>
cls
=
l
.
getClass
();
Method
getMethod
=
cls
.
getMethod
(
getMethodName
,
new
Class
[]
{});
Object
val
=
getMethod
.
invoke
(
l
,
new
Object
[]
{});
String
textVal
=
null
;
if
(
val
!=
null
)
{
// 如果是double类型则只保留两位小说
if
(
field
.
getGenericType
().
toString
()
.
equals
(
"double"
)
||
field
.
getGenericType
().
toString
()
.
equals
(
"Double"
))
{
DecimalFormat
df
=
new
DecimalFormat
(
"#0.00"
);
String
format
=
df
.
format
(
val
);
textVal
=
String
.
valueOf
(
format
);
}
else
{
textVal
=
String
.
valueOf
(
val
);
// 转化成String
}
}
else
{
textVal
=
null
;
}
row
.
createCell
((
short
)
zdCell
)
.
setCellValue
(
textVal
);
zdCell
++;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
OutputStream
out
=
response
.
getOutputStream
();
try
{
response
.
setContentType
(
"application/x-download"
);
response
.
setCharacterEncoding
(
"utf-8"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment;fileName="
+
new
String
((
name
).
getBytes
(
"gbk"
),
"iso8859-1"
)
+
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
format
(
new
Date
())
+
".xls"
);
wb
.
write
(
out
);
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
out
!=
null
)
{
try
{
out
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
service/publicapi/src/main/java/com/founder/publicapi/controller/SkDataOperation/SkTrailController.java
View file @
1470bc27
...
...
@@ -9,15 +9,12 @@ import com.founder.commonutils.model.newPublicEntity.MapRestResult;
import
com.founder.commonutils.model.vo.param.SkTrailParam
;
import
com.founder.commonutils.model.vo.param.SkTrailSaveParam
;
import
com.founder.commonutils.model.vo.response.SkTrailVO
;
import
com.founder.commonutils.util.Base64Util
;
import
com.founder.commonutils.util.DateUtil
;
import
com.founder.commonutils.util.KeyUtil
;
import
com.founder.commonutils.util.*
;
import
com.founder.commonutils.model.newPublicEntity.SkTrail
;
import
com.founder.servicebase.controller.ExcelControllerInterface
;
import
com.founder.servicebase.logs.OperLog
;
import
com.founder.servicebase.logs.OperationType
;
import
com.founder.publicapi.service.SkTrailService
;
import
com.founder.commonutils.util.SpringUtil
;
import
com.founder.servicebase.service.SkDictionaryService
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.BeanUtils
;
...
...
@@ -25,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -234,6 +232,39 @@ public class SkTrailController extends ApiController implements ExcelControllerI
}
@PostMapping
(
"/export"
)
@ApiOperation
(
"Excel接口"
)
//@OperLog(message = "Excel接口(轨迹导出)", operation = OperationType.QUERY)
public
void
export
(
HttpServletResponse
response
,
@RequestBody
SkTrailParam
sktrailParam
)
throws
Exception
{
//分页
Page
page
=
new
Page
(
sktrailParam
.
getPage
(),
sktrailParam
.
getPageSize
());
QueryWrapper
queryWrapper
=
new
QueryWrapper
();
queryWrapper
.
eq
(
"isDeleted"
,
"0"
);
if
(
sktrailParam
.
getLikeField
()
!=
null
&&
!
sktrailParam
.
getLikeField
().
equals
(
""
))
{
queryWrapper
.
eq
(
"objectType"
,
sktrailParam
.
getLikeField
());
}
if
(
sktrailParam
.
getLikeValue
()
!=
null
&&
!
sktrailParam
.
getLikeValue
().
equals
(
""
))
{
queryWrapper
.
like
(
"objectValue"
,
sktrailParam
.
getLikeValue
());
}
//时间
if
(
sktrailParam
.
getKssj
()
!=
null
&&
!
sktrailParam
.
getKssj
().
equals
(
""
))
{
queryWrapper
.
ge
(
"str_to_date(date,'%Y-%m-%d %H:%i:%s')"
,
sktrailParam
.
getKssj
());
}
if
(
sktrailParam
.
getJssj
()
!=
null
&&
!
sktrailParam
.
getJssj
().
equals
(
""
))
{
queryWrapper
.
le
(
"str_to_date(date,'%Y-%m-%d %H:%i:%s')"
,
sktrailParam
.
getJssj
());
}
queryWrapper
.
groupBy
(
"objectValue"
);
IPage
page1
=
skTrailService
.
page
(
page
,
queryWrapper
);
List
<
SkTrail
>
records
=
page1
.
getRecords
();
ExportExcelUtil
<
SkTrail
>
exportExcelUtil
=
new
ExportExcelUtil
<>();
String
[]
headersName
=
{
"号码"
,
"经度"
,
"纬度"
,
"时间"
,
"地址"
};
String
[]
headersField
=
{
"objectvalue"
,
"x"
,
"y"
,
"time"
,
"address"
};
exportExcelUtil
.
exportExcel
(
"DataStatistics"
,
headersName
,
headersField
,
records
,
response
,
"轨迹信息"
);
}
/**
* 根据编号查找相关轨迹
*/
...
...
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