Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fgqyxxlr
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
yaru
fgqyxxlr
Commits
84068a2e
Commit
84068a2e
authored
Sep 04, 2018
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
升级poi到最新版3.17
parent
1cb672f8
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
24 deletions
+58
-24
pom.xml
+1
-1
src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+57
-23
No files found.
pom.xml
View file @
84068a2e
...
@@ -238,7 +238,7 @@
...
@@ -238,7 +238,7 @@
<dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml
</artifactId>
<artifactId>
poi-ooxml
</artifactId>
<version>
3.
9
</version>
<version>
3.
17
</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
...
...
src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
View file @
84068a2e
...
@@ -13,6 +13,7 @@ import java.util.HashMap;
...
@@ -13,6 +13,7 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.UUID
;
import
java.util.UUID
;
import
org.apache.poi.hssf.usermodel.DVConstraint
;
import
org.apache.poi.hssf.usermodel.DVConstraint
;
import
org.apache.poi.hssf.usermodel.HSSFCell
;
import
org.apache.poi.hssf.usermodel.HSSFCell
;
import
org.apache.poi.hssf.usermodel.HSSFCellStyle
;
import
org.apache.poi.hssf.usermodel.HSSFCellStyle
;
...
@@ -21,17 +22,21 @@ import org.apache.poi.hssf.usermodel.HSSFFont;
...
@@ -21,17 +22,21 @@ import org.apache.poi.hssf.usermodel.HSSFFont;
import
org.apache.poi.hssf.usermodel.HSSFRow
;
import
org.apache.poi.hssf.usermodel.HSSFRow
;
import
org.apache.poi.hssf.usermodel.HSSFSheet
;
import
org.apache.poi.hssf.usermodel.HSSFSheet
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.hssf.util.HSSFColor
;
import
org.apache.poi.hssf.util.HSSFColor
.HSSFColorPredefined
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.CellStyle
;
import
org.apache.poi.ss.usermodel.CellType
;
import
org.apache.poi.ss.usermodel.FillPatternType
;
import
org.apache.poi.ss.usermodel.HorizontalAlignment
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.VerticalAlignment
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.usermodel.WorkbookFactory
;
import
org.apache.poi.ss.usermodel.WorkbookFactory
;
import
org.apache.poi.ss.util.CellRangeAddressList
;
import
org.apache.poi.ss.util.CellRangeAddressList
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.util.ResourceUtils
;
import
org.springframework.util.ResourceUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.framework.aspectj.lang.annotation.Excel
;
import
com.ruoyi.framework.aspectj.lang.annotation.Excel
;
import
com.ruoyi.framework.shiro.web.session.OnlineWebSessionManager
;
import
com.ruoyi.framework.shiro.web.session.OnlineWebSessionManager
;
...
@@ -98,6 +103,8 @@ public class ExcelUtil<T>
...
@@ -98,6 +103,8 @@ public class ExcelUtil<T>
if
(
rows
>
0
)
if
(
rows
>
0
)
{
{
// 默认序号
int
serialNum
=
0
;
// 有数据时才处理 得到类的所有field.
// 有数据时才处理 得到类的所有field.
Field
[]
allFields
=
clazz
.
getDeclaredFields
();
Field
[]
allFields
=
clazz
.
getDeclaredFields
();
// 定义一个map用于存放列的序号和field.
// 定义一个map用于存放列的序号和field.
...
@@ -110,14 +117,14 @@ public class ExcelUtil<T>
...
@@ -110,14 +117,14 @@ public class ExcelUtil<T>
{
{
// 设置类的私有字段属性可访问.
// 设置类的私有字段属性可访问.
field
.
setAccessible
(
true
);
field
.
setAccessible
(
true
);
fieldsMap
.
put
(
col
,
field
);
fieldsMap
.
put
(
++
serialNum
,
field
);
}
}
}
}
for
(
int
i
=
1
;
i
<
rows
;
i
++)
for
(
int
i
=
1
;
i
<
rows
;
i
++)
{
{
// 从第2行开始取数据,默认第一行是表头.
// 从第2行开始取数据,默认第一行是表头.
Row
row
=
sheet
.
getRow
(
i
);
Row
row
=
sheet
.
getRow
(
i
);
int
cellNum
=
s
heet
.
getRow
(
0
).
getPhysicalNumberOfCells
()
;
int
cellNum
=
s
erialNum
;
T
entity
=
null
;
T
entity
=
null
;
for
(
int
j
=
0
;
j
<
cellNum
;
j
++)
for
(
int
j
=
0
;
j
<
cellNum
;
j
++)
{
{
...
@@ -129,7 +136,7 @@ public class ExcelUtil<T>
...
@@ -129,7 +136,7 @@ public class ExcelUtil<T>
else
else
{
{
// 先设置Cell的类型,然后就可以把纯数字作为String类型读进来了
// 先设置Cell的类型,然后就可以把纯数字作为String类型读进来了
row
.
getCell
(
j
).
setCellType
(
Cell
.
CELL_TYPE_
STRING
);
row
.
getCell
(
j
).
setCellType
(
Cell
Type
.
STRING
);
cell
=
row
.
getCell
(
j
);
cell
=
row
.
getCell
(
j
);
}
}
...
@@ -178,7 +185,7 @@ public class ExcelUtil<T>
...
@@ -178,7 +185,7 @@ public class ExcelUtil<T>
}
}
else
if
(
java
.
util
.
Date
.
class
==
fieldType
)
else
if
(
java
.
util
.
Date
.
class
==
fieldType
)
{
{
if
(
cell
.
getCellType
()
==
Cell
.
CELL_TYPE_
NUMERIC
)
if
(
cell
.
getCellType
Enum
()
==
CellType
.
NUMERIC
)
{
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
cell
.
setCellValue
(
sdf
.
format
(
cell
.
getNumericCellValue
()));
cell
.
setCellValue
(
sdf
.
format
(
cell
.
getNumericCellValue
()));
...
@@ -211,6 +218,11 @@ public class ExcelUtil<T>
...
@@ -211,6 +218,11 @@ public class ExcelUtil<T>
*/
*/
public
AjaxResult
exportExcel
(
List
<
T
>
list
,
String
sheetName
)
public
AjaxResult
exportExcel
(
List
<
T
>
list
,
String
sheetName
)
{
{
OutputStream
out
=
null
;
HSSFWorkbook
workbook
=
null
;
try
{
// 得到所有定义字段
// 得到所有定义字段
Field
[]
allFields
=
clazz
.
getDeclaredFields
();
Field
[]
allFields
=
clazz
.
getDeclaredFields
();
List
<
Field
>
fields
=
new
ArrayList
<
Field
>();
List
<
Field
>
fields
=
new
ArrayList
<
Field
>();
...
@@ -224,7 +236,7 @@ public class ExcelUtil<T>
...
@@ -224,7 +236,7 @@ public class ExcelUtil<T>
}
}
// 产生工作薄对象
// 产生工作薄对象
HSSFWorkbook
workbook
=
new
HSSFWorkbook
();
workbook
=
new
HSSFWorkbook
();
// excel2003中每个sheet中最多有65536行
// excel2003中每个sheet中最多有65536行
int
sheetSize
=
65536
;
int
sheetSize
=
65536
;
// 取出一共有多少个sheet.
// 取出一共有多少个sheet.
...
@@ -255,30 +267,30 @@ public class ExcelUtil<T>
...
@@ -255,30 +267,30 @@ public class ExcelUtil<T>
// 创建列
// 创建列
cell
=
row
.
createCell
(
i
);
cell
=
row
.
createCell
(
i
);
// 设置列中写入内容为String类型
// 设置列中写入内容为String类型
cell
.
setCellType
(
HSSFCell
.
CELL_TYPE_
STRING
);
cell
.
setCellType
(
CellType
.
STRING
);
HSSFCellStyle
cellStyle
=
workbook
.
createCellStyle
();
HSSFCellStyle
cellStyle
=
workbook
.
createCellStyle
();
cellStyle
.
setAlignment
(
HSSFCellStyle
.
ALIGN_
CENTER
);
cellStyle
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
cellStyle
.
setVerticalAlignment
(
CellStyle
.
VERTICAL_
CENTER
);
cellStyle
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
if
(
attr
.
name
().
indexOf
(
"注:"
)
>=
0
)
if
(
attr
.
name
().
indexOf
(
"注:"
)
>=
0
)
{
{
HSSFFont
font
=
workbook
.
createFont
();
HSSFFont
font
=
workbook
.
createFont
();
font
.
setColor
(
HSSFFont
.
COLOR_RED
);
font
.
setColor
(
HSSFFont
.
COLOR_RED
);
cellStyle
.
setFont
(
font
);
cellStyle
.
setFont
(
font
);
cellStyle
.
setFillForegroundColor
(
HSSFColor
.
LIGHT_YELLOW
.
index
);
cellStyle
.
setFillForegroundColor
(
HSSFColorPredefined
.
YELLOW
.
getIndex
()
);
sheet
.
setColumnWidth
(
i
,
6000
);
sheet
.
setColumnWidth
(
i
,
6000
);
}
}
else
else
{
{
HSSFFont
font
=
workbook
.
createFont
();
HSSFFont
font
=
workbook
.
createFont
();
// 粗体显示
// 粗体显示
font
.
setBoldweight
(
HSSFFont
.
BOLDWEIGHT_BOLD
);
font
.
setBold
(
true
);
// 选择需要用到的字体格式
// 选择需要用到的字体格式
cellStyle
.
setFont
(
font
);
cellStyle
.
setFont
(
font
);
cellStyle
.
setFillForegroundColor
(
HSSFColor
.
LIGHT_YELLOW
.
index
);
cellStyle
.
setFillForegroundColor
(
HSSFColorPredefined
.
LIGHT_YELLOW
.
getIndex
()
);
// 设置列宽
// 设置列宽
sheet
.
setColumnWidth
(
i
,
3766
);
sheet
.
setColumnWidth
(
i
,
3766
);
}
}
cellStyle
.
setFillPattern
(
HSSFCellStyl
e
.
SOLID_FOREGROUND
);
cellStyle
.
setFillPattern
(
FillPatternTyp
e
.
SOLID_FOREGROUND
);
cellStyle
.
setWrapText
(
true
);
cellStyle
.
setWrapText
(
true
);
cell
.
setCellStyle
(
cellStyle
);
cell
.
setCellStyle
(
cellStyle
);
...
@@ -303,8 +315,8 @@ public class ExcelUtil<T>
...
@@ -303,8 +315,8 @@ public class ExcelUtil<T>
int
endNo
=
Math
.
min
(
startNo
+
sheetSize
,
list
.
size
());
int
endNo
=
Math
.
min
(
startNo
+
sheetSize
,
list
.
size
());
// 写入各条记录,每条记录对应excel表中的一行
// 写入各条记录,每条记录对应excel表中的一行
HSSFCellStyle
cs
=
workbook
.
createCellStyle
();
HSSFCellStyle
cs
=
workbook
.
createCellStyle
();
cs
.
setAlignment
(
HSSFCellStyle
.
ALIGN_
CENTER
);
cs
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
cs
.
setVerticalAlignment
(
CellStyle
.
VERTICAL_
CENTER
);
cs
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
for
(
int
i
=
startNo
;
i
<
endNo
;
i
++)
for
(
int
i
=
startNo
;
i
<
endNo
;
i
++)
{
{
row
=
sheet
.
createRow
(
i
+
1
-
startNo
);
row
=
sheet
.
createRow
(
i
+
1
-
startNo
);
...
@@ -333,12 +345,12 @@ public class ExcelUtil<T>
...
@@ -333,12 +345,12 @@ public class ExcelUtil<T>
}
}
// 如果可以转成数字则导出为数字类型
// 如果可以转成数字则导出为数字类型
BigDecimal
bc
=
new
BigDecimal
(
String
.
valueOf
(
field
.
get
(
vo
)));
BigDecimal
bc
=
new
BigDecimal
(
String
.
valueOf
(
field
.
get
(
vo
)));
cell
.
setCellType
(
HSSFCell
.
CELL_TYPE_
NUMERIC
);
cell
.
setCellType
(
CellType
.
NUMERIC
);
cell
.
setCellValue
(
bc
.
doubleValue
());
cell
.
setCellValue
(
bc
.
doubleValue
());
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
cell
.
setCellType
(
HSSFCell
.
CELL_TYPE_
STRING
);
cell
.
setCellType
(
CellType
.
STRING
);
if
(
vo
==
null
)
if
(
vo
==
null
)
{
{
// 如果数据存在就填入,不存在填入空格.
// 如果数据存在就填入,不存在填入空格.
...
@@ -360,19 +372,41 @@ public class ExcelUtil<T>
...
@@ -360,19 +372,41 @@ public class ExcelUtil<T>
}
}
}
}
}
}
try
{
String
filename
=
encodingFilename
(
sheetName
);
String
filename
=
encodingFilename
(
sheetName
);
OutputStream
out
=
new
FileOutputStream
(
getfile
()
+
filename
);
out
=
new
FileOutputStream
(
getfile
()
+
filename
);
workbook
.
write
(
out
);
workbook
.
write
(
out
);
out
.
close
();
return
AjaxResult
.
success
(
filename
);
return
AjaxResult
.
success
(
filename
);
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
log
.
error
(
"
关闭flush失败
{}"
,
e
.
getMessage
());
log
.
error
(
"
导出Excel异常
{}"
,
e
.
getMessage
());
return
AjaxResult
.
error
(
"导出Excel失败,请联系网站管理员!"
);
return
AjaxResult
.
error
(
"导出Excel失败,请联系网站管理员!"
);
}
}
finally
{
if
(
workbook
!=
null
)
{
try
{
workbook
.
close
();
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
}
if
(
out
!=
null
)
{
try
{
out
.
close
();
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
}
}
}
}
/**
/**
...
...
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