Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
苏木力格
easyexcel
提交
7e6d81c5
E
easyexcel
项目概览
苏木力格
/
easyexcel
与 Fork 源项目一致
从无法访问的项目Fork
通知
7
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,发现更多精彩内容 >>
提交
7e6d81c5
编写于
12月 01, 2020
作者:
庄家钜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改07在特殊情况下用`String`接收数字会丢小数位的bug
上级
19fcc3f4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
76 addition
and
12 deletion
+76
-12
pom.xml
pom.xml
+1
-1
src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java
...java/com/alibaba/excel/metadata/format/DataFormatter.java
+69
-9
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
+3
-2
update.md
update.md
+3
-0
未找到文件。
pom.xml
浏览文件 @
7e6d81c5
...
...
@@ -4,7 +4,7 @@
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.alibaba
</groupId>
<artifactId>
easyexcel
</artifactId>
<version>
2.2.
6
</version>
<version>
2.2.
7
</version>
<packaging>
jar
</packaging>
<name>
easyexcel
</name>
...
...
src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java
浏览文件 @
7e6d81c5
...
...
@@ -34,6 +34,8 @@ import java.util.Map;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
org.apache.poi.ss.format.CellFormat
;
import
org.apache.poi.ss.format.CellFormatResult
;
import
org.apache.poi.ss.usermodel.DateUtil
;
import
org.apache.poi.ss.usermodel.ExcelStyleDateFormatter
;
import
org.apache.poi.ss.usermodel.FractionFormat
;
...
...
@@ -168,22 +170,58 @@ public class DataFormatter {
this
.
decimalSymbols
=
DecimalFormatSymbols
.
getInstance
(
this
.
locale
);
}
private
Format
getFormat
(
Integer
dataFormat
,
String
dataFormatString
)
{
private
Format
getFormat
(
Double
data
,
Integer
dataFormat
,
String
dataFormatString
)
{
// Might be better to separate out the n p and z formats, falling back to p when n and z are not set.
// That however would require other code to be re factored.
// String[] formatBits = formatStrIn.split(";");
// int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2;
// String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0];
String
formatStr
=
dataFormatString
;
// Excel supports 2+ part conditional data formats, eg positive/negative/zero,
// or (>1000),(>0),(0),(negative). As Java doesn't handle these kinds
// of different formats for different ranges, just +ve/-ve, we need to
// handle these ourselves in a special way.
// For now, if we detect 2+ parts, we call out to CellFormat to handle it
// TODO Going forward, we should really merge the logic between the two classes
if
(
formatStr
.
contains
(
";"
)
&&
(
formatStr
.
indexOf
(
';'
)
!=
formatStr
.
lastIndexOf
(
';'
)
||
rangeConditionalPattern
.
matcher
(
formatStr
).
matches
()
)
)
{
try
{
// Ask CellFormat to get a formatter for it
CellFormat
cfmt
=
CellFormat
.
getInstance
(
locale
,
formatStr
);
// CellFormat requires callers to identify date vs not, so do so
Object
cellValueO
=
data
;
if
(
DateUtil
.
isADateFormat
(
dataFormat
,
formatStr
)
&&
// don't try to handle Date value 0, let a 3 or 4-part format take care of it
data
.
doubleValue
()
!=
0.0
)
{
cellValueO
=
DateUtil
.
getJavaDate
(
data
,
use1904windowing
);
}
// Wrap and return (non-cachable - CellFormat does that)
return
new
CellFormatResultWrapper
(
cfmt
.
apply
(
cellValueO
)
);
}
catch
(
Exception
e
)
{
LOGGER
.
warn
(
"Formatting failed for format {}, falling back"
,
formatStr
,
e
);
}
}
// See if we already have it cached
Format
format
=
formats
.
get
(
dataFormatString
);
Format
format
=
formats
.
get
(
formatStr
);
if
(
format
!=
null
)
{
return
format
;
}
// Is it one of the special built in types, General or @?
if
(
"General"
.
equalsIgnoreCase
(
dataFormatString
)
||
"@"
.
equals
(
dataFormatString
))
{
if
(
"General"
.
equalsIgnoreCase
(
formatStr
)
||
"@"
.
equals
(
formatStr
))
{
format
=
getDefaultFormat
();
addFormat
(
dataFormatString
,
format
);
addFormat
(
formatStr
,
format
);
return
format
;
}
// Build a formatter, and cache it
format
=
createFormat
(
dataFormat
,
dataFormatString
);
addFormat
(
dataFormatString
,
format
);
format
=
createFormat
(
dataFormat
,
formatStr
);
addFormat
(
formatStr
,
format
);
return
format
;
}
...
...
@@ -530,7 +568,7 @@ public class DataFormatter {
try
{
return
new
InternalDecimalFormatWithScale
(
format
,
symbols
);
}
catch
(
IllegalArgumentException
iae
)
{
LOGGER
.
debug
(
"Formatting failed for format {}, falling back"
,
formatStr
,
iae
);
LOGGER
.
error
(
"Formatting failed for format {}, falling back"
,
formatStr
,
iae
);
// the pattern could not be parsed correctly,
// so fall back to the default number format
return
getDefaultFormat
();
...
...
@@ -570,7 +608,7 @@ public class DataFormatter {
* @return Formatted value
*/
private
String
getFormattedDateString
(
Double
data
,
Integer
dataFormat
,
String
dataFormatString
)
{
Format
dateFormat
=
getFormat
(
dataFormat
,
dataFormatString
);
Format
dateFormat
=
getFormat
(
data
,
data
Format
,
dataFormatString
);
if
(
dateFormat
instanceof
ExcelStyleDateFormatter
)
{
// Hint about the raw excel value
((
ExcelStyleDateFormatter
)
dateFormat
).
setDateToBeFormatted
(
data
);
...
...
@@ -593,7 +631,7 @@ public class DataFormatter {
* @return a formatted number string
*/
private
String
getFormattedNumberString
(
Double
data
,
Integer
dataFormat
,
String
dataFormatString
)
{
Format
numberFormat
=
getFormat
(
dataFormat
,
dataFormatString
);
Format
numberFormat
=
getFormat
(
data
,
data
Format
,
dataFormatString
);
String
formatted
=
numberFormat
.
format
(
data
);
return
formatted
.
replaceFirst
(
"E(\\d)"
,
"E+$1"
);
// to match Excel's E-notation
}
...
...
@@ -794,4 +832,26 @@ public class DataFormatter {
}
}
/**
* Workaround until we merge {@link org.apache.poi.ss.usermodel.DataFormatter} with {@link CellFormat}. Constant, non-cachable wrapper around a
* {@link CellFormatResult}
*/
private
final
class
CellFormatResultWrapper
extends
Format
{
private
final
CellFormatResult
result
;
private
CellFormatResultWrapper
(
CellFormatResult
result
)
{
this
.
result
=
result
;
}
@Override
public
StringBuffer
format
(
Object
obj
,
StringBuffer
toAppendTo
,
FieldPosition
pos
)
{
return
toAppendTo
.
append
(
result
.
text
.
trim
());
}
@Override
public
Object
parseObject
(
String
source
,
ParsePosition
pos
)
{
return
null
;
// Not supported
}
}
}
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
浏览文件 @
7e6d81c5
...
...
@@ -33,9 +33,10 @@ public class Lock2Test {
@Test
public
void
test
()
throws
Exception
{
File
file
=
TestFileUtil
.
readUserHomeFile
(
"test/test6.xls"
);
// File file = TestFileUtil.readUserHomeFile("test/test6.xls");
File
file
=
new
File
(
"D:\\test\\T85_税金入库表202010.xlsx"
);
List
<
Object
>
list
=
EasyExcel
.
read
(
file
).
sheet
(
1
).
headRowNumber
(
0
).
doReadSync
();
List
<
Object
>
list
=
EasyExcel
.
read
(
file
).
sheet
(
0
).
headRowNumber
(
0
).
doReadSync
();
LOGGER
.
info
(
"数据:{}"
,
list
.
size
());
for
(
Object
data
:
list
)
{
LOGGER
.
info
(
"返回数据:{}"
,
JSON
.
toJSONString
(
data
));
...
...
update.md
浏览文件 @
7e6d81c5
# 2.2.6
*
修改07在特殊情况下用
`String`
接收数字会丢小数位的bug
# 2.2.6
*
修改跳着读取03版本空指针bug
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录