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
8609f079
Commit
8609f079
authored
Jul 30, 2025
by
wujipeng
Browse files
解析excel数据并入库
parent
98321c30
Changes
5
Show whitespace changes
Inline
Side-by-side
src/main/java/com/platform/controller/AnalysisController.java
View file @
8609f079
...
...
@@ -27,8 +27,9 @@ public class AnalysisController {
@ApiOperation
(
"解析画布边文件上传excel或文件夹"
)
@PostMapping
(
"/canvas"
)
public
Result
<?>
canvas
(
@RequestParam
(
value
=
"file"
,
required
=
false
)
MultipartFile
file
)
{
return
Result
.
data
(
analysisService
.
canvas
(
file
));
public
Result
<?>
canvas
(
@RequestParam
(
value
=
"file"
)
MultipartFile
file
,
@RequestParam
(
value
=
"templateId"
)
Integer
templateId
)
{
return
analysisService
.
canvas
(
file
,
templateId
);
}
@ApiOperation
(
"解析模板表头"
)
...
...
src/main/java/com/platform/model/vo/TableHeaderVO.java
View file @
8609f079
package
com.platform.model.vo
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
...
...
@@ -7,7 +8,7 @@ import lombok.NoArgsConstructor;
/**
* @author : wujipeng
* @date : 2025/7/30 1
4
:4
2
* @date : 2025/7/30 1
5
:4
5
* @Version: 1.0
* @Desc : 描述信息
*/
...
...
@@ -17,8 +18,9 @@ import lombok.NoArgsConstructor;
@Builder
public
class
TableHeaderVO
{
@ApiModelProperty
(
"字段名"
)
private
String
name
;
@ApiModelProperty
(
"字段索引"
)
private
Integer
index
;
}
src/main/java/com/platform/service/AnalysisService.java
View file @
8609f079
...
...
@@ -19,7 +19,7 @@ public interface AnalysisService {
* @param file
* @return
*/
Object
canvas
(
MultipartFile
file
);
Result
<?>
canvas
(
MultipartFile
file
,
Integer
templateId
);
/**
* 读取模板文件
...
...
src/main/java/com/platform/service/impl/AnalysisServiceImpl.java
View file @
8609f079
package
com.platform.service.impl
;
import
com.alibaba.fastjson.JSON
;
import
com.platform.model.entity.ExcelDataEntity
;
import
com.platform.model.entity.ExcelTemplateEntity
;
import
com.platform.model.resp.Result
;
import
com.platform.model.vo.TableHeaderVO
;
import
com.platform.service.AnalysisService
;
import
com.platform.service.ExcelDataService
;
import
com.platform.service.ExcelTemplateService
;
import
com.platform.utils.DateUtils
;
import
org.apache.commons.compress.utils.Lists
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.StandardCopyOption
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipInputStream
;
/**
* @author : wujipeng
...
...
@@ -30,18 +36,28 @@ import java.util.zip.ZipInputStream;
@Service
public
class
AnalysisServiceImpl
implements
AnalysisService
{
@Resource
private
ExcelTemplateService
excelTemplateService
;
@Resource
private
ExcelDataService
excelDataService
;
private
static
final
String
[]
EXCEL_EXTENSIONS
=
{
".xlsx"
,
".xls"
};
@Override
public
Object
canvas
(
MultipartFile
file
)
{
public
Result
<?>
canvas
(
MultipartFile
file
,
Integer
templateId
)
{
//查询模板
ExcelTemplateEntity
ExcelTemplate
=
excelTemplateService
.
getById
(
templateId
);
if
(
ObjectUtils
.
isEmpty
(
ExcelTemplate
)){
return
Result
.
fail
(
"模板不存在"
);
}
try
{
if
(
file
.
isEmpty
())
{
return
"上传的文件为空"
;
return
Result
.
fail
(
"上传的文件为空"
)
;
}
// 检查是否是Excel文件
else
if
(
isExcelFile
(
file
.
getOriginalFilename
()))
{
File
tempFile
=
convertMultipartFileToFile
(
file
);
return
processSingleExcelFile
(
tempFile
);
return
Result
.
data
(
processSingleExcelFile
(
tempFile
,
ExcelTemplate
)
);
}
// 检查是否是目录(通过检查是否没有扩展名)
else
if
(
isLikelyDirectory
(
file
.
getOriginalFilename
()))
{
...
...
@@ -64,14 +80,12 @@ public class AnalysisServiceImpl implements AnalysisService {
excelFiles
.
add
(
uploadedFile
.
toFile
());
}
}
return
processExcelFiles
(
excelFiles
);
return
Result
.
data
(
processExcelFiles
(
excelFiles
));
}
else
{
return
"不支持的文件类型,请上传Excel文件(.xlsx, .xls)
、文件夹
或包含Excel文件的ZIP文件夹"
;
return
Result
.
fail
(
"不支持的文件类型,请上传Excel文件(.xlsx, .xls)或包含Excel文件的ZIP文件夹"
)
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
"文件处理失败: "
+
e
.
getMessage
();
return
Result
.
fail
(
"文件处理失败: "
+
e
.
getMessage
());
}
}
...
...
@@ -91,15 +105,17 @@ public class AnalysisServiceImpl implements AnalysisService {
}
}
//返回Excel文件表头数组
private
List
<
TableHeaderVO
>
parseTheHeader
(
File
tempFile
)
throws
IOException
{
//读取第一个sheet
Sheet
sheet
=
getWorkbook
(
tempFile
).
getSheetAt
(
0
);
//获取文件表头
Row
headerRow
=
sheet
.
getRow
(
0
);
List
<
TableHeaderVO
>
headers
=
new
ArrayList
<>
();
List
<
TableHeaderVO
>
headers
=
Lists
.
newArrayList
();
int
index
=
0
;
for
(
Cell
cell
:
headerRow
)
{
headers
.
add
(
TableHeaderVO
.
builder
().
name
(
getCellValueAsString
(
cell
)).
index
(
index
++).
build
());
headers
.
add
(
TableHeaderVO
.
builder
().
name
(
getCellValueAsString
(
cell
)).
index
(
index
).
build
());
index
++;
}
return
headers
;
}
...
...
@@ -151,22 +167,81 @@ public class AnalysisServiceImpl implements AnalysisService {
}
// 处理单个Excel文件
private
String
processSingleExcelFile
(
File
excelFile
)
throws
IOException
{
private
String
processSingleExcelFile
(
File
excelFile
,
ExcelTemplateEntity
ExcelTemplate
)
throws
IOException
{
String
json
=
ExcelTemplate
.
getJson
();
List
<
TableHeaderVO
>
tableHeaderList
=
JSON
.
parseArray
(
json
,
TableHeaderVO
.
class
);
//读取第一个sheet
Sheet
sheet
=
getWorkbook
(
excelFile
).
getSheetAt
(
0
);
//获取sheet长度
int
sheetLength
=
sheet
.
getLastRowNum
();
//获取文件表头
Row
headerRow
=
sheet
.
getRow
(
0
);
List
<
String
>
headers
=
new
ArrayList
<>();
for
(
Cell
cell
:
headerRow
)
{
headers
.
add
(
getCellValueAsString
(
cell
));
}
List
<
ExcelDataEntity
>
excelData
=
Lists
.
newArrayList
();
String
content
=
readExcelFile
(
excelFile
);
if
(!
excelFile
.
delete
())
{
excelFile
.
deleteOnExit
();
for
(
int
i
=
1
;
i
<
sheetLength
;
i
++)
{
Row
row
=
sheet
.
getRow
(
i
);
//处理excel数据
handleExcelData
(
tableHeaderList
,
row
,
excelData
);
}
return
"处理单个Excel文件:\n"
+
content
;
//保存excel数据
excelDataService
.
saveBatch
(
excelData
);
return
"处理单个Excel文件:\n"
;
}
private
void
handleExcelData
(
List
<
TableHeaderVO
>
tableHeaderList
,
Row
row
,
List
<
ExcelDataEntity
>
excelData
)
{
ExcelDataEntity
excelDataEntity
=
new
ExcelDataEntity
();
try
{
excelDataEntity
.
setType
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
0
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setDirection
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
1
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setTime
(
DateUtils
.
parseCompactDateTime
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
2
).
getIndex
()))));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setAmount
(
Double
.
parseDouble
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
3
).
getIndex
()))));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setSerial
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
4
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setPayeeBankCard
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
5
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setPayeeAccount
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
6
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setPayeeMerchant
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
7
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setPayeeName
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
8
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setPaymentBankName
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
9
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setPaymentBankCard
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
10
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setPaymentAccount
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
11
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setIp
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
12
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setMac
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
13
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setExternalChannel
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
14
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setPayAccount
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
15
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
try
{
excelDataEntity
.
setRemark
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
16
).
getIndex
())));
}
catch
(
Exception
ignored
)
{}
excelData
.
add
(
excelDataEntity
);
}
// 读取Excel文件内容
...
...
src/main/java/com/platform/utils/DateUtils.java
0 → 100644
View file @
8609f079
package
com.platform.utils
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
public
class
DateUtils
{
/**
* 将格式为 yyyyMMddHHmmss 的字符串转换为 Date 对象
* @param dateStr 日期时间字符串,格式必须为 yyyyMMddHHmmss
* @return 对应的 Date 对象
* @throws ParseException 如果字符串格式不匹配或无效
*/
public
static
Date
parseCompactDateTime
(
String
dateStr
)
throws
ParseException
{
if
(
dateStr
==
null
||
dateStr
.
length
()
!=
14
)
{
throw
new
ParseException
(
"日期字符串格式必须为 yyyyMMddHHmmss,长度为14位"
,
0
);
}
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
sdf
.
setLenient
(
false
);
// 严格模式,不允许自动转换无效日期
return
sdf
.
parse
(
dateStr
);
}
// 可选:添加一个重载方法,允许自定义格式
public
static
Date
parseDateTime
(
String
dateStr
,
String
pattern
)
throws
ParseException
{
if
(
dateStr
==
null
||
pattern
==
null
)
{
throw
new
ParseException
(
"日期字符串和格式模式不能为null"
,
0
);
}
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
pattern
);
sdf
.
setLenient
(
false
);
return
sdf
.
parse
(
dateStr
);
}
}
\ No newline at end of file
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