Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
dsk-operate-sys-cscec
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
fulixin
dsk-operate-sys-cscec
Commits
817aeeb1
Commit
817aeeb1
authored
Feb 06, 2024
by
lcl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
u
parent
62f94d3d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
172 deletions
+74
-172
ExcelUtils.java
...common/src/main/java/com/dsk/common/excel/ExcelUtils.java
+74
-172
No files found.
dsk-common/src/main/java/com/dsk/common/excel/ExcelUtils.java
View file @
817aeeb1
...
@@ -1389,113 +1389,11 @@ public class ExcelUtils<T> {
...
@@ -1389,113 +1389,11 @@ public class ExcelUtils<T> {
public
List
<
T
>
importExcel
(
String
sheetName
,
InputStream
is
,
int
titleNum
)
throws
Exception
{
public
List
<
T
>
importExcel
(
String
sheetName
,
InputStream
is
,
int
titleNum
)
throws
Exception
{
this
.
type
=
Excel
.
Type
.
IMPORT
;
this
.
type
=
Excel
.
Type
.
IMPORT
;
this
.
wb
=
WorkbookFactory
.
create
(
is
);
this
.
wb
=
WorkbookFactory
.
create
(
is
);
List
<
T
>
l
ist
=
new
ArrayList
<
T
>();
List
<
T
>
resL
ist
=
new
ArrayList
<
T
>();
// 如果指定sheet名,则取指定sheet中的内容 否则默认指向第1个sheet
// 如果指定sheet名,则取指定sheet中的内容 否则默认指向第1个sheet
Sheet
sheet
=
StringUtils
.
isNotEmpty
(
sheetName
)
?
wb
.
getSheet
(
sheetName
)
:
wb
.
getSheetAt
(
0
);
Sheet
sheet
=
StringUtils
.
isNotEmpty
(
sheetName
)
?
wb
.
getSheet
(
sheetName
)
:
wb
.
getSheetAt
(
0
);
if
(
sheet
==
null
)
{
dealwithSheet
(
resList
,
sheet
,
titleNum
);
throw
new
IOException
(
"文件sheet不存在"
);
return
resList
;
}
boolean
isXSSFWorkbook
=
!(
wb
instanceof
HSSFWorkbook
);
Map
<
String
,
PictureData
>
pictures
;
if
(
isXSSFWorkbook
)
{
pictures
=
getSheetPictures07
((
XSSFSheet
)
sheet
,
(
XSSFWorkbook
)
wb
);
}
else
{
pictures
=
getSheetPictures03
((
HSSFSheet
)
sheet
,
(
HSSFWorkbook
)
wb
);
}
// 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
int
rows
=
sheet
.
getLastRowNum
();
if
(
rows
>
0
)
{
// 定义一个map用于存放excel列的序号和field.
Map
<
String
,
Integer
>
cellMap
=
new
HashMap
<
String
,
Integer
>();
// 获取表头
Row
heard
=
sheet
.
getRow
(
titleNum
);
for
(
int
i
=
0
;
i
<
heard
.
getPhysicalNumberOfCells
();
i
++)
{
Cell
cell
=
heard
.
getCell
(
i
);
if
(
ObjectUtils
.
isEmpty
(
cell
))
{
cellMap
.
put
(
null
,
i
);
}
else
{
String
value
=
this
.
getCellValue
(
heard
,
i
).
toString
();
cellMap
.
put
(
value
,
i
);
}
}
// 有数据时才处理 得到类的所有field.
List
<
Object
[]>
fields
=
this
.
getFields
();
Map
<
Integer
,
Object
[]>
fieldsMap
=
new
HashMap
<
Integer
,
Object
[]>();
for
(
Object
[]
objects
:
fields
)
{
Excel
attr
=
(
Excel
)
objects
[
1
];
Integer
column
=
cellMap
.
get
(
attr
.
name
());
if
(
column
!=
null
)
{
fieldsMap
.
put
(
column
,
objects
);
}
}
for
(
int
i
=
titleNum
+
1
;
i
<=
rows
;
i
++)
{
// 从第2行开始取数据,默认第一行是表头.
Row
row
=
sheet
.
getRow
(
i
);
// 判断当前行是否是空行
if
(
isRowEmpty
(
row
))
{
continue
;
}
T
entity
=
null
;
for
(
Map
.
Entry
<
Integer
,
Object
[]>
entry
:
fieldsMap
.
entrySet
())
{
Object
val
=
this
.
getCellValue
(
row
,
entry
.
getKey
());
// 如果不存在实例则新建.
entity
=
(
entity
==
null
?
clazz
.
newInstance
()
:
entity
);
// 从map中得到对应列的field.
Field
field
=
(
Field
)
entry
.
getValue
()[
0
];
Excel
attr
=
(
Excel
)
entry
.
getValue
()[
1
];
// 取得类型,并根据对象类型设置值.
Class
<?>
fieldType
=
field
.
getType
();
if
(
String
.
class
==
fieldType
)
{
String
s
=
Convert
.
toStr
(
val
);
if
(
StringUtils
.
endsWith
(
s
,
".0"
))
{
val
=
StringUtils
.
substringBefore
(
s
,
".0"
);
}
else
{
String
dateFormat
=
field
.
getAnnotation
(
Excel
.
class
).
dateFormat
();
if
(
StringUtils
.
isNotEmpty
(
dateFormat
))
{
val
=
parseDateToStr
(
dateFormat
,
val
);
}
else
{
val
=
Convert
.
toStr
(
val
);
}
}
}
else
if
((
Integer
.
TYPE
==
fieldType
||
Integer
.
class
==
fieldType
)
&&
StringUtils
.
isNumeric
(
Convert
.
toStr
(
val
)))
{
val
=
Convert
.
toInt
(
val
);
}
else
if
((
Long
.
TYPE
==
fieldType
||
Long
.
class
==
fieldType
)
&&
StringUtils
.
isNumeric
(
Convert
.
toStr
(
val
)))
{
val
=
Convert
.
toLong
(
val
);
}
else
if
(
Double
.
TYPE
==
fieldType
||
Double
.
class
==
fieldType
)
{
val
=
Convert
.
toDouble
(
val
);
}
else
if
(
Float
.
TYPE
==
fieldType
||
Float
.
class
==
fieldType
)
{
val
=
Convert
.
toFloat
(
val
);
}
else
if
(
BigDecimal
.
class
==
fieldType
)
{
val
=
Convert
.
toBigDecimal
(
val
);
}
else
if
(
Date
.
class
==
fieldType
)
{
if
(
val
instanceof
String
)
{
val
=
DateUtils
.
parseDate
(
val
);
}
else
if
(
val
instanceof
Double
)
{
val
=
DateUtil
.
getJavaDate
((
Double
)
val
);
}
}
else
if
(
Boolean
.
TYPE
==
fieldType
||
Boolean
.
class
==
fieldType
)
{
val
=
Convert
.
toBool
(
val
,
false
);
}
if
(!
ObjectUtils
.
isEmpty
(
fieldType
))
{
String
propertyName
=
field
.
getName
();
if
(
StringUtils
.
isNotEmpty
(
attr
.
targetAttr
()))
{
propertyName
=
field
.
getName
()
+
"."
+
attr
.
targetAttr
();
}
else
if
(
StringUtils
.
isNotEmpty
(
attr
.
readConverterExp
()))
{
val
=
reverseByExp
(
Convert
.
toStr
(
val
),
attr
.
readConverterExp
(),
attr
.
separator
());
}
else
if
(
StringUtils
.
isNotEmpty
(
attr
.
dictType
()))
{
val
=
reverseDictByExp
(
Convert
.
toStr
(
val
),
attr
.
dictType
(),
attr
.
separator
());
}
else
if
(!
attr
.
handler
().
equals
(
ExcelHandlerAdapter
.
class
))
{
val
=
dataFormatHandlerAdapter
(
val
,
attr
);
}
ReflectUtils
.
invokeSetter
(
entity
,
propertyName
,
val
);
}
}
list
.
add
(
entity
);
}
}
return
list
;
}
}
/**
/**
...
@@ -1508,14 +1406,20 @@ public class ExcelUtils<T> {
...
@@ -1508,14 +1406,20 @@ public class ExcelUtils<T> {
public
List
<
T
>
importExcelAllSheet
(
InputStream
is
,
int
titleNum
)
throws
Exception
{
public
List
<
T
>
importExcelAllSheet
(
InputStream
is
,
int
titleNum
)
throws
Exception
{
this
.
type
=
Excel
.
Type
.
IMPORT
;
this
.
type
=
Excel
.
Type
.
IMPORT
;
this
.
wb
=
WorkbookFactory
.
create
(
is
);
this
.
wb
=
WorkbookFactory
.
create
(
is
);
List
<
T
>
list
=
new
ArrayList
<
T
>();
List
<
T
>
resList
=
new
ArrayList
<
T
>();
int
sheetCount
=
wb
.
getNumberOfSheets
();
int
sheetCount
=
wb
.
getNumberOfSheets
();
System
.
out
.
println
(
"工作表个数为:"
+
sheetCount
);
System
.
out
.
println
(
"工作表个数为:"
+
sheetCount
);
IntStream
.
rangeClosed
(
0
,
sheetCount
-
1
).
parallel
().
forEach
(
sheetNum
->
{
IntStream
.
rangeClosed
(
0
,
sheetCount
-
1
).
parallel
().
forEach
(
sheetNum
->
{
Sheet
sheet
=
wb
.
getSheetAt
(
sheetNum
);
Sheet
sheet
=
wb
.
getSheetAt
(
sheetNum
);
dealwithSheet
(
resList
,
sheet
,
titleNum
);
});
return
resList
;
}
private
void
dealwithSheet
(
List
<
T
>
resList
,
Sheet
sheet
,
int
titleNum
)
{
try
{
if
(
sheet
==
null
)
{
if
(
sheet
==
null
)
{
throw
new
Service
Exception
(
sheet
.
getSheetName
()
+
"为空表!"
);
throw
new
IO
Exception
(
sheet
.
getSheetName
()
+
"为空表!"
);
}
}
boolean
isXSSFWorkbook
=
!(
wb
instanceof
HSSFWorkbook
);
boolean
isXSSFWorkbook
=
!(
wb
instanceof
HSSFWorkbook
);
Map
<
String
,
PictureData
>
pictures
;
Map
<
String
,
PictureData
>
pictures
;
...
@@ -1551,7 +1455,7 @@ public class ExcelUtils<T> {
...
@@ -1551,7 +1455,7 @@ public class ExcelUtils<T> {
fieldsMap
.
put
(
column
,
objects
);
fieldsMap
.
put
(
column
,
objects
);
}
}
}
}
try
{
for
(
int
i
=
titleNum
+
1
;
i
<=
rows
;
i
++)
{
for
(
int
i
=
titleNum
+
1
;
i
<=
rows
;
i
++)
{
// 从第2行开始取数据,默认第一行是表头.
// 从第2行开始取数据,默认第一行是表头.
Row
row
=
sheet
.
getRow
(
i
);
Row
row
=
sheet
.
getRow
(
i
);
...
@@ -1619,14 +1523,12 @@ public class ExcelUtils<T> {
...
@@ -1619,14 +1523,12 @@ public class ExcelUtils<T> {
ReflectUtils
.
invokeSetter
(
entity
,
propertyName
,
val
);
ReflectUtils
.
invokeSetter
(
entity
,
propertyName
,
val
);
}
}
}
}
list
.
add
(
entity
);
resList
.
add
(
entity
);
}
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
});
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