Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
zh-baseversion-project
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
王浩
zh-baseversion-project
Commits
f6da0235
Commit
f6da0235
authored
Jun 03, 2024
by
军师中郎将
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1 从业人员 - 导出 - 模版全局为文本格式
parent
b0347b04
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
69 deletions
+27
-69
ExcelUtil.java
.../src/main/java/com/zehong/common/utils/poi/ExcelUtil.java
+27
-69
No files found.
zh-baseversion-common/src/main/java/com/zehong/common/utils/poi/ExcelUtil.java
View file @
f6da0235
...
...
@@ -18,25 +18,8 @@ import java.util.Map;
import
java.util.Set
;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
import
org.apache.poi.ss.usermodel.BorderStyle
;
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.ClientAnchor
;
import
org.apache.poi.ss.usermodel.DataValidation
;
import
org.apache.poi.ss.usermodel.DataValidationConstraint
;
import
org.apache.poi.ss.usermodel.DataValidationHelper
;
import
org.apache.poi.ss.usermodel.DateUtil
;
import
org.apache.poi.ss.usermodel.Drawing
;
import
org.apache.poi.ss.usermodel.FillPatternType
;
import
org.apache.poi.ss.usermodel.Font
;
import
org.apache.poi.ss.usermodel.HorizontalAlignment
;
import
org.apache.poi.ss.usermodel.IndexedColors
;
import
org.apache.poi.ss.usermodel.Row
;
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.WorkbookFactory
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.ss.util.CellRangeAddressList
;
import
org.apache.poi.xssf.streaming.SXSSFWorkbook
;
import
org.apache.poi.xssf.usermodel.XSSFClientAnchor
;
...
...
@@ -349,55 +332,6 @@ public class ExcelUtil<T>
this
.
init
(
null
,
sheetName
,
Type
.
IMPORT
);
return
exportExcel
();
}
/**
* 对list数据源将其里面的数据导入到excel表单 - 只返回 outputstream
*
* @return 结果
*/
public
OutputStream
importTemplateExcelOutputStream
()
{
this
.
init
(
null
,
sheetName
,
Type
.
IMPORT
);
return
exportExcelOutputStream
();
}
/**
* 对list数据源将其里面的数据导入到excel表单- 只返回 outputstream
*
* @return 结果
*/
public
OutputStream
exportExcelOutputStream
(){
OutputStream
out
=
null
;
try
{
// 取出一共有多少个sheet.
double
sheetNo
=
Math
.
ceil
(
list
.
size
()
/
sheetSize
);
for
(
int
index
=
0
;
index
<=
sheetNo
;
index
++)
{
createSheet
(
sheetNo
,
index
);
// 产生一行
Row
row
=
sheet
.
createRow
(
0
);
int
column
=
0
;
// 写入各个字段的列头名称
for
(
Object
[]
os
:
fields
)
{
Excel
excel
=
(
Excel
)
os
[
1
];
this
.
createCell
(
excel
,
row
,
column
++);
}
if
(
Type
.
EXPORT
.
equals
(
type
))
{
fillExcelData
(
index
,
row
);
addStatisticsRow
();
}
}
String
filename
=
encodingFilename
(
sheetName
);
out
=
new
FileOutputStream
(
getAbsoluteFile
(
filename
));
}
catch
(
Exception
e
)
{
log
.
error
(
"导出Excel异常{}"
,
e
.
getMessage
());
throw
new
CustomException
(
"导出Excel失败,请联系网站管理员!"
);
}
return
out
;
}
/**
* 对list数据源将其里面的数据导入到excel表单
*
...
...
@@ -416,12 +350,15 @@ public class ExcelUtil<T>
// 产生一行
Row
row
=
sheet
.
createRow
(
0
);
int
column
=
0
;
int
column
=
0
,
tempColumn
=
0
;
// 写入各个字段的列头名称
for
(
Object
[]
os
:
fields
)
{
Excel
excel
=
(
Excel
)
os
[
1
];
this
.
createCell
(
excel
,
row
,
column
++);
// 设置列默认格式
this
.
createDefaultColumnStyle
(
wb
,
tempColumn
++);
}
if
(
Type
.
EXPORT
.
equals
(
type
))
{
...
...
@@ -504,6 +441,7 @@ public class ExcelUtil<T>
// 写入各条记录,每条记录对应excel表中的一行
Map
<
String
,
CellStyle
>
styles
=
new
HashMap
<
String
,
CellStyle
>();
CellStyle
style
=
wb
.
createCellStyle
();
DataFormat
excelFormat
=
wb
.
createDataFormat
();
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
style
.
setBorderRight
(
BorderStyle
.
THIN
);
...
...
@@ -518,6 +456,7 @@ public class ExcelUtil<T>
dataFont
.
setFontName
(
"Arial"
);
dataFont
.
setFontHeightInPoints
((
short
)
10
);
style
.
setFont
(
dataFont
);
style
.
setDataFormat
(
excelFormat
.
getFormat
(
"@"
));
styles
.
put
(
"data"
,
style
);
style
=
wb
.
createCellStyle
();
...
...
@@ -575,6 +514,25 @@ public class ExcelUtil<T>
return
cell
;
}
/**
* 创建默认列样式
*
* @param wb 工作薄对象
* @param column 列
*/
private
void
createDefaultColumnStyle
(
Workbook
wb
,
int
column
)
{
// 设置默认输入类型为文本
CellStyle
style
=
wb
.
createCellStyle
();
DataFormat
excelFormat
=
wb
.
createDataFormat
();
// 自动换行
style
.
setWrapText
(
true
);
// 文本格式
style
.
setDataFormat
(
excelFormat
.
getFormat
(
"@"
));
sheet
.
setDefaultColumnStyle
(
column
,
style
);
}
/**
* 设置单元格信息
*
...
...
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