Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
relation
Commits
e8a7d940
Commit
e8a7d940
authored
Aug 29, 2025
by
wujipeng
Browse files
文件解析优化
parent
88bd68b2
Changes
2
Hide whitespace changes
Inline
Side-by-side
pom.xml
View file @
e8a7d940
...
@@ -50,6 +50,7 @@
...
@@ -50,6 +50,7 @@
<artifactId>
poi-ooxml
</artifactId>
<artifactId>
poi-ooxml
</artifactId>
<version>
5.2.3
</version>
<version>
5.2.3
</version>
</dependency>
</dependency>
<!-- Commons IO for file operations -->
<!-- Commons IO for file operations -->
<dependency>
<dependency>
<groupId>
commons-io
</groupId>
<groupId>
commons-io
</groupId>
...
...
src/main/java/com/platform/service/impl/AnalysisServiceImpl.java
View file @
e8a7d940
...
@@ -29,6 +29,7 @@ import javax.annotation.Resource;
...
@@ -29,6 +29,7 @@ import javax.annotation.Resource;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.text.ParseException
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.List
;
import
java.util.List
;
...
@@ -188,7 +189,7 @@ public class AnalysisServiceImpl implements AnalysisService {
...
@@ -188,7 +189,7 @@ public class AnalysisServiceImpl implements AnalysisService {
.
type
(
"2"
)
.
type
(
"2"
)
.
build
());
.
build
());
}
}
if
(!
userList
.
isEmpty
()){
if
(!
userList
.
isEmpty
())
{
userService
.
saveUserBatch
(
userList
);
userService
.
saveUserBatch
(
userList
);
}
}
}
else
{
}
else
{
...
@@ -217,7 +218,7 @@ public class AnalysisServiceImpl implements AnalysisService {
...
@@ -217,7 +218,7 @@ public class AnalysisServiceImpl implements AnalysisService {
.
type
(
"2"
)
.
type
(
"2"
)
.
build
());
.
build
());
}
}
if
(!
userList
.
isEmpty
()){
if
(!
userList
.
isEmpty
())
{
userService
.
saveUserBatch
(
userList
);
userService
.
saveUserBatch
(
userList
);
}
}
}
}
...
@@ -263,7 +264,7 @@ public class AnalysisServiceImpl implements AnalysisService {
...
@@ -263,7 +264,7 @@ public class AnalysisServiceImpl implements AnalysisService {
}
}
if
(
filterIndex
.
contains
(
2
))
{
if
(
filterIndex
.
contains
(
2
))
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
if
(!
ObjectUtils
.
isEmpty
(
excelDatum
.
getTime
())){
if
(!
ObjectUtils
.
isEmpty
(
excelDatum
.
getTime
()))
{
String
formattedDate
=
sdf
.
format
(
excelDatum
.
getTime
());
String
formattedDate
=
sdf
.
format
(
excelDatum
.
getTime
());
key
.
append
(
formattedDate
);
key
.
append
(
formattedDate
);
}
}
...
@@ -450,27 +451,69 @@ public class AnalysisServiceImpl implements AnalysisService {
...
@@ -450,27 +451,69 @@ public class AnalysisServiceImpl implements AnalysisService {
}
}
// 根据文件类型创建Workbook
// // 根据文件类型创建Workbook
private
Workbook
getWorkbook
(
File
excelFile
)
{
// private Workbook getWorkbook(File excelFile) {
String
fileName
=
excelFile
.
getName
().
toLowerCase
();
// String fileName = excelFile.getName().toLowerCase();
//
// if (fileName.endsWith(".xlsx")) {
// try {
// return new XSSFWorkbook(new FileInputStream(excelFile));
// } catch (Exception e) {
// return null;
// }
// } else if (fileName.endsWith(".xls")) {
// try {
// return new HSSFWorkbook(new FileInputStream(excelFile));
// } catch (Exception e) {
// return null;
// }
// } else {
// throw new IllegalArgumentException("不支持的Excel文件格式");
// }
// }
if
(
fileName
.
endsWith
(
".xlsx"
))
{
private
Workbook
getWorkbook
(
File
excelFile
)
throws
IOException
{
try
{
if
(
excelFile
==
null
||
!
excelFile
.
exists
())
{
return
new
XSSFWorkbook
(
new
FileInputStream
(
excelFile
));
throw
new
IllegalArgumentException
(
"文件不存在或为 null"
);
}
catch
(
Exception
e
)
{
}
return
null
;
try
(
InputStream
inputStream
=
new
FileInputStream
(
excelFile
))
{
byte
[]
header
=
new
byte
[
8
];
if
(
inputStream
.
read
(
header
)
<
8
)
{
throw
new
IOException
(
"文件太小,无法识别格式"
);
}
}
}
else
if
(
fileName
.
endsWith
(
".xls"
))
{
try
{
// 检查文件头(Magic Number)判断真实格式
if
(
isXLSX
(
header
))
{
return
new
XSSFWorkbook
(
new
FileInputStream
(
excelFile
));
}
else
if
(
isXLS
(
header
))
{
return
new
HSSFWorkbook
(
new
FileInputStream
(
excelFile
));
return
new
HSSFWorkbook
(
new
FileInputStream
(
excelFile
));
}
catch
(
Exception
e
)
{
}
else
if
(
isET
(
header
))
{
return
null
;
// .et 文件(WPS 格式),尝试用 POI 解析(可能需要额外依赖)
return
new
HSSFWorkbook
(
new
FileInputStream
(
excelFile
));
// 假设 .et 兼容旧版 .xls
}
else
{
throw
new
IllegalArgumentException
(
"不支持的文件格式,仅支持 .xls, .xlsx, .et"
);
}
}
}
else
{
throw
new
IllegalArgumentException
(
"不支持的Excel文件格式"
);
}
}
}
}
// 检查是否是 .xlsx(PK/ZIP 头)
private
boolean
isXLSX
(
byte
[]
header
)
{
return
header
[
0
]
==
0x50
&&
header
[
1
]
==
0x4B
&&
header
[
2
]
==
0x03
&&
header
[
3
]
==
0x04
;
}
// 检查是否是 .xls(OLE2 头)
private
boolean
isXLS
(
byte
[]
header
)
{
return
header
[
0
]
==
(
byte
)
0xD0
&&
header
[
1
]
==
(
byte
)
0xCF
&&
header
[
2
]
==
(
byte
)
0x11
&&
header
[
3
]
==
(
byte
)
0xE0
;
}
// 检查是否是 .et(WPS 格式,可能和 .xls 类似)
private
boolean
isET
(
byte
[]
header
)
{
return
header
[
0
]
==
(
byte
)
0xD0
&&
header
[
1
]
==
(
byte
)
0xCF
&&
header
[
2
]
==
(
byte
)
0x11
&&
header
[
3
]
==
(
byte
)
0xE0
;
// 注意:.et 文件可能和 .xls 有相同的 OLE2 头,可能需要额外检查
}
// 获取单元格值作为字符串
// 获取单元格值作为字符串
private
String
getCellValueAsString
(
Cell
cell
)
{
private
String
getCellValueAsString
(
Cell
cell
)
{
if
(
cell
==
null
)
{
if
(
cell
==
null
)
{
...
...
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