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 dd8474c6
authored
May 06, 2021
by
孙长庆
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
批量导入功能初步实现
1 parent
ae1e7e28
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
281 additions
and
0 deletions
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/service/impl/ImportExcelServiceImpl.java
0 → 100644
View file @
dd8474c
package
org
.
nafmii
.
admin
.
user
.
service
.
impl
;
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.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.util.CellRangeAddress
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
org.nafmii.admin.user.service.IImportExcelService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.annotation.Resource
;
import
java.util.List
;
/**
* <p>
* 中心端-用户表 服务实现类
* </p>
*
* @author suncq
* @since 2021-04-27
*/
@Slf4j
@Service
public
class
ImportExcelServiceImpl
implements
IImportExcelService
{
@Resource
private
ImportExcelMapper
importExcelMapper
;
@Override
public
ApiResponse
batchImportExcel
(
List
<
ImportExcelDto
>
list
)
{
int
i
=
importExcelMapper
.
insertExcelData
(
list
);
if
(
i
<
0
)
{
return
new
ApiResponse
(
ApiResponse
.
FAIL
,
ApiResponse
.
FAIL_TEXT
);
}
else
{
return
new
ApiResponse
(
ApiResponse
.
SUCCESS
,
ApiResponse
.
SUCCESS_TEXT
);
}
}
public
String
importExcel
(
InputStream
inputStream
,
String
fileName
)
throws
Exception
{
String
message
=
"Import success"
;
boolean
isE2007
=
false
;
//判断是否是excel2007格式
if
(
fileName
.
endsWith
(
"xlsx"
))
{
isE2007
=
true
;
}
int
rowIndex
=
0
;
try
{
InputStream
input
=
inputStream
;
//建立输入流
Workbook
wb
;
//根据文件格式(2003或者2007)来初始化
if
(
isE2007
)
{
wb
=
new
XSSFWorkbook
(
input
);
}
else
{
wb
=
new
HSSFWorkbook
(
input
);
}
Sheet
sheet
=
wb
.
getSheetAt
(
0
);
//获得第一个表单
int
rowCount
=
sheet
.
getLastRowNum
()
+
1
;
for
(
int
i
=
1
;
i
<
rowCount
;
i
++)
{
rowIndex
=
i
;
Row
row
;
for
(
int
j
=
0
;
j
<
26
;
j
++)
{
if
(
isMergedRegion
(
sheet
,
i
,
j
))
{
System
.
out
.
print
(
getMergedRegionValue
(
sheet
,
i
,
j
)
+
"\t"
);
}
else
{
row
=
sheet
.
getRow
(
i
);
System
.
out
.
print
(
row
.
getCell
(
j
)
+
"\t"
);
}
}
System
.
out
.
print
(
"\n"
);
}
}
catch
(
Exception
ex
)
{
message
=
"Import failed, please check the data in "
+
rowIndex
+
" rows "
;
}
return
message
;
}
/**
* 获取单元格的值
*
* @param cell
* @return
*/
public
String
getCellValue
(
Cell
cell
)
{
if
(
cell
==
null
)
return
""
;
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 row
* @param column
* @return
*/
public
String
getMergedRegionValue
(
Sheet
sheet
,
int
row
,
int
column
)
{
int
sheetMergeCount
=
sheet
.
getNumMergedRegions
();
for
(
int
i
=
0
;
i
<
sheetMergeCount
;
i
++)
{
CellRangeAddress
ca
=
sheet
.
getMergedRegion
(
i
);
int
firstColumn
=
ca
.
getFirstColumn
();
int
lastColumn
=
ca
.
getLastColumn
();
int
firstRow
=
ca
.
getFirstRow
();
int
lastRow
=
ca
.
getLastRow
();
if
(
row
>=
firstRow
&&
row
<=
lastRow
)
{
if
(
column
>=
firstColumn
&&
column
<=
lastColumn
)
{
Row
fRow
=
sheet
.
getRow
(
firstRow
);
Cell
fCell
=
fRow
.
getCell
(
firstColumn
);
return
getCellValue
(
fCell
);
}
}
}
return
null
;
}
/**
* 判断指定的单元格是否是合并单元格
*
* @param sheet
* @param row 行下标
* @param column 列下标
* @return
*/
private
boolean
isMergedRegion
(
Sheet
sheet
,
int
row
,
int
column
)
{
int
sheetMergeCount
=
sheet
.
getNumMergedRegions
();
for
(
int
i
=
0
;
i
<
sheetMergeCount
;
i
++)
{
CellRangeAddress
range
=
sheet
.
getMergedRegion
(
i
);
int
firstColumn
=
range
.
getFirstColumn
();
int
lastColumn
=
range
.
getLastColumn
();
int
firstRow
=
range
.
getFirstRow
();
int
lastRow
=
range
.
getLastRow
();
if
(
row
>=
firstRow
&&
row
<=
lastRow
)
{
if
(
column
>=
firstColumn
&&
column
<=
lastColumn
)
{
return
true
;
}
}
}
return
false
;
}
}
\ No newline at end of file
nafmii-admin/src/main/resources/mapper/ImportExcelMapper.xml
0 → 100644
View file @
dd8474c
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace=
"org.nafmii.admin.user.dao.ImportExcelMapper"
>
<!-- 通用查询映射结果 -->
<resultMap
id=
"BaseResultMap"
type=
"org.nafmii.admin.user.entity.ImportExcelPO"
>
<id
column=
"ID"
property=
"id"
/>
<result
column=
"COLUMN1"
property=
"column1"
/>
<result
column=
"COLUMN2"
property=
"column2"
/>
<result
column=
"COLUMN3"
property=
"column3"
/>
</resultMap>
<!-- 通用查询结果列 -->
<sql
id=
"Base_Column_List"
>
ID, LOGIN_NAME, LOGIN_PWD, CN_NAME, EN_NAME, PHONE, TELPHONE, SEX, EMAIL, CREATE_USER_ID, UPDATE_USER_ID, CREATE_TIME, UPDATE_TIME, IS_ENABLE
</sql>
<!-- 导入excel测试 -->
<!-- <insert id="insertExcelData" parameterType="org.nafmii.admin.user.entity.ImportExcelPO">
<foreach collection="list" item="item" index="index" separator=",">
INSERT INTO IMPORT_EXCEL_TEST(COLUMN1,COLUMN2,COLUMN3) VALUES (#{item.column1}, #{item.column2}, #{item.column3})
</foreach>
</insert>-->
<insert
id =
"insertExcelData"
parameterType=
"java.util.List"
>
insert into IMPORT_EXCEL_TEST
(COLUMN1,COLUMN2,COLUMN3)
values
<foreach
collection =
"list"
item=
"ImportExcelPO"
index=
"index"
separator =
","
>
(
#{ImportExcelPO.column1}, #{ImportExcelPO.column2},#{ImportExcelPO.column3}
)
</foreach >
</insert >
</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