Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_879456671
easyexcel
提交
f679cb45
E
easyexcel
项目概览
qq_879456671
/
easyexcel
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
easyexcel
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f679cb45
编写于
9月 17, 2021
作者:
庄家钜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改获取位置函数
上级
e358db4c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
42 addition
and
25 deletion
+42
-25
src/main/java/com/alibaba/excel/analysis/v07/handlers/CountTagHandler.java
.../alibaba/excel/analysis/v07/handlers/CountTagHandler.java
+5
-5
src/main/java/com/alibaba/excel/util/PositionUtils.java
src/main/java/com/alibaba/excel/util/PositionUtils.java
+32
-20
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java
...est/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java
+5
-0
未找到文件。
src/main/java/com/alibaba/excel/analysis/v07/handlers/CountTagHandler.java
浏览文件 @
f679cb45
package
com.alibaba.excel.analysis.v07.handlers
;
package
com.alibaba.excel.analysis.v07.handlers
;
import
org.xml.sax.Attributes
;
import
com.alibaba.excel.constant.ExcelXmlConstants
;
import
com.alibaba.excel.constant.ExcelXmlConstants
;
import
com.alibaba.excel.context.xlsx.XlsxReadContext
;
import
com.alibaba.excel.context.xlsx.XlsxReadContext
;
import
com.alibaba.excel.util.PositionUtils
;
import
org.xml.sax.Attributes
;
/**
/**
* Cell Handler
* Cell Handler
...
@@ -15,9 +16,8 @@ public class CountTagHandler extends AbstractXlsxTagHandler {
...
@@ -15,9 +16,8 @@ public class CountTagHandler extends AbstractXlsxTagHandler {
@Override
@Override
public
void
startElement
(
XlsxReadContext
xlsxReadContext
,
String
name
,
Attributes
attributes
)
{
public
void
startElement
(
XlsxReadContext
xlsxReadContext
,
String
name
,
Attributes
attributes
)
{
String
d
=
attributes
.
getValue
(
ExcelXmlConstants
.
ATTRIBUTE_REF
);
String
d
=
attributes
.
getValue
(
ExcelXmlConstants
.
ATTRIBUTE_REF
);
String
totalStr
=
d
.
substring
(
d
.
indexOf
(
":"
)
+
1
,
d
.
length
());
String
totalStr
=
d
.
substring
(
d
.
indexOf
(
":"
)
+
1
);
String
c
=
totalStr
.
toUpperCase
().
replaceAll
(
"[A-Z]"
,
""
);
xlsxReadContext
.
readSheetHolder
().
setApproximateTotalRowNumber
(
PositionUtils
.
getRow
(
totalStr
)
+
1
);
xlsxReadContext
.
readSheetHolder
().
setApproximateTotalRowNumber
(
Integer
.
parseInt
(
c
));
}
}
}
}
src/main/java/com/alibaba/excel/util/PositionUtils.java
浏览文件 @
f679cb45
package
com.alibaba.excel.util
;
package
com.alibaba.excel.util
;
import
java.util.Locale
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
org.apache.poi.ss.util.CellReference
;
/**
/**
* @author jipengfei
* @author jipengfei
*/
*/
public
class
PositionUtils
{
public
class
PositionUtils
{
private
static
final
Pattern
CELL_REF_PATTERN
=
Pattern
.
compile
(
"(\\$?[A-Z]+)?"
+
"(\\$?[0-9]+)?"
,
Pattern
.
CASE_INSENSITIVE
);
private
static
final
char
SHEET_NAME_DELIMITER
=
'!'
;
private
PositionUtils
()
{}
private
PositionUtils
()
{}
public
static
int
getRowByRowTagt
(
String
rowTagt
)
{
public
static
int
getRowByRowTagt
(
String
rowTagt
)
{
...
@@ -30,35 +40,37 @@ public class PositionUtils {
...
@@ -30,35 +40,37 @@ public class PositionUtils {
}
}
public
static
int
getRow
(
String
currentCellIndex
)
{
public
static
int
getRow
(
String
currentCellIndex
)
{
int
row
=
0
;
if
(
currentCellIndex
!=
null
)
{
String
rowStr
=
currentCellIndex
.
replaceAll
(
"[A-Z]"
,
""
).
replaceAll
(
"[a-z]"
,
""
);
row
=
Integer
.
parseInt
(
rowStr
)
-
1
;
}
return
row
;
}
public
static
int
getCol
(
String
currentCellIndex
)
{
int
col
=
0
;
if
(
currentCellIndex
!=
null
)
{
if
(
currentCellIndex
!=
null
)
{
int
plingPos
=
currentCellIndex
.
lastIndexOf
(
SHEET_NAME_DELIMITER
);
char
[]
currentIndex
=
currentCellIndex
.
replaceAll
(
"[0-9]"
,
""
).
toCharArray
();
String
cell
=
currentCellIndex
.
substring
(
plingPos
+
1
).
toUpperCase
(
Locale
.
ROOT
);
for
(
int
i
=
0
;
i
<
currentIndex
.
length
;
i
++)
{
Matcher
matcher
=
CELL_REF_PATTERN
.
matcher
(
cell
);
col
+=
(
currentIndex
[
i
]
-
'@'
)
*
Math
.
pow
(
26
,
(
currentIndex
.
length
-
i
-
1
));
if
(!
matcher
.
matches
())
{
throw
new
IllegalArgumentException
(
"Invalid CellReference: "
+
currentCellIndex
);
}
}
String
row
=
matcher
.
group
(
2
);
return
Integer
.
parseInt
(
row
)
-
1
;
}
}
return
col
-
1
;
return
-
1
;
}
}
public
static
int
getCol
(
String
currentCellIndex
,
Integer
before
)
{
public
static
int
getCol
(
String
currentCellIndex
,
Integer
before
)
{
int
col
=
0
;
if
(
currentCellIndex
!=
null
)
{
if
(
currentCellIndex
!=
null
)
{
int
plingPos
=
currentCellIndex
.
lastIndexOf
(
SHEET_NAME_DELIMITER
);
String
cell
=
currentCellIndex
.
substring
(
plingPos
+
1
).
toUpperCase
(
Locale
.
ROOT
);
Matcher
matcher
=
CELL_REF_PATTERN
.
matcher
(
cell
);
if
(!
matcher
.
matches
())
{
throw
new
IllegalArgumentException
(
"Invalid CellReference: "
+
currentCellIndex
);
}
String
col
=
matcher
.
group
(
1
);
char
[]
currentIndex
=
currentCellIndex
.
replaceAll
(
"[0-9]"
,
""
).
toCharArray
();
if
(
col
.
length
()
>
0
&&
col
.
charAt
(
0
)
==
'$'
)
{
for
(
int
i
=
0
;
i
<
currentIndex
.
length
;
i
++)
{
col
=
col
.
substring
(
1
);
col
+=
(
currentIndex
[
i
]
-
'@'
)
*
Math
.
pow
(
26
,
(
currentIndex
.
length
-
i
-
1
));
}
if
(
col
.
length
()
==
0
)
{
return
-
1
;
}
else
{
return
CellReference
.
convertColStringToIndex
(
col
);
}
}
return
col
-
1
;
}
else
{
}
else
{
if
(
before
==
null
)
{
if
(
before
==
null
)
{
before
=
-
1
;
before
=
-
1
;
...
...
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java
浏览文件 @
f679cb45
...
@@ -39,8 +39,10 @@ public class PoiTest {
...
@@ -39,8 +39,10 @@ public class PoiTest {
SXSSFSheet
xssfSheet
=
xssfWorkbook
.
getSheetAt
(
0
);
SXSSFSheet
xssfSheet
=
xssfWorkbook
.
getSheetAt
(
0
);
LOGGER
.
info
(
"一共行数:{}"
,
xssfSheet
.
getLastRowNum
());
LOGGER
.
info
(
"一共行数:{}"
,
xssfSheet
.
getLastRowNum
());
SXSSFRow
row
=
xssfSheet
.
getRow
(
0
);
SXSSFRow
row
=
xssfSheet
.
getRow
(
0
);
LOGGER
.
info
(
"dd{}"
,
row
.
getCell
(
0
).
getColumnIndex
());
Date
date
=
row
.
getCell
(
1
).
getDateCellValue
();
Date
date
=
row
.
getCell
(
1
).
getDateCellValue
();
}
}
@Test
@Test
...
@@ -51,6 +53,9 @@ public class PoiTest {
...
@@ -51,6 +53,9 @@ public class PoiTest {
XSSFSheet
xssfSheet
=
xssfWorkbook
.
getSheetAt
(
0
);
XSSFSheet
xssfSheet
=
xssfWorkbook
.
getSheetAt
(
0
);
LOGGER
.
info
(
"一共行数:{}"
,
xssfSheet
.
getLastRowNum
());
LOGGER
.
info
(
"一共行数:{}"
,
xssfSheet
.
getLastRowNum
());
XSSFRow
row
=
xssfSheet
.
getRow
(
0
);
XSSFRow
row
=
xssfSheet
.
getRow
(
0
);
LOGGER
.
info
(
"dd{}"
,
row
.
getCell
(
0
).
getRow
().
getRowNum
());
LOGGER
.
info
(
"dd{}"
,
xssfSheet
.
getLastRowNum
());
Date
date
=
row
.
getCell
(
1
).
getDateCellValue
();
Date
date
=
row
.
getCell
(
1
).
getDateCellValue
();
LOGGER
.
info
(
"date{}"
,
date
);
LOGGER
.
info
(
"date{}"
,
date
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录