Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nnjcy-data-model
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
梁家彪
nnjcy-data-model
Commits
de5c100d
Commit
de5c100d
authored
Jan 11, 2024
by
黄明步
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加获取Excel表头和数据的方法
parent
c950ea6e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
129 additions
and
3 deletions
+129
-3
model-server/src/main/java/com.zq.model/util/MyExcelUtil.java
+129
-3
No files found.
model-server/src/main/java/com.zq.model/util/MyExcelUtil.java
View file @
de5c100d
package
com
.
zq
.
model
.
util
;
package
com
.
zq
.
model
.
util
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.poi.excel.cell.CellUtil
;
import
com.zq.common.exception.BusinessException
;
import
com.zq.common.exception.BusinessException
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.ss.util.CellRangeAddress
;
import
org.apache.poi.xssf.usermodel.XSSFSheet
;
import
org.apache.poi.xssf.usermodel.XSSFSheet
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.text.DecimalFormat
;
import
java.text.DecimalFormat
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.List
;
/**
/**
* @program: instrument-cloud
* @program: instrument-cloud
...
@@ -81,4 +83,127 @@ public class MyExcelUtil {
...
@@ -81,4 +83,127 @@ public class MyExcelUtil {
}
}
return
value
;
return
value
;
}
}
/**
* 获取Excel的表头
*
* @param excelFilePath 文件路径
* @param sheetIndex 工作表索引
* @return {@link List}<{@link String}>
*/
public
static
List
<
String
>
getHeaders
(
String
excelFilePath
,
int
sheetIndex
)
{
List
<
String
>
headers
=
new
ArrayList
<>();
try
(
FileInputStream
file
=
new
FileInputStream
(
excelFilePath
))
{
Workbook
workbook
=
WorkbookFactory
.
create
(
file
);
Sheet
sheet
=
workbook
.
getSheetAt
(
sheetIndex
);
int
rowIndex
=
getFirstUnmergedRowIndex
(
sheet
);
Row
headerRow
=
sheet
.
getRow
(
rowIndex
);
for
(
int
i
=
0
;
i
<
headerRow
.
getLastCellNum
();
i
++)
{
Cell
cell
=
headerRow
.
getCell
(
i
);
String
header
=
CellUtil
.
getCellValue
(
cell
).
toString
();
headers
.
add
(
header
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
headers
;
}
/**
* 获取表格数据,Key-Value形式,Key为表头,Value为单元格的值
*
* @param excelFilePath 文件路径
* @param sheetIndex 工作表索引
* @return {@link List}<{@link Map}<{@link String}, {@link String}>>
*/
public
static
List
<
Map
<
String
,
String
>>
getData
(
String
excelFilePath
,
int
sheetIndex
)
{
List
<
Map
<
String
,
String
>>
data
=
new
ArrayList
<>();
try
(
FileInputStream
file
=
new
FileInputStream
(
excelFilePath
))
{
Workbook
workbook
=
WorkbookFactory
.
create
(
file
);
Sheet
sheet
=
workbook
.
getSheetAt
(
sheetIndex
);
int
headerIndex
=
getFirstUnmergedRowIndex
(
sheet
);
Row
headerRow
=
sheet
.
getRow
(
headerIndex
);
for
(
int
rowIndex
=
headerIndex
+
1
;
rowIndex
<
sheet
.
getPhysicalNumberOfRows
();
rowIndex
++)
{
Row
row
=
sheet
.
getRow
(
rowIndex
);
// 检查行中的所有值是否为空
boolean
allValuesEmpty
=
true
;
for
(
int
cellIndex
=
0
;
cellIndex
<
headerRow
.
getLastCellNum
();
cellIndex
++)
{
Cell
dataCell
=
row
.
getCell
(
cellIndex
);
if
(
dataCell
!=
null
&&
!
CellUtil
.
getCellValue
(
dataCell
).
toString
().
trim
().
isEmpty
())
{
allValuesEmpty
=
false
;
break
;
}
}
if
(!
allValuesEmpty
)
{
Map
<
String
,
String
>
rowMap
=
new
HashMap
<>();
for
(
int
cellIndex
=
0
;
cellIndex
<
headerRow
.
getLastCellNum
();
cellIndex
++)
{
Cell
headerCell
=
headerRow
.
getCell
(
cellIndex
);
Cell
dataCell
=
row
.
getCell
(
cellIndex
);
String
header
=
CellUtil
.
getCellValue
(
headerCell
).
toString
();
String
value
=
(
dataCell
==
null
)
?
""
:
CellUtil
.
getCellValue
(
dataCell
).
toString
();
rowMap
.
put
(
header
,
value
);
}
data
.
add
(
rowMap
);
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
data
;
}
/**
* 获取第一个没有合并单元格的行坐标
*
* @param sheet
* @return int
*/
private
static
int
getFirstUnmergedRowIndex
(
Sheet
sheet
)
{
int
lastRowNum
=
sheet
.
getLastRowNum
();
for
(
int
i
=
0
;
i
<=
lastRowNum
;
i
++)
{
Row
row
=
sheet
.
getRow
(
i
);
if
(
row
!=
null
)
{
boolean
hasMergedCells
=
hasMergedCellsInRow
(
row
);
// 如果当前行没有合并单元格,返回当前行的索引
if
(!
hasMergedCells
)
{
return
i
;
}
}
}
// 如果所有行都有合并单元格,返回最后一行的索引
return
lastRowNum
;
}
/**
* 判断该行是否包含合并单元格
*
* @param row
* @return boolean
*/
private
static
boolean
hasMergedCellsInRow
(
Row
row
)
{
for
(
int
i
=
0
;
i
<
row
.
getSheet
().
getNumMergedRegions
();
i
++)
{
CellRangeAddress
mergedRegion
=
row
.
getSheet
().
getMergedRegion
(
i
);
if
(
mergedRegion
.
getFirstRow
()
==
row
.
getRowNum
())
{
return
true
;
}
}
return
false
;
}
}
}
\ No newline at end of file
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