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
6c1f982f
Commit
6c1f982f
authored
Feb 28, 2023
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
EXCEL自定义列处理
parent
5ea7d103
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
189 additions
and
9 deletions
+189
-9
CellReferenceUtil.java
...rc/main/java/com/yanzuoguang/excel/CellReferenceUtil.java
+134
-0
ExcelConsole.java
...-db/src/main/java/com/yanzuoguang/excel/ExcelConsole.java
+29
-2
ExcelDefineCell.java
.../src/main/java/com/yanzuoguang/excel/ExcelDefineCell.java
+12
-2
ExcelDefineRow.java
...b/src/main/java/com/yanzuoguang/excel/ExcelDefineRow.java
+1
-5
ExcelRowIndex.java
...db/src/main/java/com/yanzuoguang/excel/ExcelRowIndex.java
+13
-0
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/excel/CellReferenceUtil.java
0 → 100644
View file @
6c1f982f
package
com
.
yanzuoguang
.
excel
;
/**
* 单元格引用工具类
*
* @author 颜佐光
*/
public
class
CellReferenceUtil
{
/**
* Input Coordination: C<br/>
* Output Column Number: 2<br/>
*
* @param colName
* @return colIndex: 0 based index
*/
public
static
int
getColIndex
(
String
colName
)
{
colName
=
colName
.
toUpperCase
();
int
value
=
0
;
for
(
int
i
=
0
;
i
<
colName
.
length
();
i
++)
{
int
delta
=
colName
.
charAt
(
i
)
-
64
;
value
=
value
*
26
+
delta
;
}
int
colIndex
=
value
-
1
;
return
colIndex
;
}
/**
* Input column index: 2 <br/>
* Output column name: C <br/>
*
* @param colIndex
* @return
*/
public
static
String
getColName
(
int
colIndex
)
{
int
quotient
=
(
colIndex
)
/
26
;
if
(
quotient
>
0
)
{
return
getColName
(
quotient
-
1
)
+
(
char
)
((
colIndex
%
26
)
+
65
);
}
else
{
return
""
+
(
char
)
((
colIndex
%
26
)
+
65
);
}
}
/**
* Input coord: C8<br/>
* Output Row Number: 7<br/>
*
* @param rowName
* @return rowIndex starts with 0
*/
public
static
int
getRowIndex
(
String
rowName
)
{
int
rowIndex
=
Integer
.
parseInt
(
rowName
)
-
1
;
return
rowIndex
;
}
/**
* Input rowIndex: 7 <br/>
* Output rowName: 8 <br/>
*
* @param rowIndex
* @return
*/
public
static
String
getRowName
(
int
rowIndex
)
{
int
rowName
=
rowIndex
+
1
;
return
String
.
valueOf
(
rowName
);
}
/**
* Input pos: col = 2, row = 7<br/>
* Output coord: C8<br/>
*
* @param colIndex
* @param rowIndex
* @return
*/
public
static
String
getCoordName
(
int
colIndex
,
int
rowIndex
)
{
String
colName
=
getColName
(
colIndex
);
String
rowName
=
getRowName
(
rowIndex
);
return
colName
+
rowName
;
}
/**
* Input coordName: C8 <br/>
* Output colIndex: 2 <br/>
*
* @param coordName
* @return colIndex: Starts with 0
*/
public
static
int
getColIndexByCoordName
(
String
coordName
)
{
String
[]
colAndRowName
=
splitColAndRow
(
coordName
);
String
colName
=
colAndRowName
[
0
];
return
getColIndex
(
colName
);
}
/**
* Input coordName: C8 <br/>
* Output rowIndex: 7 <br/>
*
* @param coordName
* @return rowIndex: 0 based index
*/
public
static
int
getRowIndexByCoordName
(
String
coordName
)
{
String
[]
colAndRowName
=
splitColAndRow
(
coordName
);
String
rowName
=
colAndRowName
[
1
];
return
getRowIndex
(
rowName
);
}
/**
* Input coordName: C8 <br/>
* Output : String[]{C, 8} <br/>
*
* @param coordName
* @return
*/
private
static
String
[]
splitColAndRow
(
String
coordName
)
{
int
rowNumStartIndex
=
0
;
for
(
int
i
=
0
;
i
<
coordName
.
length
();
i
++)
{
char
ch
=
coordName
.
charAt
(
i
);
if
(
Character
.
isDigit
(
ch
))
{
rowNumStartIndex
=
i
;
break
;
}
}
String
colName
=
coordName
.
substring
(
0
,
rowNumStartIndex
);
String
rowName
=
coordName
.
substring
(
rowNumStartIndex
);
return
new
String
[]
{
colName
,
rowName
};
}
}
\ No newline at end of file
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelConsole.java
View file @
6c1f982f
...
@@ -2,6 +2,7 @@ package com.yanzuoguang.excel;
...
@@ -2,6 +2,7 @@ package com.yanzuoguang.excel;
import
com.yanzuoguang.db.impl.DbRow
;
import
com.yanzuoguang.db.impl.DbRow
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.helper.CheckerHelper
;
import
com.yanzuoguang.util.helper.CheckerHelper
;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
...
@@ -18,7 +19,9 @@ import java.io.File;
...
@@ -18,7 +19,9 @@ import java.io.File;
import
java.io.FileOutputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
/**
/**
...
@@ -313,12 +316,36 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -313,12 +316,36 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
}
}
private
void
writeDefineFormula
(
List
<
ExcelDefineRow
>
rows
)
{
private
void
writeDefineFormula
(
List
<
ExcelDefineRow
>
rows
)
{
Map
<
String
,
Object
>
map
=
new
HashMap
<>(
this
.
config
.
getColumns
().
size
()
*
2
+
3
);
// 设置列别名
int
columnPos
=
0
;
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
map
.
put
(
column
.
getName
(),
String
.
format
(
"%s:%s"
,
CellReferenceUtil
.
getCoordName
(
columnPos
,
this
.
startDataRowIndex
),
CellReferenceUtil
.
getCoordName
(
columnPos
,
this
.
endDataRowIndex
)
));
map
.
put
(
column
.
getName
()
+
"_COL"
,
CellReferenceUtil
.
getColName
(
columnPos
));
}
// 数据行开始行号
map
.
put
(
"startDataRowIndex"
,
CellReferenceUtil
.
getRowName
(
this
.
startDataRowIndex
));
// 数据行结束行号
map
.
put
(
"endDataRowIndex"
,
CellReferenceUtil
.
getRowName
(
this
.
endDataRowIndex
));
// 设置行别名
for
(
ExcelDefineRow
row
:
rows
)
{
for
(
ExcelDefineRow
row
:
rows
)
{
// 当前行号
map
.
put
(
"rowIndex"
,
CellReferenceUtil
.
getRowName
(
row
.
rowIndex
));
for
(
ExcelDefineCell
cell
:
row
.
getCells
())
{
for
(
ExcelDefineCell
cell
:
row
.
getCells
())
{
if
(
StringHelper
.
isEmpty
(
cell
.
getFormula
()))
{
String
formula
=
cell
.
getFormula
();
if
(
StringHelper
.
isEmpty
(
formula
))
{
continue
;
continue
;
}
}
formula
=
StringHelper
.
getFormat
(
formula
,
StringHelper
.
EMPTY
,
(
sb
,
group
,
fieldFull
,
field
,
command
)
->
{
String
value
=
StringHelper
.
getFirst
(
ObjectHelper
.
getString
(
map
,
field
));
sb
.
append
(
value
);
});
// 设置公式
cell
.
cell
.
setCellFormula
(
formula
);
}
}
}
}
}
}
...
...
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelDefineCell.java
View file @
6c1f982f
...
@@ -41,9 +41,19 @@ public class ExcelDefineCell {
...
@@ -41,9 +41,19 @@ public class ExcelDefineCell {
@ApiModelProperty
(
notes
=
"文字内容"
)
@ApiModelProperty
(
notes
=
"文字内容"
)
private
String
value
;
private
String
value
;
/**
/**
* 公式,如:SUM(C2:C3),或者SUM({columnName}),columnName=数据列名称,公式优先
* 公式,如:SUM(C2:C3),或者SUM({columnName}),公式优先.不能SUM(columnA/columnB)。
* 数据列={columnName_COL},指的是数据列名称对应转换的Excel列名,
* 当前行={rowIndex},
* 数据开始行={startDataRowIndex},
* 数据结束行={endDataRowIndex},
* 数据所有单元格={columnName}={columnName_COL}{startDataRowIndex}:{columnName_COL}{endDataRowIndex}
*/
*/
@ApiModelProperty
(
notes
=
" 公式,如:SUM(C2:C3),或者SUM({columnName}),columnName=数据列名称,公式优先.不能SUM(columnA/columnB),当前行={rowIndex}"
)
@ApiModelProperty
(
notes
=
" 公式,如:SUM(C2:C3),或者SUM({columnName}),公式优先.不能SUM(columnA/columnB)。"
+
"数据列={columnName_COL},指的是数据列名称对应转换的Excel列名,"
+
"当前行={rowIndex},"
+
"数据开始行={startDataRowIndex},"
+
"数据结束行={endDataRowIndex}"
+
"数据所有单元格={columnName}={columnName_COL}{startDataRowIndex}:{columnName_COL}{endDataRowIndex}"
)
private
String
formula
;
private
String
formula
;
/**
/**
* 当前单元格样式
* 当前单元格样式
...
...
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelDefineRow.java
View file @
6c1f982f
...
@@ -10,11 +10,7 @@ import java.util.List;
...
@@ -10,11 +10,7 @@ import java.util.List;
*
*
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
ExcelDefineRow
{
public
class
ExcelDefineRow
extends
ExcelRowIndex
{
/**
* 行号
*/
int
rowIndex
;
/**
/**
* 高度
* 高度
*/
*/
...
...
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelRowIndex.java
0 → 100644
View file @
6c1f982f
package
com
.
yanzuoguang
.
excel
;
/**
* 单元格
*
* @author 颜佐光
*/
public
class
ExcelRowIndex
{
/**
* 行号
*/
int
rowIndex
;
}
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