Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
D
dataDeclaration
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄夏豪
dataDeclaration
Commits
f0fb307f
提交
f0fb307f
authored
5月 18, 2021
作者:
xc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[excle]excel导入
上级
2ab2638d
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
267 行增加
和
0 行删除
+267
-0
pom.xml
pom.xml
+16
-0
ExcelController.java
...java/com/tykj/model_layer/controller/ExcelController.java
+55
-0
ExcelData.java
src/main/java/com/tykj/model_layer/excel/ExcelData.java
+123
-0
Template.java
src/main/java/com/tykj/model_layer/excel/Template.java
+73
-0
没有找到文件。
pom.xml
浏览文件 @
f0fb307f
...
@@ -103,6 +103,22 @@
...
@@ -103,6 +103,22 @@
<artifactId>
commons-lang3
</artifactId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.3.2
</version>
<version>
3.3.2
</version>
</dependency>
</dependency>
<!-- excel -->
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi
</artifactId>
<version>
4.0.1
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml
</artifactId>
<version>
4.0.1
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml-schemas
</artifactId>
<version>
4.0.1
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
src/main/java/com/tykj/model_layer/controller/ExcelController.java
0 → 100644
浏览文件 @
f0fb307f
package
com
.
tykj
.
model_layer
.
controller
;
import
com.tykj.base.result.ResultUtil
;
import
com.tykj.model_layer.excel.ExcelData
;
import
com.tykj.model_layer.service.impl.ModelHelper
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.util.Map
;
@Slf4j
@RestController
@RequestMapping
(
"/excel"
)
@Api
(
tags
=
"excel接口"
)
public
class
ExcelController
{
@Autowired
private
ExcelData
excelData
;
@ApiOperation
(
"导入excel"
)
@PostMapping
(
"/import"
)
public
ResponseEntity
importExcel
(
@RequestParam
int
id
,
@RequestBody
MultipartFile
data
){
int
i
=
excelData
.
getExcelData
(
id
,
data
);
if
(
i
==
1
)
{
return
ResultUtil
.
success
(
""
,
"导入成功!"
);
}
else
if
(
i
==
2
){
return
ResultUtil
.
failed
(
"没有模板文件导入失败!"
);
}
return
ResultUtil
.
failed
(
"导入失败!"
);
}
@ApiOperation
(
"导入excel模板"
)
@PostMapping
(
"/template"
)
public
ResponseEntity
importTemplate
(
@RequestBody
MultipartFile
template
){
Map
<
String
,
int
[]>
i
=
null
;
try
{
i
=
excelData
.
getIndenxMap
(
template
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
ResultUtil
.
failed
(
"导入失败!"
);
}
if
(
i
==
null
||
i
.
isEmpty
())
{
return
ResultUtil
.
failed
(
"没有模板文件导入失败!"
);
}
else
{
return
ResultUtil
.
success
(
""
,
"导入成功!"
);
}
}
}
src/main/java/com/tykj/model_layer/excel/ExcelData.java
0 → 100644
浏览文件 @
f0fb307f
package
com
.
tykj
.
model_layer
.
excel
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.TypeReference
;
import
com.tykj.model_layer.service.ModelService
;
import
com.tykj.model_layer.service.impl.ModelHelper
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.HashMap
;
import
java.util.Map
;
@Component
public
class
ExcelData
{
@Autowired
private
ModelService
modelService
;
@Autowired
private
ModelHelper
modelHelper
;
public
int
getExcelData
(
int
id
,
MultipartFile
dataFile
){
String
jsonData
=
modelHelper
.
getJsonExpample
(
id
);
Map
<
String
,
Map
<
String
,
Object
>>
dataMap
=
JSON
.
parseObject
(
jsonData
,
new
TypeReference
<
Map
<
String
,
Map
<
String
,
Object
>>>(){});;
try
{
//取模板,如果有模板文件传过来就解析模板文件并保存,否则就读本地模板
Map
<
String
,
int
[]>
indexMap
=
getIndenxMap
(
null
);
if
(
indexMap
.
isEmpty
()){
return
2
;
}
//取数据
InputStream
dataFis
=
dataFile
.
getInputStream
();
Workbook
dataWb
=
null
;
String
fileName
=
dataFile
.
getResource
().
getFilename
();
if
(
fileName
.
endsWith
(
"xls"
))
{
dataWb
=
new
HSSFWorkbook
(
dataFis
);
}
else
if
(
fileName
.
endsWith
(
"xlsx"
))
{
dataWb
=
new
XSSFWorkbook
(
dataFis
);
}
for
(
int
i
=
0
;
i
<
dataWb
.
getNumberOfSheets
();
i
++){
Sheet
sheet1
=
dataWb
.
getSheetAt
(
i
);
Map
<
String
,
Object
>
objectMap
=
new
HashMap
<>();
String
className
=
null
;
for
(
String
key
:
indexMap
.
keySet
()){
int
[]
value
=
indexMap
.
get
(
key
);
int
dataRow
=
value
[
0
];
int
dataCell
=
value
[
1
];
String
[]
classArray
=
key
.
split
(
"[.]"
);
className
=
classArray
[
0
];
if
(
dataMap
.
get
(
className
).
keySet
().
contains
(
classArray
[
1
])){
objectMap
.
put
(
classArray
[
1
],
sheet1
.
getRow
(
dataRow
).
getCell
(
dataCell
).
getStringCellValue
());
}
}
if
(!
objectMap
.
isEmpty
()){
Map
<
String
,
Object
>
result
=
new
HashMap
<>();
result
.
put
(
className
,
objectMap
);
System
.
out
.
println
(
result
);
modelService
.
putValueByEntityName
(
result
);
}
}
return
1
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
0
;
}
}
/**
* 读取模板文件
* @param file 前端传过来的模板文件,如果没有传就读本地文件
* @return
*/
public
Map
<
String
,
int
[]>
getIndenxMap
(
MultipartFile
file
)
throws
IOException
{
Map
<
String
,
int
[]>
indexMap
=
new
HashMap
<>();
if
(
file
!=
null
){
String
fileName
=
file
.
getResource
().
getFilename
();
InputStream
fis
=
null
;
fis
=
file
.
getInputStream
();
Workbook
wb
=
null
;
if
(
fileName
.
endsWith
(
"xls"
))
{
wb
=
new
HSSFWorkbook
(
fis
);
}
else
if
(
fileName
.
endsWith
(
"xlsx"
))
{
wb
=
new
XSSFWorkbook
(
fis
);
}
Sheet
sheet
=
wb
.
getSheetAt
(
0
);
Row
row
=
null
;
int
lastRow
=
sheet
.
getLastRowNum
();
for
(
int
index
=
0
;
index
<=
lastRow
;
index
++){
row
=
sheet
.
getRow
(
index
);
for
(
int
i
=
row
.
getFirstCellNum
();
i
<
row
.
getLastCellNum
();
++
i
)
{
if
(
row
.
getCell
(
i
)
!=
null
&&
row
.
getCell
(
i
).
getStringCellValue
().
contains
(
"$"
))
{
//{className.propertyName=[1,2]}
String
type
=
row
.
getCell
(
i
).
getStringCellValue
().
substring
(
1
);
int
[]
idenx
=
new
int
[
2
];
idenx
[
0
]
=
row
.
getRowNum
();
idenx
[
1
]
=
i
;
indexMap
.
put
(
type
,
idenx
);
}
}
}
System
.
out
.
println
(
JSON
.
toJSONString
(
indexMap
));
Template
template
=
new
Template
();
template
.
saveTemplate
(
JSON
.
toJSONString
(
indexMap
));
}
else
{
Template
template
=
new
Template
();
String
tem
=
template
.
getTemplate
();
System
.
out
.
println
(
tem
);
if
(
tem
.
isEmpty
()){
return
indexMap
;
}
indexMap
=
JSON
.
parseObject
(
tem
,
new
TypeReference
<
Map
<
String
,
int
[]>>(){});
}
return
indexMap
;
}
}
src/main/java/com/tykj/model_layer/excel/Template.java
0 → 100644
浏览文件 @
f0fb307f
package
com
.
tykj
.
model_layer
.
excel
;
import
java.io.*
;
import
java.util.Properties
;
public
class
Template
{
private
String
path
=
System
.
getProperty
(
"user.dir"
)
+
"/template.txt"
;
/**
* 把模板数据保存在txt文件中
* @param data 模板json数据;{className.propertyName:[1,2]}
* key为类名.属性名,value为该属性在模板excel中的位置[行,列]
*/
public
void
saveTemplate
(
String
data
){
Properties
properties
=
new
Properties
();
try
{
System
.
out
.
println
(
path
);
File
writeName
=
new
File
(
path
);
// 相对路径,如果没有则要建立一个新的output.txt文件
if
(!
writeName
.
exists
())
{
writeName
.
createNewFile
();
// 创建新文件,有同名的文件的话直接覆盖
}
FileWriter
writer
=
new
FileWriter
(
writeName
);
BufferedWriter
out
=
new
BufferedWriter
(
writer
);
out
.
write
(
data
);
out
.
flush
();
// 把缓存区内容压入文件
out
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
/**
* 前端没有传模板的时候,读取本地模板
* @return
*/
public
String
getTemplate
(){
String
pathname
=
path
;
try
{
String
result
=
""
;
FileReader
fileReader
=
null
;
BufferedReader
bufferedReader
=
null
;
try
{
fileReader
=
new
FileReader
(
pathname
);
bufferedReader
=
new
BufferedReader
(
fileReader
);
try
{
String
read
=
null
;
while
((
read
=
bufferedReader
.
readLine
())
!=
null
){
// result = result + read+"\r\n";
result
=
result
+
read
;
}
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
finally
{
if
(
bufferedReader
!=
null
){
bufferedReader
.
close
();
}
if
(
fileReader
!=
null
){
fileReader
.
close
();
}
}
System
.
out
.
println
(
"读取出来的文件内容是:"
+
"\r\n"
+
result
);
return
result
;
}
catch
(
Exception
e
){
e
.
printStackTrace
();
return
null
;
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论