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
9d831705
Commit
9d831705
authored
Aug 08, 2025
by
wujipeng
Browse files
excel模板导入
parent
23de1928
Changes
3
Show whitespace changes
Inline
Side-by-side
src/main/java/com/platform/controller/ExcelTemplateController.java
View file @
9d831705
...
...
@@ -6,11 +6,10 @@ import com.platform.model.resp.Result;
import
com.platform.model.vo.ExcelTemplateVO
;
import
com.platform.page.TableDataInfo
;
import
com.platform.service.ExcelTemplateService
;
import
com.platform.utils.DozerUtils
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletResponse
;
...
...
@@ -63,8 +62,13 @@ public class ExcelTemplateController extends BaseController {
@ApiOperation
(
value
=
"导出"
)
@GetMapping
(
"/export"
)
public
void
export
(
@RequestParam
(
name
=
"ids"
)
List
<
String
>
ids
,
HttpServletResponse
response
)
{
excelTemplateService
.
export
(
ids
,
response
);
excelTemplateService
.
export
(
ids
,
response
);
}
@ApiOperation
(
value
=
"导入"
)
@PostMapping
(
"/import"
)
public
Result
<
Boolean
>
importExcel
(
@RequestParam
(
"file"
)
MultipartFile
file
)
{
return
Result
.
data
(
excelTemplateService
.
importExcel
(
file
));
}
}
src/main/java/com/platform/service/ExcelTemplateService.java
View file @
9d831705
...
...
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import
com.platform.model.dto.ExcelTemplateDTO
;
import
com.platform.model.entity.ExcelTemplateEntity
;
import
com.platform.model.vo.ExcelTemplateVO
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.List
;
...
...
@@ -38,4 +39,11 @@ public interface ExcelTemplateService extends IService<ExcelTemplateEntity> {
* @param response response
*/
void
export
(
List
<
String
>
ids
,
HttpServletResponse
response
);
/**
* 导入
* @param file file
* @return Boolean
*/
Boolean
importExcel
(
MultipartFile
file
);
}
src/main/java/com/platform/service/impl/ExcelTemplateServiceImpl.java
View file @
9d831705
...
...
@@ -9,17 +9,19 @@ import com.platform.model.vo.ExcelTemplateVO;
import
com.platform.model.vo.TableHeaderVO
;
import
com.platform.service.ExcelTemplateService
;
import
com.platform.utils.DozerUtils
;
import
org.apache.poi.xssf.usermodel.XSSFCell
;
import
org.apache.poi.xssf.usermodel.XSSFRow
;
import
org.apache.poi.xssf.usermodel.XSSFSheet
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.io.*
;
import
java.net.URLEncoder
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
...
...
@@ -131,4 +133,146 @@ public class ExcelTemplateServiceImpl extends ServiceImpl<ExcelTemplateMapper, E
throw
new
RuntimeException
(
"导出Excel失败"
,
e
);
}
}
@Override
@Transactional
public
Boolean
importExcel
(
MultipartFile
file
)
{
// 验证文件是否为空
if
(
file
.
isEmpty
())
{
throw
new
RuntimeException
(
"导入文件不能为空"
);
}
try
(
InputStream
inputStream
=
file
.
getInputStream
();
XSSFWorkbook
workbook
=
new
XSSFWorkbook
(
inputStream
))
{
// 遍历所有sheet
for
(
int
sheetIndex
=
0
;
sheetIndex
<
workbook
.
getNumberOfSheets
();
sheetIndex
++)
{
XSSFSheet
sheet
=
workbook
.
getSheetAt
(
sheetIndex
);
// 跳过空sheet
if
(
sheet
.
getPhysicalNumberOfRows
()
==
0
)
{
continue
;
}
// 读取表头信息(兼容不同行数的sheet)
List
<
TableHeaderVO
>
jsonHeaders
=
new
ArrayList
<>();
List
<
TableHeaderVO
>
allHeaders
=
new
ArrayList
<>();
// 读取jsonHeaders(前两行)
if
(
sheet
.
getRow
(
0
)
!=
null
&&
sheet
.
getRow
(
1
)
!=
null
)
{
XSSFRow
row0
=
sheet
.
getRow
(
0
);
XSSFRow
row1
=
sheet
.
getRow
(
1
);
int
maxCells
=
Math
.
max
(
row0
.
getLastCellNum
(),
row1
.
getLastCellNum
()
);
for
(
int
cellIndex
=
0
;
cellIndex
<
maxCells
;
cellIndex
++)
{
String
name
=
getCellValueAsString
(
row0
.
getCell
(
cellIndex
));
int
index
=
getCellValueAsInt
(
row1
.
getCell
(
cellIndex
),
0
);
if
(!
"-"
.
equals
(
name
))
{
TableHeaderVO
header
=
new
TableHeaderVO
();
header
.
setName
(
name
);
header
.
setIndex
(
index
);
jsonHeaders
.
add
(
header
);
}
}
}
// 读取allHeaders(第3、4行,如果有的话)
if
(
sheet
.
getRow
(
2
)
!=
null
&&
sheet
.
getRow
(
3
)
!=
null
)
{
XSSFRow
row2
=
sheet
.
getRow
(
2
);
XSSFRow
row3
=
sheet
.
getRow
(
3
);
int
maxCells
=
Math
.
max
(
row2
.
getLastCellNum
(),
row3
.
getLastCellNum
()
);
for
(
int
cellIndex
=
0
;
cellIndex
<
maxCells
;
cellIndex
++)
{
String
name
=
getCellValueAsString
(
row2
.
getCell
(
cellIndex
));
int
index
=
getCellValueAsInt
(
row3
.
getCell
(
cellIndex
),
0
);
if
(!
"-"
.
equals
(
name
))
{
TableHeaderVO
header
=
new
TableHeaderVO
();
header
.
setName
(
name
);
header
.
setIndex
(
index
);
allHeaders
.
add
(
header
);
}
}
}
// 创建或更新模板实体
ExcelTemplateEntity
template
=
new
ExcelTemplateEntity
();
template
.
setJson
(
JSON
.
toJSONString
(
jsonHeaders
));
template
.
setAllHeader
(
JSON
.
toJSONString
(
allHeaders
));
template
.
setName
(
sheet
.
getSheetName
());
// 保存到数据库
this
.
saveOrUpdate
(
template
);
}
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"导入Excel失败: "
+
e
.
getMessage
(),
e
);
}
return
Boolean
.
TRUE
;
}
/**
* 辅助方法:获取单元格值并转为字符串
*/
private
String
getCellValueAsString
(
XSSFCell
cell
)
{
if
(
cell
==
null
)
{
return
"-"
;
}
switch
(
cell
.
getCellType
())
{
case
STRING:
return
cell
.
getStringCellValue
();
case
NUMERIC:
return
String
.
valueOf
(
cell
.
getNumericCellValue
());
case
BOOLEAN:
return
String
.
valueOf
(
cell
.
getBooleanCellValue
());
case
FORMULA:
return
cell
.
getCellFormula
();
default
:
return
"-"
;
}
}
/**
* 获取单元格值并转为int类型(带默认值)
*
* @param cell Excel单元格对象
* @param defaultValue 转换失败时返回的默认值
* @return 转换后的int值,如果单元格为空或转换失败则返回指定的默认值
*/
private
int
getCellValueAsInt
(
XSSFCell
cell
,
int
defaultValue
)
{
if
(
cell
==
null
)
{
return
defaultValue
;
}
try
{
switch
(
cell
.
getCellType
())
{
case
NUMERIC:
return
(
int
)
cell
.
getNumericCellValue
();
case
STRING:
String
strValue
=
cell
.
getStringCellValue
().
trim
();
if
(
strValue
.
isEmpty
())
{
return
defaultValue
;
}
return
Integer
.
parseInt
(
strValue
);
case
BOOLEAN:
return
cell
.
getBooleanCellValue
()
?
1
:
0
;
case
FORMULA:
switch
(
cell
.
getCachedFormulaResultType
())
{
case
NUMERIC:
return
(
int
)
cell
.
getNumericCellValue
();
case
STRING:
String
formulaStrValue
=
cell
.
getStringCellValue
().
trim
();
return
formulaStrValue
.
isEmpty
()
?
defaultValue
:
Integer
.
parseInt
(
formulaStrValue
);
case
BOOLEAN:
return
cell
.
getBooleanCellValue
()
?
1
:
0
;
default
:
return
defaultValue
;
}
default
:
return
defaultValue
;
}
}
catch
(
NumberFormatException
e
)
{
// 记录日志或根据需要处理异常
return
defaultValue
;
}
}
}
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