Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
sts网站
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liyang
sts网站
Commits
3003bb0f
Commit
3003bb0f
authored
Aug 13, 2021
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Excel注解图片导入兼容xls
parent
5139265d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
9 deletions
+53
-9
ExcelUtil.java
...n/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+53
-9
No files found.
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
View file @
3003bb0f
...
...
@@ -19,6 +19,12 @@ import java.util.Set;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.poi.hssf.usermodel.HSSFClientAnchor
;
import
org.apache.poi.hssf.usermodel.HSSFPicture
;
import
org.apache.poi.hssf.usermodel.HSSFPictureData
;
import
org.apache.poi.hssf.usermodel.HSSFShape
;
import
org.apache.poi.hssf.usermodel.HSSFSheet
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ooxml.POIXMLDocumentPart
;
import
org.apache.poi.ss.usermodel.BorderStyle
;
import
org.apache.poi.ss.usermodel.Cell
;
...
...
@@ -185,7 +191,16 @@ public class ExcelUtil<T>
{
throw
new
IOException
(
"文件sheet不存在"
);
}
Map
<
String
,
PictureData
>
pictures
=
getSheetPictrues
((
XSSFSheet
)
sheet
,
(
XSSFWorkbook
)
wb
);
boolean
isXSSFWorkbook
=
!(
wb
instanceof
HSSFWorkbook
);
Map
<
String
,
PictureData
>
pictures
;
if
(
isXSSFWorkbook
)
{
pictures
=
getSheetPictrues07
((
XSSFSheet
)
sheet
,
(
XSSFWorkbook
)
wb
);
}
else
{
pictures
=
getSheetPictrues03
((
HSSFSheet
)
sheet
,
(
HSSFWorkbook
)
wb
);
}
// 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
int
rows
=
sheet
.
getLastRowNum
();
...
...
@@ -318,12 +333,8 @@ public class ExcelUtil<T>
{
val
=
reverseDictByExp
(
Convert
.
toStr
(
val
),
attr
.
dictType
(),
attr
.
separator
());
}
else
if
(
ColumnType
.
IMAGE
==
attr
.
cellType
())
else
if
(
ColumnType
.
IMAGE
==
attr
.
cellType
()
&&
StringUtils
.
isNotEmpty
(
pictures
)
)
{
if
(
StringUtils
.
isNull
(
pictures
))
{
val
=
""
;
}
PictureData
image
=
pictures
.
get
(
row
.
getRowNum
()
+
"_"
+
entry
.
getKey
());
if
(
image
==
null
)
{
...
...
@@ -1179,13 +1190,46 @@ public class ExcelUtil<T>
}
/**
* 获取Excel图片
* 获取Excel2003图片
*
* @param sheet 当前sheet对象
* @param workbook 工作簿对象
* @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
*/
public
static
Map
<
String
,
PictureData
>
getSheetPictrues03
(
HSSFSheet
sheet
,
HSSFWorkbook
workbook
)
{
Map
<
String
,
PictureData
>
sheetIndexPicMap
=
new
HashMap
<
String
,
PictureData
>();
List
<
HSSFPictureData
>
pictures
=
workbook
.
getAllPictures
();
if
(!
pictures
.
isEmpty
())
{
for
(
HSSFShape
shape
:
sheet
.
getDrawingPatriarch
().
getChildren
())
{
HSSFClientAnchor
anchor
=
(
HSSFClientAnchor
)
shape
.
getAnchor
();
if
(
shape
instanceof
HSSFPicture
)
{
HSSFPicture
pic
=
(
HSSFPicture
)
shape
;
int
pictureIndex
=
pic
.
getPictureIndex
()
-
1
;
HSSFPictureData
picData
=
pictures
.
get
(
pictureIndex
);
String
picIndex
=
String
.
valueOf
(
anchor
.
getRow1
())
+
"_"
+
String
.
valueOf
(
anchor
.
getCol1
());
sheetIndexPicMap
.
put
(
picIndex
,
picData
);
}
}
return
sheetIndexPicMap
;
}
else
{
return
sheetIndexPicMap
;
}
}
/**
* 获取Excel2007图片
*
* @param sheet 当前sheet对象
* @param workbook 工作簿对象
* @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
*/
public
static
Map
<
String
,
PictureData
>
getSheetPictrues
(
XSSFSheet
sheet
,
XSSFWorkbook
workbook
)
public
static
Map
<
String
,
PictureData
>
getSheetPictrues
07
(
XSSFSheet
sheet
,
XSSFWorkbook
workbook
)
{
Map
<
String
,
PictureData
>
sheetIndexPicMap
=
new
HashMap
<
String
,
PictureData
>();
for
(
POIXMLDocumentPart
dr
:
sheet
.
getRelations
())
...
...
@@ -1209,4 +1253,4 @@ public class ExcelUtil<T>
}
return
sheetIndexPicMap
;
}
}
\ 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