Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
Project
Project
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
项目统一框架
amos-boot-biz
Commits
ff779d37
Commit
ff779d37
authored
Feb 29, 2024
by
liguofu@yeejoin.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化:【空工大】解析Excel文件,自动创建数据库表和插入数据,并生成数据源
parent
5aff8f79
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
42 deletions
+90
-42
ClientHandler.java
.../main/java/com/yeejoin/amos/kgd/config/ClientHandler.java
+0
-0
ExcelTool.java
.../src/main/java/com/yeejoin/amos/kgd/config/ExcelTool.java
+19
-42
InputStreamCacher.java
...n/java/com/yeejoin/amos/kgd/config/InputStreamCacher.java
+71
-0
No files found.
amos-boot-module/amos-boot-module-api/amos-boot-module-kgd-api/src/main/java/com/yeejoin/amos/kgd/config/ClientHandler.java
View file @
ff779d37
This diff is collapsed.
Click to expand it.
amos-boot-module/amos-boot-module-api/amos-boot-module-kgd-api/src/main/java/com/yeejoin/amos/kgd/config/ExcelTool.java
View file @
ff779d37
...
...
@@ -7,9 +7,6 @@ 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.web.multipart.MultipartFile
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.text.NumberFormat
;
...
...
@@ -46,12 +43,13 @@ public class ExcelTool {
}
/**
* 解析Excel
表头
* 解析Excel
*
* @param inputStream 文件
* @return
*/
public
static
List
<
String
>
readColumnsFromExcel
(
InputStream
inputStream
,
String
filename
)
throws
Exception
{
public
static
Map
<
String
,
Object
>
readFromExcel
(
InputStream
inputStream
,
String
filename
)
throws
Exception
{
Map
<
String
,
Object
>
resultTableMap
=
new
HashMap
<>();
POIFSFileSystem
pfs
=
null
;
Workbook
workbook
=
null
;
try
{
...
...
@@ -70,54 +68,32 @@ public class ExcelTool {
if
(
Objects
.
isNull
(
sheet
))
{
return
null
;
}
List
<
String
>
titleList
=
new
LinkedList
<>();
numberFormat
=
NumberFormat
.
getNumberInstance
();
// 创建一个集合根据下标来确定每个单元格对应对象的什么属性
List
<
List
<
String
>>
dataList
=
new
ArrayList
<>();
// 获取第一行数据(假如第一行就是列名)
Row
sheetTitleRow
=
sheet
.
getRow
(
sheet
.
getFirstRowNum
());
// 取出最后一列
short
lastCellNum
=
sheetTitleRow
.
getLastCellNum
();
List
<
String
>
sheetTitleList
=
new
LinkedList
<>();
for
(
int
i
=
0
;
i
<
lastCellNum
;
i
++)
{
// 取出每一列的名
String
cellValue
=
sheetTitleRow
.
getCell
(
i
).
getStringCellValue
();
sheetTitleList
.
add
(
cellValue
);
}
return
sheetTitleList
;
titleList
.
add
(
cellValue
);
}
/**
* 解析Excel数据
*
* @param inputStream 文件
* @return
*/
public
static
List
<
List
<
String
>>
readDataFromExcel
(
InputStream
inputStream
,
String
filename
)
throws
Exception
{
POIFSFileSystem
pfs
=
null
;
Workbook
workbook
=
null
;
try
{
// 解析xls和xlsx不兼容问题
workbook
=
getWorkBook
(
pfs
,
workbook
,
inputStream
,
filename
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
throw
new
Exception
(
"模板保存异常。"
);
}
if
(
workbook
==
null
)
{
throw
new
Exception
(
"请使用模板上传文件"
);
}
numberFormat
=
NumberFormat
.
getNumberInstance
();
// 创建一个集合根据下标来确定每个单元格对应对象的什么属性
List
<
List
<
String
>>
resultList
=
new
ArrayList
<>();
resultTableMap
.
put
(
"tableColumns"
,
titleList
);
// 获取表格第一个sheet的内容
Sheet
sheetAt
=
workbook
.
getSheetAt
(
0
);
// 获得sheet总行数
int
lastRowNum
=
sheet
At
.
getLastRowNum
();
int
lastRowNum
=
sheet
.
getLastRowNum
();
if
(
lastRowNum
<
1
)
{
throw
new
Exception
(
"数据错误"
);
}
// 开始读取,不读取表头所以从第二行开始
for
(
int
i
=
1
;
i
<=
lastRowNum
;
i
++)
{
// 获取每一行
Row
row
=
sheet
At
.
getRow
(
i
);
Row
row
=
sheet
.
getRow
(
i
);
// 行为空不读取
if
(
row
==
null
)
{
continue
;
...
...
@@ -129,8 +105,8 @@ public class ExcelTool {
}
List
<
String
>
rowList
=
new
ArrayList
<>();
//添加数据
short
lastCellNum
=
row
.
getLastCellNum
();
for
(
int
j
=
0
;
j
<
lastCellNum
;
j
++)
{
short
lastCellNum
2
=
row
.
getLastCellNum
();
for
(
int
j
=
0
;
j
<
lastCellNum
2
;
j
++)
{
Cell
cellOne
=
row
.
getCell
(
j
);
try
{
String
item
=
convertData
(
cellOne
);
...
...
@@ -140,16 +116,17 @@ public class ExcelTool {
System
.
out
.
println
(
i
+
"行"
+
j
+
"列数据转换出现异常"
);
rowList
.
add
(
""
);
}
resultList
.
add
(
rowList
);
}
dataList
.
add
(
rowList
);
//规避行数数据后几行为空
if
(
rowList
.
size
()
<
lastCellNum
)
{
if
(
rowList
.
size
()
<
lastCellNum
2
)
{
for
(
int
k
=
0
;
k
<
15
-
rowList
.
size
();
k
++)
{
rowList
.
add
(
""
);
}
}
}
return
resultList
;
resultTableMap
.
put
(
"tableDatas"
,
dataList
);
return
resultTableMap
;
}
/**
...
...
amos-boot-module/amos-boot-module-api/amos-boot-module-kgd-api/src/main/java/com/yeejoin/amos/kgd/config/InputStreamCacher.java
0 → 100644
View file @
ff779d37
package
com
.
yeejoin
.
amos
.
kgd
.
config
;
import
org.apache.commons.lang3.ObjectUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
public
class
InputStreamCacher
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
InputStreamCacher
.
class
);
/**
* 将InputStream中的字节保存到ByteArrayOutputStream中
*/
private
ByteArrayOutputStream
byteArrayOutputStream
;
private
InputStream
inputStream
;
public
InputStreamCacher
(
InputStream
inputStream
){
if
(
inputStream
==
null
){
return
;
}
this
.
inputStream
=
inputStream
;
initCache
();
}
/**
* 初始化
*/
private
void
initCache
(){
byteArrayOutputStream
=
new
ByteArrayOutputStream
();
byte
[]
buffer
=
new
byte
[
1024
];
int
len
;
try
{
while
((
len
=
inputStream
.
read
(
buffer
))
>-
1
){
byteArrayOutputStream
.
write
(
buffer
,
0
,
len
);
}
byteArrayOutputStream
.
flush
();
}
catch
(
IOException
e
){
log
.
error
(
e
.
getMessage
(),
e
);
}
}
/**
* 获取缓存流
* @return InputStream
*/
public
InputStream
getInputStream
(){
if
(
byteArrayOutputStream
==
null
){
return
this
.
inputStream
;
}
return
new
ByteArrayInputStream
(
byteArrayOutputStream
.
toByteArray
());
}
/**
* 销毁
*/
public
void
destroyCache
(){
this
.
byteArrayOutputStream
=
null
;
if
(
this
.
inputStream
!=
null
){
try
{
this
.
inputStream
.
close
();
}
catch
(
IOException
e
){
e
.
printStackTrace
();
}
}
}
}
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