Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
金手指1668
easyexcel
提交
26c6de09
E
easyexcel
项目概览
金手指1668
/
easyexcel
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
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 搜索 >>
提交
26c6de09
编写于
3月 16, 2020
作者:
庄家钜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
重写主流程,代码更加优雅 测试完毕
上级
f99c198d
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
43 addition
and
30 deletion
+43
-30
src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java
...m/alibaba/excel/analysis/v07/handlers/CellTagHandler.java
+14
-8
src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
+4
-4
src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java
src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java
+18
-5
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java
.../easyexcel/test/core/converter/ConverterDataListener.java
+1
-1
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
...cel/test/core/converter/ReadAllConverterDataListener.java
+1
-1
src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java
...libaba/easyexcel/test/core/dataformat/DateFormatTest.java
+0
-2
src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
.../alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
+4
-8
src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java
...ava/com/alibaba/easyexcel/test/temp/read/CommentTest.java
+1
-1
src/test/resources/dataformat/dataformat.xls
src/test/resources/dataformat/dataformat.xls
+0
-0
src/test/resources/dataformat/dataformat.xlsx
src/test/resources/dataformat/dataformat.xlsx
+0
-0
未找到文件。
src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java
浏览文件 @
26c6de09
...
...
@@ -10,6 +10,7 @@ import com.alibaba.excel.enums.CellDataTypeEnum;
import
com.alibaba.excel.metadata.CellData
;
import
com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder
;
import
com.alibaba.excel.util.PositionUtils
;
import
com.alibaba.excel.util.StringUtils
;
/**
* Cell Handler
...
...
@@ -18,6 +19,8 @@ import com.alibaba.excel.util.PositionUtils;
*/
public
class
CellTagHandler
extends
AbstractXlsxTagHandler
{
private
static
final
int
DEFAULT_FORMAT_INDEX
=
0
;
@Override
public
void
startElement
(
XlsxReadContext
xlsxReadContext
,
String
name
,
Attributes
attributes
)
{
XlsxReadSheetHolder
xlsxReadSheetHolder
=
xlsxReadContext
.
xlsxReadSheetHolder
();
...
...
@@ -36,15 +39,18 @@ public class CellTagHandler extends AbstractXlsxTagHandler {
// Put in data transformation information
String
dateFormatIndex
=
attributes
.
getValue
(
ExcelXmlConstants
.
ATTRIBUTE_S
);
if
(
dateFormatIndex
!=
null
)
{
int
dateFormatIndexInteger
=
Integer
.
parseInt
(
dateFormatIndex
);
XSSFCellStyle
xssfCellStyle
=
xlsxReadContext
.
xlsxReadWorkbookHolder
().
getStylesTable
().
getStyleAt
(
dateFormatIndexInteger
);
int
dataFormat
=
xssfCellStyle
.
getDataFormat
();
xlsxReadSheetHolder
.
getTempCellData
().
setDataFormat
(
dataFormat
);
xlsxReadSheetHolder
.
getTempCellData
().
setDataFormatString
(
BuiltinFormats
.
getBuiltinFormat
(
dataFormat
,
xssfCellStyle
.
getDataFormatString
(),
xlsxReadSheetHolder
.
getGlobalConfiguration
().
getLocale
()));
Integer
dateFormatIndexInteger
;
if
(
StringUtils
.
isEmpty
(
dateFormatIndex
))
{
dateFormatIndexInteger
=
DEFAULT_FORMAT_INDEX
;
}
else
{
dateFormatIndexInteger
=
Integer
.
parseInt
(
dateFormatIndex
);
}
XSSFCellStyle
xssfCellStyle
=
xlsxReadContext
.
xlsxReadWorkbookHolder
().
getStylesTable
().
getStyleAt
(
dateFormatIndexInteger
);
int
dataFormat
=
xssfCellStyle
.
getDataFormat
();
xlsxReadSheetHolder
.
getTempCellData
().
setDataFormat
(
dataFormat
);
xlsxReadSheetHolder
.
getTempCellData
().
setDataFormatString
(
BuiltinFormats
.
getBuiltinFormat
(
dataFormat
,
xssfCellStyle
.
getDataFormatString
(),
xlsxReadSheetHolder
.
getGlobalConfiguration
().
getLocale
()));
}
}
src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
浏览文件 @
26c6de09
...
...
@@ -64,8 +64,8 @@ public class BuiltinFormats {
// 21
"h:mm:ss"
,
// 22
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy
/m/
d h:mm".
"yyyy
/m/
d h:mm"
,
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy
-m-
d h:mm".
"yyyy
-m-
d h:mm"
,
// 23-26 No specific correspondence found in the official documentation.
// 23
null
,
...
...
@@ -236,8 +236,8 @@ public class BuiltinFormats {
// 21
"h:mm:ss"
,
// 22
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy
/m/
d h:mm".
"yyyy
/m/
d h:mm"
,
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy
-m-
d h:mm".
"yyyy
-m-
d h:mm"
,
// 23-26 No specific correspondence found in the official documentation.
// 23
null
,
...
...
src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java
浏览文件 @
26c6de09
...
...
@@ -10,6 +10,7 @@ import org.apache.poi.poifs.filesystem.FileMagic;
import
com.alibaba.excel.exception.ExcelAnalysisException
;
import
com.alibaba.excel.exception.ExcelCommonException
;
import
com.alibaba.excel.read.metadata.ReadWorkbook
;
import
com.alibaba.excel.util.StringUtils
;
/**
* @author jipengfei
...
...
@@ -45,17 +46,29 @@ public enum ExcelTypeEnum {
if
(!
file
.
exists
())
{
throw
new
ExcelAnalysisException
(
"File "
+
file
.
getAbsolutePath
()
+
" not exists."
);
}
// If there is a password, use the FileMagic first
if
(!
StringUtils
.
isEmpty
(
readWorkbook
.
getPassword
()))
{
BufferedInputStream
bufferedInputStream
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
try
{
return
recognitionExcelType
(
bufferedInputStream
);
}
finally
{
bufferedInputStream
.
close
();
}
}
// Use the name to determine the type
String
fileName
=
file
.
getName
();
if
(
fileName
.
endsWith
(
XLSX
.
getValue
()))
{
return
XLSX
;
}
else
if
(
fileName
.
endsWith
(
XLS
.
getValue
()))
{
return
XLS
;
}
BufferedInputStream
bufferedInputStream
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
try
{
return
recognitionExcelType
(
bufferedInputStream
);
}
finally
{
bufferedInputStream
.
close
();
if
(
StringUtils
.
isEmpty
(
readWorkbook
.
getPassword
()))
{
BufferedInputStream
bufferedInputStream
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
try
{
return
recognitionExcelType
(
bufferedInputStream
);
}
finally
{
bufferedInputStream
.
close
();
}
}
}
if
(!
inputStream
.
markSupported
())
{
...
...
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java
浏览文件 @
26c6de09
...
...
@@ -32,7 +32,7 @@ public class ConverterDataListener extends AnalysisEventListener<ConverterData>
Assert
.
assertEquals
(
list
.
size
(),
1
);
ConverterData
data
=
list
.
get
(
0
);
try
{
Assert
.
assertEquals
(
data
.
getDate
(),
DateUtils
.
parseDate
(
"2020-01-01 01:01:01"
));
Assert
.
assertEquals
(
DateUtils
.
parseDate
(
"2020-01-01 01:01:01"
),
data
.
getDate
(
));
}
catch
(
ParseException
e
)
{
throw
new
ExcelCommonException
(
"Test Exception"
,
e
);
}
...
...
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
浏览文件 @
26c6de09
...
...
@@ -64,7 +64,7 @@ public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllC
Assert
.
assertEquals
(
data
.
getStringBoolean
(),
"true"
);
Assert
.
assertEquals
(
data
.
getStringString
(),
"测试"
);
Assert
.
assertEquals
(
data
.
getStringError
(),
"#VALUE!"
);
Assert
.
assertEquals
(
data
.
getStringNumberDate
(),
"2020-01-01 01:01:01"
);
Assert
.
assertEquals
(
"2020-1-1 1:01"
,
data
.
getStringNumberDate
()
);
double
doubleStringFormulaNumber
=
new
BigDecimal
(
data
.
getStringFormulaNumber
()).
doubleValue
();
Assert
.
assertEquals
(
doubleStringFormulaNumber
,
2.0
,
0.0
);
Assert
.
assertEquals
(
data
.
getStringFormulaString
(),
"1测试"
);
...
...
src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java
浏览文件 @
26c6de09
...
...
@@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory;
import
com.alibaba.easyexcel.test.util.TestFileUtil
;
import
com.alibaba.excel.EasyExcel
;
import
com.alibaba.fastjson.JSON
;
/**
*
...
...
@@ -45,7 +44,6 @@ public class DateFormatTest {
private
void
readCn
(
File
file
)
{
List
<
DateFormatData
>
list
=
EasyExcel
.
read
(
file
,
DateFormatData
.
class
,
null
).
locale
(
Locale
.
CHINA
).
sheet
().
doReadSync
();
System
.
out
.
println
(
JSON
.
toJSONString
(
list
));
for
(
DateFormatData
data
:
list
)
{
Assert
.
assertEquals
(
data
.
getDateStringCn
(),
data
.
getDate
());
Assert
.
assertEquals
(
data
.
getNumberStringCn
(),
data
.
getNumber
());
...
...
src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
浏览文件 @
26c6de09
...
...
@@ -6,19 +6,15 @@ import java.io.FileOutputStream;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.junit.Assert
;
import
org.junit.BeforeClass
;
import
org.junit.FixMethodOrder
;
import
org.junit.Test
;
import
org.junit.runners.MethodSorters
;
import
com.alibaba.easyexcel.test.core.simple.SimpleData
;
import
com.alibaba.easyexcel.test.core.simple.SimpleDataListener
;
import
com.alibaba.easyexcel.test.core.simple.SimpleDataSheetNameListener
;
import
com.alibaba.easyexcel.test.util.TestFileUtil
;
import
com.alibaba.excel.EasyExcel
;
import
com.alibaba.excel.support.ExcelTypeEnum
;
import
com.alibaba.excel.util.FileUtils
;
/**
*
...
...
@@ -51,12 +47,12 @@ public class EncryptDataTest {
}
@Test
public
void
t0
1
ReadAndWriteStream07
()
throws
Exception
{
public
void
t0
3
ReadAndWriteStream07
()
throws
Exception
{
readAndWriteStream
(
file07OutputStream
,
ExcelTypeEnum
.
XLSX
);
}
@Test
public
void
t0
2
ReadAndWriteStream03
()
throws
Exception
{
public
void
t0
4
ReadAndWriteStream03
()
throws
Exception
{
readAndWriteStream
(
file03OutputStream
,
ExcelTypeEnum
.
XLS
);
}
...
...
@@ -72,8 +68,8 @@ public class EncryptDataTest {
fileOutputStream
.
close
();
FileInputStream
fileInputStream
=
new
FileInputStream
(
file
);
EasyExcel
.
read
(
fileInputStream
,
EncryptData
.
class
,
new
EncryptDataListener
()).
password
(
"123456"
)
.
excelType
(
excelType
).
sheet
().
doRead
();
EasyExcel
.
read
(
fileInputStream
,
EncryptData
.
class
,
new
EncryptDataListener
()).
password
(
"123456"
)
.
sheet
()
.
doRead
();
}
private
List
<
SimpleData
>
data
()
{
...
...
src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java
浏览文件 @
26c6de09
...
...
@@ -24,7 +24,7 @@ public class CommentTest {
@Test
public
void
comment
()
throws
Exception
{
File
file
=
new
File
(
"D:\\test\\
listHead07
.xlsx"
);
File
file
=
new
File
(
"D:\\test\\
d1
.xlsx"
);
List
<
Map
<
Integer
,
CellData
>>
datas
=
EasyExcel
.
read
(
file
).
doReadAllSync
();
for
(
Map
<
Integer
,
CellData
>
data
:
datas
)
{
LOGGER
.
info
(
"数据:{}"
,
JSON
.
toJSONString
(
data
));
...
...
src/test/resources/dataformat/dataformat.xls
浏览文件 @
26c6de09
无法预览此类型文件
src/test/resources/dataformat/dataformat.xlsx
浏览文件 @
26c6de09
无法预览此类型文件
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录