Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
polysoft-nafmii
/
polysoft-parent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit a5f04db9
authored
May 07, 2021
by
孙长庆
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
导入EXCEL添加读取合并单元格功能
1 parent
5c4d0db5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
56 additions
and
161 deletions
nafmii-admin/src/main/java/org/nafmii/admin/user/controller/ImportExcelController.java
nafmii-admin/src/main/java/org/nafmii/admin/user/dao/ImportExcelMapper.java
nafmii-admin/src/main/java/org/nafmii/admin/user/service/IImportExcelService.java
nafmii-admin/src/main/java/org/nafmii/admin/user/service/impl/ImportExcelServiceImpl.java
nafmii-admin/src/main/resources/mapper/ImportExcelMapper.xml
nafmii-admin/src/main/java/org/nafmii/admin/user/controller/ImportExcelController.java
View file @
a5f04db
package
org
.
nafmii
.
admin
.
user
.
controller
;
package
org
.
nafmii
.
admin
.
user
.
controller
;
import
com.alibaba.excel.EasyExcel
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
org.nafmii.admin.user.dto.ImportExcelDto
;
import
org.nafmii.admin.user.service.IImportExcelService
;
import
org.nafmii.admin.user.service.IImportExcelService
;
import
org.nafmii.common.response.ApiResponse
;
import
org.nafmii.common.response.ApiResponse
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
...
@@ -13,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController;
...
@@ -13,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.
util.List
;
import
java.
io.IOException
;
@RestController
@RestController
@RequestMapping
(
"/nafmii/admin/import"
)
@RequestMapping
(
"/nafmii/admin/import"
)
...
@@ -25,7 +23,7 @@ public class ImportExcelController {
...
@@ -25,7 +23,7 @@ public class ImportExcelController {
@ApiOperation
(
value
=
"中心端批量导入Excel"
,
notes
=
"中心端批量导入Excel"
)
@ApiOperation
(
value
=
"中心端批量导入Excel"
,
notes
=
"中心端批量导入Excel"
)
@PostMapping
(
"/batchImportExcel"
)
@PostMapping
(
"/batchImportExcel"
)
@ResponseBody
@ResponseBody
public
ApiResponse
batchImportExcel
(
MultipartFile
file
){
public
ApiResponse
batchImportExcel
(
MultipartFile
file
)
throws
IOException
{
ApiResponse
result
=
iImportExcelService
.
batchImportExcel
(
file
);
ApiResponse
result
=
iImportExcelService
.
batchImportExcel
(
file
);
return
result
;
return
result
;
}
}
...
...
nafmii-admin/src/main/java/org/nafmii/admin/user/dao/ImportExcelMapper.java
View file @
a5f04db
...
@@ -2,10 +2,8 @@ package org.nafmii.admin.user.dao;
...
@@ -2,10 +2,8 @@ package org.nafmii.admin.user.dao;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
org.apache.ibatis.annotations.Mapper
;
import
org.apache.ibatis.annotations.Mapper
;
import
org.apache.ibatis.annotations.Param
;
import
org.nafmii.admin.user.dto.ImportExcelDto
;
import
org.nafmii.admin.user.dto.ImportExcelDto
;
import
org.nafmii.admin.user.entity.CenterUserPO
;
import
java.util.List
;
/**
/**
* <p>
* <p>
...
@@ -16,14 +14,14 @@ import java.util.List;
...
@@ -16,14 +14,14 @@ import java.util.List;
* @since 2021-04-27
* @since 2021-04-27
*/
*/
@Mapper
@Mapper
public
interface
ImportExcelMapper
extends
BaseMapper
<
CenterUserPO
>
{
public
interface
ImportExcelMapper
extends
BaseMapper
<
ImportExcelDto
>
{
/**
/**
* @author:suncq
* @author:suncq
* @Function:
中心端用户登陆
* @Function:
批量导入EXCEL数据
* @date 2021/
4/27
* @date 2021/
5/6
* @ClassName:
* @ClassName:
* @version:
* @version:
* @remark:
* @remark:
*/
*/
int
insertExcelData
(
ImportExcelDto
data
);
int
insertExcelData
(
@Param
(
"data"
)
ImportExcelDto
data
);
}
}
nafmii-admin/src/main/java/org/nafmii/admin/user/service/IImportExcelService.java
View file @
a5f04db
package
org
.
nafmii
.
admin
.
user
.
service
;
package
org
.
nafmii
.
admin
.
user
.
service
;
import
org.nafmii.admin.user.dto.ImportExcelDto
;
import
org.nafmii.common.response.ApiResponse
;
import
org.nafmii.common.response.ApiResponse
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.
util.List
;
import
java.
io.IOException
;
/**
/**
* <p>
* <p>
* 中心端
-用户表 服务类
* 中心端
批量导入Excel
* </p>
* </p>
*
*
* @author suncq
* @author suncq
...
@@ -17,12 +16,12 @@ import java.util.List;
...
@@ -17,12 +16,12 @@ import java.util.List;
public
interface
IImportExcelService
{
public
interface
IImportExcelService
{
/**
/**
* @author:suncq
* @author:suncq
* @Function:中心端
用户登陆
* @Function:中心端
批量导入Excel
* @date 2021/4/27
* @date 2021/4/27
* @ClassName:
* @ClassName:
* @version:
* @version:
* @remark:
* @remark:
*/
*/
ApiResponse
batchImportExcel
(
MultipartFile
file
);
ApiResponse
batchImportExcel
(
MultipartFile
file
)
throws
IOException
;
}
}
nafmii-admin/src/main/java/org/nafmii/admin/user/service/impl/ImportExcelServiceImpl.java
View file @
a5f04db
package
org
.
nafmii
.
admin
.
user
.
service
.
impl
;
package
org
.
nafmii
.
admin
.
user
.
service
.
impl
;
import
com.alibaba.excel.EasyExcel
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.nafmii.admin.user.dao.ImportExcelMapper
;
import
org.nafmii.admin.user.dto.ImportExcelDto
;
import
org.nafmii.admin.user.service.IImportExcelService
;
import
org.nafmii.common.response.ApiResponse
;
import
org.springframework.stereotype.Service
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Row
;
...
@@ -15,23 +8,25 @@ import org.apache.poi.ss.usermodel.Sheet;
...
@@ -15,23 +8,25 @@ import org.apache.poi.ss.usermodel.Sheet;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.util.CellRangeAddress
;
import
org.apache.poi.ss.util.CellRangeAddress
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
org.nafmii.admin.user.dao.ImportExcelMapper
;
import
org.nafmii.admin.user.dto.ImportExcelDto
;
import
org.nafmii.admin.user.service.IImportExcelService
;
import
org.nafmii.admin.user.service.IImportExcelService
;
import
org.
springframework.beans.factory.annotation.Autowired
;
import
org.
nafmii.common.response.ApiResponse
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
javax.annotation.Resource
;
import
java.util.List
;
/**
/**
* <p>
* <p>
* 中心端
-用户表 服务实现类
* 中心端
批量导入Excel
* </p>
* </p>
*
*
* @author suncq
* @author suncq
...
@@ -45,40 +40,17 @@ public class ImportExcelServiceImpl implements IImportExcelService {
...
@@ -45,40 +40,17 @@ public class ImportExcelServiceImpl implements IImportExcelService {
private
ImportExcelMapper
importExcelMapper
;
private
ImportExcelMapper
importExcelMapper
;
@Override
@Override
public
ApiResponse
batchImportExcel
(
MultipartFile
file
)
{
@Transactional
(
rollbackFor
=
Exception
.
class
)
List
<
ImportExcelDto
>
list
=
null
;
public
ApiResponse
batchImportExcel
(
MultipartFile
file
)
throws
IOException
{
try
{
List
<
ImportExcelDto
>
list
=
new
ArrayList
<
ImportExcelDto
>();
list
=
EasyExcel
.
read
(
file
.
getInputStream
()).
sheet
(
0
).
head
(
ImportExcelDto
.
class
).
doReadSync
();
String
fileName
=
file
.
getOriginalFilename
();
}
catch
(
IOException
e
)
{
byte
[]
byteArr
=
file
.
getBytes
();
e
.
printStackTrace
();
InputStream
inputStream
=
new
ByteArrayInputStream
(
byteArr
);
}
log
.
info
(
"导入后生成的数据源=============>"
+
list
);
if
(!
list
.
isEmpty
())
{
for
(
ImportExcelDto
data
:
list
)
{
int
i
=
importExcelMapper
.
insertExcelData
(
data
);
if
(
i
<
0
)
{
return
new
ApiResponse
(
ApiResponse
.
FAIL
,
ApiResponse
.
FAIL_TEXT
);
}
else
{
return
new
ApiResponse
(
ApiResponse
.
SUCCESS
,
ApiResponse
.
SUCCESS_TEXT
);
}
}
}
return
new
ApiResponse
(
ApiResponse
.
FAIL
,
"导入时数据为空,导入失败"
);
}
public
String
importExcel
(
InputStream
inputStream
,
String
fileName
)
throws
Exception
{
String
message
=
"Import success"
;
boolean
isE2007
=
false
;
boolean
isE2007
=
false
;
//判断是否是excel2007格式
//判断是否是excel2007格式
if
(
fileName
.
endsWith
(
"xlsx"
))
{
if
(
fileName
.
endsWith
(
"xlsx"
))
{
isE2007
=
true
;
isE2007
=
true
;
}
}
int
rowIndex
=
0
;
try
{
InputStream
input
=
inputStream
;
//建立输入流
InputStream
input
=
inputStream
;
//建立输入流
Workbook
wb
;
Workbook
wb
;
//根据文件格式(2003或者2007)来初始化
//根据文件格式(2003或者2007)来初始化
...
@@ -88,138 +60,71 @@ public class ImportExcelServiceImpl implements IImportExcelService {
...
@@ -88,138 +60,71 @@ public class ImportExcelServiceImpl implements IImportExcelService {
wb
=
new
HSSFWorkbook
(
input
);
wb
=
new
HSSFWorkbook
(
input
);
}
}
Sheet
sheet
=
wb
.
getSheetAt
(
0
);
//获得第一个表单
Sheet
sheet
=
wb
.
getSheetAt
(
0
);
//获得第一个表单
int
rowCount
=
sheet
.
getLastRowNum
()
+
1
;
int
rowCount
=
sheet
.
getLastRowNum
()
+
1
;
//总行数
for
(
int
i
=
1
;
i
<
rowCount
;
i
++)
{
for
(
int
i
=
1
;
i
<
rowCount
;
i
++)
{
rowIndex
=
i
;
ImportExcelDto
dto
=
new
ImportExcelDto
()
;
Row
row
;
Row
row
;
int
columns
=
sheet
.
getRow
(
i
).
getLastCellNum
();
//单行列数
for
(
int
j
=
0
;
j
<
26
;
j
++)
{
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
if
(
isMergedRegion
(
sheet
,
i
,
j
))
{
if
(
isMergedRegion
(
sheet
,
i
,
j
))
{
System
.
out
.
print
(
getMergedRegionValue
(
sheet
,
i
,
j
)
+
"\t"
);
if
(
j
==
0
){
dto
.
setColumn1
(
getMergedRegionValue
(
sheet
,
i
,
j
));
}
else
if
(
j
==
1
){
dto
.
setColumn2
(
getMergedRegionValue
(
sheet
,
i
,
j
));
}
else
if
(
j
==
2
){
dto
.
setColumn3
(
getMergedRegionValue
(
sheet
,
i
,
j
));
}
}
else
{
}
else
{
row
=
sheet
.
getRow
(
i
);
row
=
sheet
.
getRow
(
i
);
System
.
out
.
print
(
row
.
getCell
(
j
)
+
"\t"
);
if
(
j
==
0
){
dto
.
setColumn1
(
String
.
valueOf
(
row
.
getCell
(
j
)));
}
else
if
(
j
==
1
){
dto
.
setColumn2
(
String
.
valueOf
(
row
.
getCell
(
j
)));
}
else
if
(
j
==
2
){
dto
.
setColumn3
(
String
.
valueOf
(
toString
()));
}
}
}
}
list
.
add
(
dto
);
}
if
(!
list
.
isEmpty
())
{
for
(
ImportExcelDto
data
:
list
)
{
int
i
=
importExcelMapper
.
insertExcelData
(
data
);
if
(
i
<
0
)
{
return
new
ApiResponse
(
ApiResponse
.
FAIL
,
ApiResponse
.
FAIL_TEXT
);
}
}
System
.
out
.
print
(
"\n"
);
}
}
}
catch
(
Exception
ex
)
{
}
else
{
message
=
"Import failed, please check the data in "
+
rowIndex
+
" rows "
;
return
new
ApiResponse
(
ApiResponse
.
FAIL
,
"导入时数据为空,导入失败"
)
;
}
}
return
message
;
return
new
ApiResponse
(
ApiResponse
.
SUCCESS
,
ApiResponse
.
SUCCESS_TEXT
)
;
}
}
/**
/**
* 获取单元格的值
* 获取单元格的值
*
* @param cell
* @param cell
* @return
* @return
*/
*/
public
String
getCellValue
(
Cell
cell
)
{
public
String
getCellValue
(
Cell
cell
)
{
if
(
cell
==
null
)
return
""
;
if
(
cell
==
null
)
return
""
;
return
cell
.
getStringCellValue
();
return
cell
.
getStringCellValue
();
}
}
/**
/**
* 合并单元格处理,获取合并行
*
* @param sheet
* @return List<CellRangeAddress>
*/
public
List
<
CellRangeAddress
>
getCombineCell
(
Sheet
sheet
)
{
List
<
CellRangeAddress
>
list
=
new
ArrayList
<>();
//获得一个 sheet 中合并单元格的数量
int
sheetmergerCount
=
sheet
.
getNumMergedRegions
();
//遍历所有的合并单元格
for
(
int
i
=
0
;
i
<
sheetmergerCount
;
i
++)
{
//获得合并单元格保存进list中
CellRangeAddress
ca
=
sheet
.
getMergedRegion
(
i
);
list
.
add
(
ca
);
}
return
list
;
}
private
int
getRowNum
(
List
<
CellRangeAddress
>
listCombineCell
,
Cell
cell
,
Sheet
sheet
)
{
int
xr
=
0
;
int
firstC
=
0
;
int
lastC
=
0
;
int
firstR
=
0
;
int
lastR
=
0
;
for
(
CellRangeAddress
ca
:
listCombineCell
)
{
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC
=
ca
.
getFirstColumn
();
lastC
=
ca
.
getLastColumn
();
firstR
=
ca
.
getFirstRow
();
lastR
=
ca
.
getLastRow
();
if
(
cell
.
getRowIndex
()
>=
firstR
&&
cell
.
getRowIndex
()
<=
lastR
)
{
if
(
cell
.
getColumnIndex
()
>=
firstC
&&
cell
.
getColumnIndex
()
<=
lastC
)
{
xr
=
lastR
;
}
}
}
return
xr
;
}
/**
* 判断单元格是否为合并单元格,是的话则将单元格的值返回
*
* @param listCombineCell 存放合并单元格的list
* @param cell 需要判断的单元格
* @param sheet sheet
* @return
*/
public
String
isCombineCell
(
List
<
CellRangeAddress
>
listCombineCell
,
Cell
cell
,
Sheet
sheet
)
throws
Exception
{
int
firstC
=
0
;
int
lastC
=
0
;
int
firstR
=
0
;
int
lastR
=
0
;
String
cellValue
=
null
;
for
(
CellRangeAddress
ca
:
listCombineCell
)
{
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC
=
ca
.
getFirstColumn
();
lastC
=
ca
.
getLastColumn
();
firstR
=
ca
.
getFirstRow
();
lastR
=
ca
.
getLastRow
();
if
(
cell
.
getRowIndex
()
>=
firstR
&&
cell
.
getRowIndex
()
<=
lastR
)
{
if
(
cell
.
getColumnIndex
()
>=
firstC
&&
cell
.
getColumnIndex
()
<=
lastC
)
{
Row
fRow
=
sheet
.
getRow
(
firstR
);
Cell
fCell
=
fRow
.
getCell
(
firstC
);
cellValue
=
getCellValue
(
fCell
);
break
;
}
}
else
{
cellValue
=
""
;
}
}
return
cellValue
;
}
/**
* 获取合并单元格的值
* 获取合并单元格的值
*
* @param sheet
* @param sheet
* @param row
* @param row
* @param column
* @param column
* @return
* @return
*/
*/
public
String
getMergedRegionValue
(
Sheet
sheet
,
int
row
,
int
column
)
{
public
String
getMergedRegionValue
(
Sheet
sheet
,
int
row
,
int
column
)
{
int
sheetMergeCount
=
sheet
.
getNumMergedRegions
();
int
sheetMergeCount
=
sheet
.
getNumMergedRegions
();
for
(
int
i
=
0
;
i
<
sheetMergeCount
;
i
++)
{
for
(
int
i
=
0
;
i
<
sheetMergeCount
;
i
++)
{
CellRangeAddress
ca
=
sheet
.
getMergedRegion
(
i
);
CellRangeAddress
ca
=
sheet
.
getMergedRegion
(
i
);
int
firstColumn
=
ca
.
getFirstColumn
();
int
firstColumn
=
ca
.
getFirstColumn
();
int
lastColumn
=
ca
.
getLastColumn
();
int
lastColumn
=
ca
.
getLastColumn
();
int
firstRow
=
ca
.
getFirstRow
();
int
firstRow
=
ca
.
getFirstRow
();
int
lastRow
=
ca
.
getLastRow
();
int
lastRow
=
ca
.
getLastRow
();
if
(
row
>=
firstRow
&&
row
<=
lastRow
)
{
if
(
row
>=
firstRow
&&
row
<=
lastRow
)
{
if
(
column
>=
firstColumn
&&
column
<=
lastColumn
)
{
if
(
column
>=
firstColumn
&&
column
<=
lastColumn
)
{
Row
fRow
=
sheet
.
getRow
(
firstRow
);
Row
fRow
=
sheet
.
getRow
(
firstRow
);
...
@@ -228,19 +133,16 @@ public class ImportExcelServiceImpl implements IImportExcelService {
...
@@ -228,19 +133,16 @@ public class ImportExcelServiceImpl implements IImportExcelService {
}
}
}
}
}
}
return
null
;
return
null
;
}
}
/**
/**
* 判断指定的单元格是否是合并单元格
* 判断指定的单元格是否是合并单元格
*
* @param sheet
* @param sheet
* @param row 行下标
* @param row 行下标
* @param column 列下标
* @param column 列下标
* @return
* @return
*/
*/
private
boolean
isMergedRegion
(
Sheet
sheet
,
int
row
,
int
column
)
{
private
boolean
isMergedRegion
(
Sheet
sheet
,
int
row
,
int
column
)
{
int
sheetMergeCount
=
sheet
.
getNumMergedRegions
();
int
sheetMergeCount
=
sheet
.
getNumMergedRegions
();
for
(
int
i
=
0
;
i
<
sheetMergeCount
;
i
++)
{
for
(
int
i
=
0
;
i
<
sheetMergeCount
;
i
++)
{
...
@@ -257,5 +159,4 @@ public class ImportExcelServiceImpl implements IImportExcelService {
...
@@ -257,5 +159,4 @@ public class ImportExcelServiceImpl implements IImportExcelService {
}
}
return
false
;
return
false
;
}
}
}
}
\ No newline at end of file
\ No newline at end of file
nafmii-admin/src/main/resources/mapper/ImportExcelMapper.xml
View file @
a5f04db
...
@@ -3,8 +3,7 @@
...
@@ -3,8 +3,7 @@
<mapper
namespace=
"org.nafmii.admin.user.dao.ImportExcelMapper"
>
<mapper
namespace=
"org.nafmii.admin.user.dao.ImportExcelMapper"
>
<!-- 通用查询映射结果 -->
<!-- 通用查询映射结果 -->
<resultMap
id=
"BaseResultMap"
type=
"org.nafmii.admin.user.entity.ImportExcelPO"
>
<resultMap
id=
"BaseResultMap"
type=
"org.nafmii.admin.user.dto.ImportExcelDto"
>
<id
column=
"ID"
property=
"id"
/>
<result
column=
"COLUMN1"
property=
"column1"
/>
<result
column=
"COLUMN1"
property=
"column1"
/>
<result
column=
"COLUMN2"
property=
"column2"
/>
<result
column=
"COLUMN2"
property=
"column2"
/>
<result
column=
"COLUMN3"
property=
"column3"
/>
<result
column=
"COLUMN3"
property=
"column3"
/>
...
@@ -13,9 +12,9 @@
...
@@ -13,9 +12,9 @@
<insert
id =
"insertExcelData"
parameterType=
"org.nafmii.admin.user.dto.ImportExcelDto"
>
<insert
id =
"insertExcelData"
parameterType=
"org.nafmii.admin.user.dto.ImportExcelDto"
>
insert into IMPORT_EXCEL_TEST
insert into IMPORT_EXCEL_TEST
(COLUMN1,COLUMN2,COLUMN3)
(COLUMN1,COLUMN2,COLUMN3)
values(#{
ImportExcelPO
.column1},
values(#{
data
.column1},
#{
ImportExcelPO
.column2},
#{
data
.column2},
#{
ImportExcelPO
.column3})
#{
data
.column3})
</insert >
</insert >
</mapper>
</mapper>
Write
Preview
Markdown
is supported
Attach a file
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 post a comment