Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
b6b4e06d
Commit
b6b4e06d
authored
Feb 28, 2023
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
EXCEL自定义列处理
parent
b53bbf1a
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
79 additions
and
16 deletions
+79
-16
StringHelper.java
...c/main/java/com/yanzuoguang/util/helper/StringHelper.java
+43
-5
ExcelConsole.java
...-db/src/main/java/com/yanzuoguang/excel/ExcelConsole.java
+27
-4
ExcelDefineCell.java
.../src/main/java/com/yanzuoguang/excel/ExcelDefineCell.java
+3
-3
ExcelRow.java
...util-db/src/main/java/com/yanzuoguang/excel/ExcelRow.java
+2
-1
ExcelRowDefault.java
.../src/main/java/com/yanzuoguang/excel/ExcelRowDefault.java
+4
-3
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/StringHelper.java
View file @
b6b4e06d
...
...
@@ -28,6 +28,7 @@ public class StringHelper {
public
static
final
String
TYPE_DOUBLE
=
"double"
;
public
static
final
String
TYPE_INT
=
"int"
;
public
static
final
String
TYPE_LONG
=
"long"
;
public
static
final
String
TYPE_SHORT
=
"short"
;
public
static
final
String
TYPE_BOOL
=
"boolean"
;
public
static
final
String
TYPE_OBJECT
=
"System.Object"
;
public
static
final
String
UNDER_FLAG
=
"_"
;
...
...
@@ -410,6 +411,8 @@ public class StringHelper {
to
=
toBoolean
(
strValue
);
}
else
if
(
isBaseType
(
TYPE_INT
,
Integer
.
class
,
fromCls
,
from
))
{
to
=
toInt
(
from
);
}
else
if
(
isBaseType
(
TYPE_SHORT
,
Short
.
class
,
fromCls
,
from
))
{
to
=
toShort
(
from
);
}
else
if
(
isBaseType
(
TYPE_LONG
,
Long
.
class
,
fromCls
,
from
))
{
to
=
toLong
(
from
);
}
else
if
(
isBaseType
(
TYPE_DOUBLE
,
Double
.
class
,
fromCls
,
from
))
{
...
...
@@ -465,6 +468,45 @@ public class StringHelper {
return
isType
(
fromCls
,
cls
);
}
/**
* 是否是浮点数
*
* @param obj 对象
* @return 是否是浮点数
*/
public
static
boolean
isInt
(
Object
obj
)
{
Class
<?>
fromCls
=
obj
!=
null
?
obj
.
getClass
()
:
Object
.
class
;
boolean
isInt
=
StringHelper
.
isBaseType
(
StringHelper
.
TYPE_INT
,
Integer
.
class
,
fromCls
,
obj
);
boolean
isShort
=
StringHelper
.
isBaseType
(
StringHelper
.
TYPE_SHORT
,
Short
.
class
,
fromCls
,
obj
);
boolean
isLong
=
StringHelper
.
isBaseType
(
StringHelper
.
TYPE_LONG
,
Long
.
class
,
fromCls
,
obj
);
return
isInt
||
isShort
||
isLong
;
}
/**
* 是否是浮点数
*
* @param obj 对象
* @return 是否是浮点数
*/
public
static
boolean
isDouble
(
Object
obj
)
{
Class
<?>
fromCls
=
obj
!=
null
?
obj
.
getClass
()
:
Object
.
class
;
boolean
isDouble
=
StringHelper
.
isBaseType
(
StringHelper
.
TYPE_DOUBLE
,
Double
.
class
,
fromCls
,
obj
);
boolean
isFloat
=
StringHelper
.
isBaseType
(
StringHelper
.
TYPE_FLOAT
,
Float
.
class
,
fromCls
,
obj
);
return
obj
instanceof
BigDecimal
||
isDouble
||
isFloat
;
}
/**
* 是否是浮点数
*
* @param obj 对象
* @return 是否是浮点数
*/
public
static
boolean
isNumber
(
Object
obj
)
{
return
isInt
(
obj
)
||
isDouble
(
obj
);
}
/**
* 将 object 转换为String
*
...
...
@@ -472,11 +514,7 @@ public class StringHelper {
* @return 转换成功的字符串
*/
public
static
String
toString
(
Object
obj
)
{
Class
<?>
fromCls
=
obj
!=
null
?
obj
.
getClass
()
:
Object
.
class
;
boolean
isDouble
=
StringHelper
.
isBaseType
(
StringHelper
.
TYPE_DOUBLE
,
Double
.
class
,
fromCls
,
obj
);
boolean
isFromDouble
=
obj
instanceof
BigDecimal
||
isDouble
;
if
(
isFromDouble
)
{
if
(
isDouble
(
obj
))
{
// 格式化设置(显示所有整数部分,小数点保留2位)
DecimalFormat
decimalFormat
=
new
DecimalFormat
(
"#.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
);
String
to
=
decimalFormat
.
format
(
obj
);
...
...
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelConsole.java
View file @
b6b4e06d
...
...
@@ -296,7 +296,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
sheetRow
.
setHeight
(
getUnit
(
row
.
getHeight
()));
for
(
ExcelDefineCell
cell
:
row
.
getCells
())
{
cell
.
cellStyle
=
createCellStyle
(
cell
);
cell
.
cell
=
createCell
(
sheetRow
,
cell
.
getColumn
(),
cell
.
getValue
(),
cell
.
cellStyle
);
Object
value
=
cell
.
getValue
();
if
(
value
!=
null
)
{
cell
.
cell
=
createCell
(
sheetRow
,
cell
.
getColumn
(),
StringHelper
.
isNumber
(
value
),
StringHelper
.
toString
(
value
),
cell
.
cellStyle
);
}
// 合并单元格
if
(
cell
.
getWidthSize
()
>
1
||
cell
.
getHeightSize
()
>
1
)
{
int
rowStart
=
rowIndex
;
...
...
@@ -382,7 +386,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 写入本行内容
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
String
columnName
=
column
.
getName
();
String
value
=
StringHelper
.
getFirst
(
rowHandle
.
get
(
t
,
columnName
));
Object
columnValue
=
rowHandle
.
get
(
t
,
columnName
);
String
value
=
StringHelper
.
getFirst
(
String
.
valueOf
(
columnValue
));
// 合并組数据处理
if
(
column
.
isMerger
())
{
sb
.
append
(
value
.
replace
(
":"
,
"::"
));
...
...
@@ -400,7 +405,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 当前行行高
height
=
Math
.
max
(
nowCellHeight
,
height
);
// 当不需要合并历史记录时,则创建新的内容
createCell
(
row
,
column
.
columnIndex
,
value
,
column
.
cellStyle
);
createCell
(
row
,
column
.
columnIndex
,
StringHelper
.
isNumber
(
columnValue
),
value
,
column
.
cellStyle
);
// 合并列
mergerColumn
(
column
,
column
.
columnIndex
,
false
);
}
...
...
@@ -563,9 +568,27 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
* @return 单元格
*/
private
Cell
createCell
(
Row
row
,
int
column
,
String
content
,
CellStyle
cellStyle
)
{
return
createCell
(
row
,
column
,
false
,
content
,
cellStyle
);
}
/**
* 创建单元格
*
* @param row 行
* @param column 列
* @param isNumber 是否是数字
* @param content 内容
* @param cellStyle 设置默认样式
* @return 单元格
*/
private
Cell
createCell
(
Row
row
,
int
column
,
boolean
isNumber
,
String
content
,
CellStyle
cellStyle
)
{
// 获取字节数、用于设置最大宽度
Cell
cell
=
row
.
createCell
(
column
);
if
(
isNumber
)
{
cell
.
setCellValue
(
StringHelper
.
toDouble
(
content
));
}
else
{
cell
.
setCellValue
(
content
);
}
cell
.
setCellStyle
(
cellStyle
);
return
cell
;
}
...
...
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelDefineCell.java
View file @
b6b4e06d
...
...
@@ -39,7 +39,7 @@ public class ExcelDefineCell {
* 文字内容
*/
@ApiModelProperty
(
notes
=
"文字内容"
)
private
String
value
;
private
Object
value
;
/**
* 公式,如:SUM(C2:C3),或者SUM({columnName}),公式优先.不能SUM(columnA/columnB)。
* 数据列={columnName_COL},指的是数据列名称对应转换的Excel列名,
...
...
@@ -104,11 +104,11 @@ public class ExcelDefineCell {
this
.
verticalAlignment
=
verticalAlignment
;
}
public
String
getValue
()
{
public
Object
getValue
()
{
return
value
;
}
public
void
setValue
(
String
value
)
{
public
void
setValue
(
Object
value
)
{
this
.
value
=
value
;
}
...
...
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelRow.java
View file @
b6b4e06d
...
...
@@ -2,6 +2,7 @@ package com.yanzuoguang.excel;
/**
* 获取行中的某列数据
*
* @param <T>
* @author 颜佐光
*/
...
...
@@ -14,5 +15,5 @@ public interface ExcelRow<T> {
* @param field 列
* @return
*/
String
get
(
T
row
,
String
field
);
Object
get
(
T
row
,
String
field
);
}
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelRowDefault.java
View file @
b6b4e06d
...
...
@@ -5,6 +5,7 @@ import com.yanzuoguang.util.base.ObjectHelper;
/**
* 行数据默认处理
*
* @author 颜佐光
*/
public
class
ExcelRowDefault
<
T
>
implements
ExcelRow
<
T
>
{
...
...
@@ -14,10 +15,10 @@ public class ExcelRowDefault<T> implements ExcelRow<T> {
*
* @param row 行
* @param field 列
* @return
* @return
列值
*/
@Override
public
String
get
(
T
row
,
String
field
)
{
return
ObjectHelper
.
get
String
(
row
,
field
);
public
Object
get
(
T
row
,
String
field
)
{
return
ObjectHelper
.
get
(
row
,
field
);
}
}
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