Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tagManager
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
lanmw
tagManager
Commits
10007c56
Commit
10007c56
authored
Dec 03, 2019
by
lanmw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加 ExcelFileCtrl
parent
bb9badd9
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
308 additions
and
22 deletions
+308
-22
pom.xml
pom.xml
+17
-22
TagFileCtrl.java
src/main/java/com/keymobile/tagmanager/api/TagFileCtrl.java
+35
-0
Tag.java
src/main/java/com/keymobile/tagmanager/model/Tag.java
+27
-0
TagFileService.java
...java/com/keymobile/tagmanager/service/TagFileService.java
+56
-0
ExcelExportStylerCustomImpl.java
...eymobile/tagmanager/util/ExcelExportStylerCustomImpl.java
+63
-0
ExcelUtils.java
src/main/java/com/keymobile/tagmanager/util/ExcelUtils.java
+110
-0
No files found.
pom.xml
View file @
10007c56
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
...
...
@@ -113,16 +114,6 @@
<artifactId>
spring-boot-starter-amqp
</artifactId>
</dependency>
<dependency>
<groupId>
org.elasticsearch.plugin
</groupId>
<artifactId>
transport-netty4-client
</artifactId>
<version>
${elasticsearch.version}
</version>
</dependency>
<dependency>
<groupId>
org.elasticsearch.client
</groupId>
<artifactId>
transport
</artifactId>
<version>
${elasticsearch.version}
</version>
</dependency>
<dependency>
<groupId>
org.antlr
</groupId>
<artifactId>
antlr4-runtime
</artifactId>
<version>
4.7
</version>
...
...
@@ -133,11 +124,6 @@
<artifactId>
spring-boot-starter-data-jpa
</artifactId>
</dependency>
<dependency>
<groupId>
net.sourceforge.javacsv
</groupId>
<artifactId>
javacsv
</artifactId>
<version>
2.0
</version>
</dependency>
<dependency>
<groupId>
redis.clients
</groupId>
...
...
@@ -151,14 +137,23 @@
<version>
5.1.6
</version>
</dependency>
<!-- 引入easypoi -->
<dependency>
<groupId>
c
om.kakawait
</groupId>
<artifactId>
cas-security-spring-boot-starter
</artifactId>
<version>
1.0.0-beta-1
</version>
<groupId>
c
n.afterturn
</groupId>
<artifactId>
easypoi-base
</artifactId>
<version>
3.0.3
</version>
</dependency>
<dependency>
<groupId>
cn.afterturn
</groupId>
<artifactId>
easypoi-web
</artifactId>
<version>
3.0.3
</version>
</dependency>
<dependency>
<groupId>
cn.afterturn
</groupId>
<artifactId>
easypoi-annotation
</artifactId>
<version>
3.0.3
</version>
</dependency>
<!-- 引入easypoi -->
</dependencies>
<dependencyManagement>
...
...
src/main/java/com/keymobile/tagmanager/api/TagFileCtrl.java
0 → 100644
View file @
10007c56
package
com
.
keymobile
.
tagmanager
.
api
;
import
javax.servlet.http.HttpServletResponse
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.keymobile.tagmanager.model.Tag
;
import
com.keymobile.tagmanager.service.TagFileService
;
import
com.keymobile.tagmanager.util.UserInfoUtils
;
import
io.swagger.annotations.Api
;
@Api
(
value
=
"文件"
,
tags
=
"文件"
)
@RestController
public
class
TagFileCtrl
{
@Autowired
private
TagFileService
tagExportService
;
@GetMapping
(
"/exportExcel"
)
public
int
export
(
HttpServletResponse
response
)
{
String
userName
=
UserInfoUtils
.
getUserName
();
return
tagExportService
.
exportExcel
(
userName
,
response
);
}
@PostMapping
(
"/importExcel"
)
public
int
importExcel
(
MultipartFile
file
)
throws
Exception
{
String
userName
=
UserInfoUtils
.
getUserName
();
return
tagExportService
.
importExcel
(
userName
,
file
,
Tag
.
class
);
}
}
src/main/java/com/keymobile/tagmanager/model/Tag.java
View file @
10007c56
...
...
@@ -8,6 +8,10 @@ import org.springframework.data.mongodb.core.mapping.Document;
import
com.keymobile.tagmanager.util.Constants
;
import
cn.afterturn.easypoi.excel.annotation.Excel
;
import
cn.afterturn.easypoi.excel.annotation.ExcelTarget
;
@ExcelTarget
(
"20"
)
@Document
(
collection
=
"Tag"
)
public
class
Tag
implements
Serializable
{
...
...
@@ -18,15 +22,22 @@ public class Tag implements Serializable{
@Id
private
String
id
;
@Excel
(
name
=
"名称"
,
orderNum
=
"0"
,
width
=
30
)
private
String
name
;
@Excel
(
name
=
"英文名"
,
orderNum
=
"1"
,
width
=
30
)
private
String
nameEn
;
@Excel
(
name
=
"路径"
,
orderNum
=
"2"
,
width
=
30
)
private
String
path
;
@Excel
(
name
=
"描述"
,
orderNum
=
"3"
,
width
=
30
)
private
String
desc
;
private
String
imgId
;
@Excel
(
name
=
"排序权重"
,
orderNum
=
"4"
,
width
=
30
)
private
Integer
order
;
private
Integer
level
;
@Excel
(
name
=
"标签类型"
,
replace
=
{
"系统标签_0"
,
"自定义标签_1"
},
orderNum
=
"5"
,
width
=
30
)
private
String
tagType
;
//0, 系统标签, 1 ,自定义标签
private
String
creator
;
private
String
createDate
;
private
String
isOpen
=
Constants
.
TAG_CLOSE_STATUS
;
//0 不公开, 1, 公开
private
Integer
domain
;
private
String
dimensionType
=
Constants
.
TAG_DIMENSION_FALSE
;
//0 非维度标签, 1 维度标签
...
...
@@ -50,6 +61,14 @@ public class Tag implements Serializable{
this
.
imgId
=
imgId
;
}
public
void
setCreateDate
(
String
createDate
)
{
this
.
createDate
=
createDate
;
}
public
String
getCreateDate
()
{
return
createDate
;
}
public
Tag
(
String
parentPath
,
String
name
,
String
id
)
{
this
(
parentPath
,
name
,
""
,
""
);
this
.
id
=
id
;
...
...
@@ -159,4 +178,12 @@ public class Tag implements Serializable{
return
dimensionType
;
}
@Override
public
String
toString
()
{
return
"Tag [id="
+
id
+
", name="
+
name
+
", nameEn="
+
nameEn
+
", path="
+
path
+
", desc="
+
desc
+
", imgId="
+
imgId
+
", order="
+
order
+
", level="
+
level
+
", tagType="
+
tagType
+
", creator="
+
creator
+
", createDate="
+
createDate
+
", isOpen="
+
isOpen
+
", domain="
+
domain
+
", dimensionType="
+
dimensionType
+
"]"
;
}
}
src/main/java/com/keymobile/tagmanager/service/TagFileService.java
0 → 100644
View file @
10007c56
package
com
.
keymobile
.
tagmanager
.
service
;
import
java.util.Date
;
import
java.util.List
;
import
javax.servlet.http.HttpServletResponse
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.mongodb.core.MongoOperations
;
import
org.springframework.data.mongodb.core.query.Criteria
;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.keymobile.tagmanager.model.Tag
;
import
com.keymobile.tagmanager.util.Constants
;
import
com.keymobile.tagmanager.util.DateUtils
;
import
com.keymobile.tagmanager.util.ExcelUtils
;
@Service
public
class
TagFileService
{
@Autowired
private
MongoOperations
mongoOperations
;
public
int
exportExcel
(
String
userName
,
HttpServletResponse
response
)
{
List
<
Tag
>
toExportTags
=
mongoOperations
.
find
(
Query
.
query
(
Criteria
.
where
(
"creator"
).
is
(
userName
)
.
and
(
"dimensionType"
).
is
(
Constants
.
TAG_DIMENSION_TRUE
)),
Tag
.
class
);
// 导出操作
ExcelUtils
.
exportExcel
(
toExportTags
,
null
,
"sheet1"
,
Tag
.
class
,
"标签.xlsx"
,
response
);
return
toExportTags
.
size
();
}
public
int
importExcel
(
String
userName
,
MultipartFile
file
,
Class
<
Tag
>
class1
)
{
List
<
Tag
>
tags
=
ExcelUtils
.
importExcel
(
file
,
0
,
1
,
Tag
.
class
);
int
importCount
=
0
;
for
(
Tag
t
:
tags
)
{
if
(
checkIsValid
(
t
))
{
t
.
setLevel
(
t
.
getPath
().
split
(
","
).
length
);
t
.
setCreator
(
userName
);
t
.
setDimensionType
(
Constants
.
TAG_DIMENSION_TRUE
);
t
.
setCreateDate
(
DateUtils
.
formatDate
(
new
Date
(),
"yyyy-mm-dd"
));
mongoOperations
.
save
(
t
);
importCount
++;
}
}
return
importCount
;
}
private
boolean
checkIsValid
(
Tag
t
)
{
t
=
mongoOperations
.
findOne
(
Query
.
query
(
Criteria
.
where
(
"path"
)
.
is
(
t
.
getPath
()).
and
(
"dimensionType"
).
is
(
Constants
.
TAG_DIMENSION_TRUE
)),
Tag
.
class
);
return
t
==
null
;
}
}
src/main/java/com/keymobile/tagmanager/util/ExcelExportStylerCustomImpl.java
0 → 100644
View file @
10007c56
package
com
.
keymobile
.
tagmanager
.
util
;
import
org.apache.poi.hssf.usermodel.HSSFFont
;
import
org.apache.poi.ss.usermodel.CellStyle
;
import
org.apache.poi.ss.usermodel.Font
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler
;
import
cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler
;
public
class
ExcelExportStylerCustomImpl
extends
AbstractExcelExportStyler
implements
IExcelExportStyler
{
public
ExcelExportStylerCustomImpl
(
Workbook
workbook
)
{
super
.
createStyles
(
workbook
);
}
@Override
public
CellStyle
getHeaderStyle
(
short
headerColor
)
{
CellStyle
titleStyle
=
workbook
.
createCellStyle
();
titleStyle
.
setAlignment
(
CellStyle
.
ALIGN_CENTER
);
titleStyle
.
setVerticalAlignment
(
CellStyle
.
VERTICAL_CENTER
);
titleStyle
.
setWrapText
(
true
);
return
titleStyle
;
}
@Override
public
CellStyle
stringSeptailStyle
(
Workbook
workbook
,
boolean
isWarp
)
{
CellStyle
style
=
workbook
.
createCellStyle
();
style
.
setAlignment
(
CellStyle
.
ALIGN_CENTER
);
style
.
setVerticalAlignment
(
CellStyle
.
VERTICAL_CENTER
);
style
.
setDataFormat
(
STRING_FORMAT
);
if
(
isWarp
)
{
style
.
setWrapText
(
true
);
}
return
style
;
}
@Override
public
CellStyle
stringNoneStyle
(
Workbook
workbook
,
boolean
isWarp
)
{
CellStyle
style
=
workbook
.
createCellStyle
();
style
.
setAlignment
(
CellStyle
.
ALIGN_CENTER
);
style
.
setVerticalAlignment
(
CellStyle
.
VERTICAL_CENTER
);
style
.
setDataFormat
(
STRING_FORMAT
);
if
(
isWarp
)
{
style
.
setWrapText
(
true
);
}
return
style
;
}
@Override
public
CellStyle
getTitleStyle
(
short
color
)
{
CellStyle
titleStyle
=
workbook
.
createCellStyle
();
Font
headFont
=
workbook
.
createFont
();
headFont
.
setBoldweight
(
HSSFFont
.
BOLDWEIGHT_BOLD
);
headFont
.
setFontName
(
"宋体"
);
headFont
.
setFontHeightInPoints
((
short
)
11
);
titleStyle
.
setFont
(
headFont
);
titleStyle
.
setAlignment
(
CellStyle
.
ALIGN_CENTER
);
titleStyle
.
setVerticalAlignment
(
CellStyle
.
VERTICAL_CENTER
);
return
titleStyle
;
}
}
src/main/java/com/keymobile/tagmanager/util/ExcelUtils.java
0 → 100644
View file @
10007c56
package
com
.
keymobile
.
tagmanager
.
util
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.URLEncoder
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.NoSuchElementException
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.springframework.web.multipart.MultipartFile
;
import
cn.afterturn.easypoi.excel.ExcelExportUtil
;
import
cn.afterturn.easypoi.excel.ExcelImportUtil
;
import
cn.afterturn.easypoi.excel.entity.ExportParams
;
import
cn.afterturn.easypoi.excel.entity.ImportParams
;
import
cn.afterturn.easypoi.excel.entity.enmus.ExcelType
;
public
class
ExcelUtils
{
private
ExcelUtils
()
{};
public
static
void
exportExcel
(
List
<?>
list
,
String
title
,
String
sheetName
,
Class
<?>
pojoClass
,
String
fileName
,
boolean
isCreateHeader
,
HttpServletResponse
response
)
{
ExportParams
exportParams
=
new
ExportParams
(
title
,
sheetName
);
exportParams
.
setCreateHeadRows
(
isCreateHeader
);
defaultExport
(
list
,
pojoClass
,
fileName
,
response
,
exportParams
);
}
public
static
void
exportExcel
(
List
<?>
list
,
String
title
,
String
sheetName
,
Class
<?>
pojoClass
,
String
fileName
,
HttpServletResponse
response
)
{
ExportParams
exportParams
=
new
ExportParams
(
title
,
sheetName
,
ExcelType
.
XSSF
);
exportParams
.
setStyle
(
ExcelExportStylerCustomImpl
.
class
);
defaultExport
(
list
,
pojoClass
,
fileName
,
response
,
exportParams
);
}
public
static
void
exportExcel
(
List
<
Map
<
String
,
Object
>>
list
,
String
fileName
,
HttpServletResponse
response
)
{
defaultExport
(
list
,
fileName
,
response
);
}
private
static
void
defaultExport
(
List
<?>
list
,
Class
<?>
pojoClass
,
String
fileName
,
HttpServletResponse
response
,
ExportParams
exportParams
)
{
Workbook
workbook
=
ExcelExportUtil
.
exportExcel
(
exportParams
,
pojoClass
,
list
);
if
(
workbook
!=
null
)
;
downLoadExcel
(
fileName
,
response
,
workbook
);
}
private
static
void
downLoadExcel
(
String
fileName
,
HttpServletResponse
response
,
Workbook
workbook
)
{
try
{
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setHeader
(
"content-Type"
,
"application/vnd.ms-excel"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
URLEncoder
.
encode
(
fileName
,
"UTF-8"
));
workbook
.
write
(
response
.
getOutputStream
());
}
catch
(
IOException
e
)
{
// throw new NormalException(e.getMessage());
}
}
private
static
void
defaultExport
(
List
<
Map
<
String
,
Object
>>
list
,
String
fileName
,
HttpServletResponse
response
)
{
Workbook
workbook
=
ExcelExportUtil
.
exportExcel
(
list
,
ExcelType
.
XSSF
);
if
(
workbook
!=
null
)
;
downLoadExcel
(
fileName
,
response
,
workbook
);
}
public
static
<
T
>
List
<
T
>
importExcel
(
String
filePath
,
Integer
titleRows
,
Integer
headerRows
,
Class
<
T
>
pojoClass
)
{
if
(
StringUtils
.
isBlank
(
filePath
))
{
return
null
;
}
ImportParams
params
=
new
ImportParams
();
params
.
setTitleRows
(
titleRows
);
params
.
setHeadRows
(
headerRows
);
List
<
T
>
list
=
null
;
try
{
list
=
ExcelImportUtil
.
importExcel
(
new
File
(
filePath
),
pojoClass
,
params
);
}
catch
(
NoSuchElementException
e
)
{
// throw new NormalException("模板不能为空");
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
// throw new NormalException(e.getMessage());
}
return
list
;
}
public
static
<
T
>
List
<
T
>
importExcel
(
MultipartFile
file
,
Integer
titleRows
,
Integer
headerRows
,
Class
<
T
>
pojoClass
)
{
if
(
file
==
null
)
{
return
null
;
}
ImportParams
params
=
new
ImportParams
();
params
.
setTitleRows
(
titleRows
);
params
.
setHeadRows
(
headerRows
);
List
<
T
>
list
=
null
;
try
{
list
=
ExcelImportUtil
.
importExcel
(
file
.
getInputStream
(),
pojoClass
,
params
);
}
catch
(
NoSuchElementException
e
)
{
// throw new NormalException("excel文件不能为空");
}
catch
(
Exception
e
)
{
// throw new NormalException(e.getMessage());
System
.
out
.
println
(
e
.
getMessage
());
}
return
list
;
}
}
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