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
61391b39
Commit
61391b39
authored
Jul 30, 2025
by
wujipeng
Browse files
解析excel新增字段
parent
703fa13c
Changes
5
Show whitespace changes
Inline
Side-by-side
pom.xml
View file @
61391b39
...
...
@@ -86,6 +86,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
8
</source>
<target>
8
</target>
</configuration>
</plugin>
</plugins>
</build>
...
...
src/main/java/com/platform/controller/AnalysisController.java
View file @
61391b39
...
...
@@ -28,8 +28,10 @@ public class AnalysisController {
@ApiOperation
(
"解析画布边文件上传excel内容"
)
@PostMapping
(
"/canvas"
)
public
Result
<?>
canvas
(
@RequestParam
(
value
=
"files"
)
MultipartFile
[]
files
,
@RequestParam
(
value
=
"templateId"
)
Integer
templateId
)
{
return
analysisService
.
canvas
(
files
,
templateId
);
@RequestParam
(
value
=
"templateId"
)
Integer
templateId
,
@RequestParam
(
value
=
"caseId"
)
String
caseId
,
@RequestParam
(
value
=
"folderName"
,
required
=
false
)
String
folderName
)
{
return
analysisService
.
canvas
(
files
,
templateId
,
caseId
,
folderName
);
}
@ApiOperation
(
"解析模板表头"
)
...
...
src/main/java/com/platform/service/AnalysisService.java
View file @
61391b39
...
...
@@ -19,7 +19,7 @@ public interface AnalysisService {
* @param files
* @return
*/
Result
<?>
canvas
(
MultipartFile
[]
files
,
Integer
templateId
);
Result
<?>
canvas
(
MultipartFile
[]
files
,
Integer
templateId
,
String
caseId
,
String
folderName
);
/**
* 读取模板文件
...
...
src/main/java/com/platform/service/impl/AnalysisServiceImpl.java
View file @
61391b39
...
...
@@ -11,11 +11,13 @@ import com.platform.service.ExcelDataService;
import
com.platform.service.ExcelTemplateService
;
import
com.platform.service.FileUploadService
;
import
com.platform.utils.DateUtils
;
import
com.platform.utils.StringUtils
;
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
;
...
...
@@ -29,6 +31,8 @@ import java.util.Arrays;
import
java.util.List
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.CompletionException
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.stream.Collectors
;
/**
...
...
@@ -50,115 +54,37 @@ public class AnalysisServiceImpl implements AnalysisService {
private
static
final
String
[]
EXCEL_EXTENSIONS
=
{
".xlsx"
,
".xls"
};
// @Override
// public Result<?> canvas(MultipartFile[] files, Integer templateId) {
// //查询模板
// ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId);
// String json = ExcelTemplate.getJson();
// List<TableHeaderVO> tableHeaderList = JSON.parseArray(json, TableHeaderVO.class);
// if (ObjectUtils.isEmpty(ExcelTemplate)) {
// return Result.fail("模板不存在");
// }
// try {
// if (files.length == 0) {
// return Result.fail("上传的文件为空");
// }
// List<FileUploadVO> fileUploadList = Lists.newArrayList();
// for (MultipartFile file : files) {
// // 检查是否是Excel文件
// if (isExcelFile(file.getOriginalFilename())) {
// File tempFile = convertMultipartFileToFile(file);
// processSingleExcelFile(tempFile, tableHeaderList);
// //文件上传
// FileUploadVO fileUploadVO = fileUploadService.uploadFile(file);
// fileUploadList.add(fileUploadVO);
// }
// }
// return Result.data(fileUploadList);
// } catch (Exception e) {
// return Result.fail("文件处理失败: " + e.getMessage());
// }
// }
@Override
public
Result
<?>
canvas
(
MultipartFile
[]
files
,
Integer
templateId
)
{
// 1. 预检查
if
(
files
==
null
||
files
.
length
==
0
)
{
return
Result
.
fail
(
"上传的文件为空"
);
public
Result
<?>
canvas
(
MultipartFile
[]
files
,
Integer
templateId
,
String
caseId
,
String
folderName
)
{
//查询模板
ExcelTemplateEntity
ExcelTemplate
=
excelTemplateService
.
getById
(
templateId
);
String
json
=
ExcelTemplate
.
getJson
();
List
<
TableHeaderVO
>
tableHeaderList
=
JSON
.
parseArray
(
json
,
TableHeaderVO
.
class
);
if
(
ObjectUtils
.
isEmpty
(
ExcelTemplate
))
{
return
Result
.
fail
(
"模板不存在"
);
}
// 2. 异步处理文件
List
<
CompletableFuture
<
FileUploadVO
>>
futures
=
Arrays
.
stream
(
files
)
.
filter
(
file
->
!
file
.
isEmpty
()
&&
isExcelFile
(
file
.
getOriginalFilename
()))
.
map
(
file
->
CompletableFuture
.
supplyAsync
(()
->
processFile
(
file
,
templateId
)))
.
collect
(
Collectors
.
toList
());
// 3. 等待所有任务完成并收集结果
try
{
List
<
FileUploadVO
>
fileUploadList
=
futures
.
stream
()
.
map
(
CompletableFuture:
:
join
)
.
collect
(
Collectors
.
toList
());
return
Result
.
data
(
fileUploadList
);
}
catch
(
CompletionException
e
)
{
return
Result
.
fail
(
"文件处理失败: "
+
e
.
getCause
().
getMessage
());
}
}
private
FileUploadVO
processFile
(
MultipartFile
file
,
Integer
templateId
)
{
try
{
// 1. 获取模板
ExcelTemplateEntity
excelTemplate
=
excelTemplateService
.
getById
(
templateId
);
if
(
excelTemplate
==
null
)
{
throw
new
RuntimeException
(
"模板不存在"
);
}
List
<
TableHeaderVO
>
tableHeaderList
=
JSON
.
parseArray
(
excelTemplate
.
getJson
(),
TableHeaderVO
.
class
);
// 2. 使用内存处理替代临时文件
Workbook
workbook
=
getWorkbook
(
file
);
Sheet
sheet
=
workbook
.
getSheetAt
(
0
);
// 3. 批量处理数据
List
<
ExcelDataEntity
>
excelData
=
new
ArrayList
<>(
sheet
.
getLastRowNum
());
for
(
int
i
=
1
;
i
<=
sheet
.
getLastRowNum
();
i
++)
{
Row
row
=
sheet
.
getRow
(
i
);
if
(
row
!=
null
)
{
ExcelDataEntity
entity
=
handleExcelData
(
tableHeaderList
,
row
);
if
(
entity
!=
null
)
{
excelData
.
add
(
entity
);
}
if
(
files
.
length
==
0
)
{
return
Result
.
fail
(
"上传的文件为空"
);
}
List
<
FileUploadVO
>
fileUploadList
=
Lists
.
newArrayList
();
for
(
MultipartFile
file
:
files
)
{
// 检查是否是Excel文件
if
(
isExcelFile
(
file
.
getOriginalFilename
()))
{
FileUploadVO
fileUploadVO
=
fileUploadService
.
uploadFile
(
file
);
File
tempFile
=
convertMultipartFileToFile
(
file
);
processSingleExcelFile
(
tempFile
,
tableHeaderList
,
folderName
,
fileUploadVO
.
getId
(),
caseId
);
//文件上传
fileUploadList
.
add
(
fileUploadVO
);
}
// 4. 批量保存
if
(!
excelData
.
isEmpty
())
{
excelDataService
.
saveBatch
(
excelData
);
}
// 5. 上传文件
return
fileUploadService
.
uploadFile
(
file
);
return
Result
.
data
(
fileUploadList
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"处理文件 "
+
file
.
getOriginalFilename
()
+
"
失败: "
+
e
.
getMessage
()
,
e
);
return
Result
.
fail
(
"文件处理
失败: "
+
e
.
getMessage
());
}
}
// 直接从MultipartFile创建Workbook
private
Workbook
getWorkbook
(
MultipartFile
file
)
throws
IOException
{
String
fileName
=
file
.
getOriginalFilename
().
toLowerCase
();
InputStream
inputStream
=
file
.
getInputStream
();
if
(
fileName
.
endsWith
(
".xlsx"
))
{
return
new
XSSFWorkbook
(
inputStream
);
}
else
if
(
fileName
.
endsWith
(
".xls"
))
{
return
new
HSSFWorkbook
(
inputStream
);
}
else
{
throw
new
IllegalArgumentException
(
"不支持的Excel文件格式"
);
}
}
// 重构handleExcelData返回单个实体
private
ExcelDataEntity
handleExcelData
(
List
<
TableHeaderVO
>
tableHeaderList
,
Row
row
)
{
private
ExcelDataEntity
handleExcelData
(
List
<
TableHeaderVO
>
tableHeaderList
,
Row
row
,
String
folderName
,
String
fileId
,
String
caseId
)
{
ExcelDataEntity
excelDataEntity
=
new
ExcelDataEntity
();
trySetValue
(()
->
excelDataEntity
.
setType
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
0
).
getIndex
()))));
...
...
@@ -183,10 +109,28 @@ public class AnalysisServiceImpl implements AnalysisService {
trySetValue
(()
->
excelDataEntity
.
setExternalChannel
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
14
).
getIndex
()))));
trySetValue
(()
->
excelDataEntity
.
setPayAccount
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
15
).
getIndex
()))));
trySetValue
(()
->
excelDataEntity
.
setRemark
(
getCellValueAsString
(
row
.
getCell
(
tableHeaderList
.
get
(
16
).
getIndex
()))));
if
(
StringUtils
.
isNotEmpty
(
folderName
)){
//切割文件名前面是中文的人名,使用正则表达式
trySetValue
(()
->
excelDataEntity
.
setPayUserName
(
trimChineseNameFromFilename
(
folderName
)));
}
excelDataEntity
.
setFileId
(
fileId
);
excelDataEntity
.
setUseFlag
(
true
);
excelDataEntity
.
setCaseId
(
caseId
);
return
excelDataEntity
;
}
public
static
String
trimChineseNameFromFilename
(
String
filename
)
{
// 正则表达式匹配连续的中文字符
Pattern
pattern
=
Pattern
.
compile
(
"[\u4e00-\u9fa5]+"
);
Matcher
matcher
=
pattern
.
matcher
(
filename
);
// 只获取第一组匹配的中文
if
(
matcher
.
find
())
{
return
matcher
.
group
();
}
else
{
return
""
;
}
}
// 辅助方法:简化异常处理
private
void
trySetValue
(
Runnable
setter
)
{
try
{
...
...
@@ -229,7 +173,9 @@ public class AnalysisServiceImpl implements AnalysisService {
// 检查是否是Excel文件
private
boolean
isExcelFile
(
String
filename
)
{
if
(
filename
==
null
)
return
false
;
if
(
filename
==
null
)
{
return
false
;
}
String
lowerCaseFilename
=
filename
.
toLowerCase
();
for
(
String
extension
:
EXCEL_EXTENSIONS
)
{
if
(
lowerCaseFilename
.
endsWith
(
extension
))
{
...
...
@@ -239,7 +185,7 @@ public class AnalysisServiceImpl implements AnalysisService {
return
false
;
}
private
void
processSingleExcelFile
(
File
excelFile
,
List
<
TableHeaderVO
>
tableHeaderList
)
throws
IOException
{
private
void
processSingleExcelFile
(
File
excelFile
,
List
<
TableHeaderVO
>
tableHeaderList
,
String
folderName
,
String
fileId
,
String
caseId
)
throws
IOException
{
//读取第一个sheet
Sheet
sheet
=
getWorkbook
(
excelFile
).
getSheetAt
(
0
);
//获取sheet长度
...
...
@@ -250,84 +196,13 @@ public class AnalysisServiceImpl implements AnalysisService {
for
(
int
i
=
1
;
i
<
sheetLength
;
i
++)
{
Row
row
=
sheet
.
getRow
(
i
);
//处理excel数据
handleExcelData
(
tableHeaderList
,
row
,
excelData
);
ExcelDataEntity
excelDataEntity
=
handleExcelData
(
tableHeaderList
,
row
,
folderName
,
fileId
,
caseId
);
excelData
.
add
(
excelDataEntity
);
}
//保存excel数据
excelDataService
.
saveBatch
(
excelData
);
}
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
);
}
// 根据文件类型创建Workbook
private
Workbook
getWorkbook
(
File
excelFile
)
throws
IOException
{
...
...
@@ -368,7 +243,6 @@ public class AnalysisServiceImpl implements AnalysisService {
}
}
// 将MultipartFile转换为File
private
File
convertMultipartFileToFile
(
MultipartFile
file
)
throws
IOException
{
File
convFile
=
new
File
(
System
.
getProperty
(
"java.io.tmpdir"
)
+
"/"
+
file
.
getOriginalFilename
());
...
...
src/main/java/com/platform/service/impl/FileUploadServiceImpl.java
View file @
61391b39
...
...
@@ -62,7 +62,7 @@ public class FileUploadServiceImpl extends
bsFileUploadVO
.
setPath
(
fileUrl
);
// bsFileUploadVO.setUploadBy(loginUserId);
bsFileUploadVO
.
setCreateTime
(
new
Date
());
this
.
save
OrUpdate
(
bsFileUploadVO
,
new
LambdaQueryWrapper
<
FileUploadEntity
>().
eq
(
FileUploadEntity:
:
getFileName
,
fileName
)
);
this
.
save
(
bsFileUploadVO
);
return
bsFileUploadVO
;
}
catch
(
IOException
e
)
{
throw
new
BusinessException
(
"文件上传失败"
);
...
...
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