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
83bd73b9
Commit
83bd73b9
authored
Jul 30, 2025
by
wujipeng
Browse files
解析模板表头
parent
d1c43361
Changes
4
Show whitespace changes
Inline
Side-by-side
src/main/java/com/platform/controller/AnalysisController.java
View file @
83bd73b9
package
com.platform.controller
;
import
com.platform.model.resp.Result
;
import
com.platform.model.vo.TableHeaderVO
;
import
com.platform.service.AnalysisService
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
...
...
@@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
java.util.List
;
/**
* @author : wujipeng
...
...
@@ -29,4 +31,9 @@ public class AnalysisController {
return
Result
.
data
(
analysisService
.
canvas
(
file
));
}
@ApiOperation
(
"解析模板表头"
)
@PostMapping
(
"/template"
)
public
Result
<
List
<
TableHeaderVO
>>
template
(
@RequestParam
(
value
=
"file"
,
required
=
false
)
MultipartFile
file
)
{
return
analysisService
.
template
(
file
);
}
}
src/main/java/com/platform/model/vo/TableHeaderVO.java
0 → 100644
View file @
83bd73b9
package
com.platform.model.vo
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
/**
* @author : wujipeng
* @date : 2025/7/30 14:42
* @Version: 1.0
* @Desc : 描述信息
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public
class
TableHeaderVO
{
private
String
name
;
private
Integer
index
;
}
src/main/java/com/platform/service/AnalysisService.java
View file @
83bd73b9
package
com.platform.service
;
import
com.platform.model.resp.Result
;
import
com.platform.model.vo.TableHeaderVO
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.util.List
;
/**
* @author : wujipeng
* @date : 2025/7/30 11:47
...
...
@@ -16,4 +20,11 @@ public interface AnalysisService {
* @return
*/
Object
canvas
(
MultipartFile
file
);
/**
* 读取模板文件
* @param file
* @return
*/
Result
<
List
<
TableHeaderVO
>>
template
(
MultipartFile
file
);
}
src/main/java/com/platform/service/impl/AnalysisServiceImpl.java
View file @
83bd73b9
package
com.platform.service.impl
;
import
com.platform.model.resp.Result
;
import
com.platform.model.vo.TableHeaderVO
;
import
com.platform.service.AnalysisService
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.*
;
...
...
@@ -70,8 +72,7 @@ public class AnalysisServiceImpl implements AnalysisService {
}
return
processExcelFiles
(
excelFiles
);
}
else
{
}
else
{
return
"不支持的文件类型,请上传Excel文件(.xlsx, .xls)、文件夹或包含Excel文件的ZIP文件夹"
;
}
}
catch
(
Exception
e
)
{
...
...
@@ -80,6 +81,35 @@ public class AnalysisServiceImpl implements AnalysisService {
}
}
@Override
public
Result
<
List
<
TableHeaderVO
>>
template
(
MultipartFile
file
)
{
try
{
if
(
file
.
isEmpty
())
{
return
Result
.
fail
(
"上传失败"
);
}
else
if
(
isExcelFile
(
file
.
getOriginalFilename
()))
{
File
tempFile
=
convertMultipartFileToFile
(
file
);
return
Result
.
data
(
parseTheHeader
(
tempFile
));
}
else
{
return
Result
.
fail
(
"上传失败"
);
}
}
catch
(
Exception
e
)
{
return
Result
.
fail
(
"上传失败"
);
}
}
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
<>();
int
index
=
0
;
for
(
Cell
cell
:
headerRow
)
{
headers
.
add
(
TableHeaderVO
.
builder
().
name
(
getCellValueAsString
(
cell
)).
index
(
index
++).
build
());
}
return
headers
;
}
private
boolean
isLikelyDirectory
(
String
filename
)
{
if
(
filename
==
null
)
return
false
;
String
lowerCaseFilename
=
filename
.
toLowerCase
();
...
...
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