Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Laubin729
easyexcel
提交
8c697950
E
easyexcel
项目概览
Laubin729
/
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,发现更多精彩内容 >>
提交
8c697950
编写于
8月 19, 2019
作者:
Z
zhuangjiaju
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化读写逻辑
上级
7aac2e68
变更
23
隐藏空白更改
内联
并排
Showing
23 changed file
with
342 addition
and
33 deletion
+342
-33
src/main/java/com/alibaba/excel/ExcelReader.java
src/main/java/com/alibaba/excel/ExcelReader.java
+1
-1
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
...in/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
+11
-5
src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
...n/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
+3
-0
src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
...baba/excel/analysis/v03/handlers/NumberRecordHandler.java
+2
-0
src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java
...alibaba/excel/analysis/v07/SharedStringsTableHandler.java
+20
-1
src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java
...va/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java
+5
-3
src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java
...n/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java
+3
-2
src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
.../java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
+9
-4
src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
...ibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
+25
-1
src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java
...in/java/com/alibaba/excel/constant/ExcelXmlConstants.java
+4
-0
src/main/java/com/alibaba/excel/context/WriteContextImpl.java
...main/java/com/alibaba/excel/context/WriteContextImpl.java
+22
-13
src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
...libaba/excel/converters/string/StringNumberConverter.java
+14
-0
src/main/java/com/alibaba/excel/metadata/CellData.java
src/main/java/com/alibaba/excel/metadata/CellData.java
+26
-0
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
+2
-2
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java
...baba/easyexcel/test/core/converter/ConverterDataTest.java
+1
-1
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java
...a/easyexcel/test/core/converter/ReadAllConverterData.java
+1
-0
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
...cel/test/core/converter/ReadAllConverterDataListener.java
+1
-0
src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java
src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java
+138
-0
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java
...va/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java
+53
-0
src/test/resources/converter/converter03.xls
src/test/resources/converter/converter03.xls
+0
-0
src/test/resources/converter/converter07.xlsx
src/test/resources/converter/converter07.xlsx
+0
-0
src/test/resources/simple/simple07.xlsx
src/test/resources/simple/simple07.xlsx
+0
-0
update.md
update.md
+1
-0
未找到文件。
src/main/java/com/alibaba/excel/ExcelReader.java
浏览文件 @
8c697950
...
...
@@ -285,7 +285,7 @@ public class ExcelReader {
}
try
{
excelAnalyser
.
finish
();
}
catch
(
Exception
e
)
{
}
catch
(
Throwable
e
)
{
LOGGER
.
warn
(
"Destroy object failed"
,
e
);
}
}
...
...
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
浏览文件 @
8c697950
package
com.alibaba.excel.analysis
;
import
java.io.IOException
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -34,7 +32,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
}
catch
(
RuntimeException
e
)
{
finish
();
throw
e
;
}
catch
(
Exception
e
)
{
}
catch
(
Throwable
e
)
{
finish
();
throw
new
ExcelAnalysisException
(
e
);
}
...
...
@@ -72,7 +70,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
}
catch
(
RuntimeException
e
)
{
finish
();
throw
e
;
}
catch
(
Exception
e
)
{
}
catch
(
Throwable
e
)
{
finish
();
throw
new
ExcelAnalysisException
(
e
);
}
...
...
@@ -88,14 +86,22 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
if
(
readWorkbookHolder
.
getReadCache
()
!=
null
)
{
readWorkbookHolder
.
getReadCache
().
destroy
();
}
}
catch
(
Throwable
e
)
{
throw
new
ExcelAnalysisException
(
"Can not close IO"
,
e
);
}
try
{
if
(
analysisContext
.
readWorkbookHolder
().
getAutoCloseStream
()
&&
readWorkbookHolder
.
getInputStream
()
!=
null
)
{
readWorkbookHolder
.
getInputStream
().
close
();
}
}
catch
(
Throwable
e
)
{
throw
new
ExcelAnalysisException
(
"Can not close IO"
,
e
);
}
try
{
if
(
readWorkbookHolder
.
getTempFile
()
!=
null
)
{
FileUtils
.
delete
(
readWorkbookHolder
.
getTempFile
());
}
}
catch
(
IOException
e
)
{
}
catch
(
Throwable
e
)
{
throw
new
ExcelAnalysisException
(
"Can not close IO"
,
e
);
}
}
...
...
src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
浏览文件 @
8c697950
...
...
@@ -14,7 +14,10 @@ import org.apache.poi.hssf.eventusermodel.HSSFListener;
import
org.apache.poi.hssf.eventusermodel.HSSFRequest
;
import
org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener
;
import
org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord
;
import
org.apache.poi.hssf.record.ExtendedFormatRecord
;
import
org.apache.poi.hssf.record.Record
;
import
org.apache.poi.hssf.record.StyleRecord
;
import
org.apache.poi.hssf.usermodel.HSSFCellStyle
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.poifs.filesystem.POIFSFileSystem
;
...
...
src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
浏览文件 @
8c697950
...
...
@@ -30,6 +30,8 @@ public class NumberRecordHandler extends AbstractXlsRecordHandler {
this
.
row
=
numrec
.
getRow
();
this
.
column
=
numrec
.
getColumn
();
this
.
cellData
=
new
CellData
(
numrec
.
getValue
());
this
.
cellData
.
setDataFormat
(
formatListener
.
getFormatIndex
(
numrec
));
this
.
cellData
.
setDataFormatString
(
formatListener
.
getFormatString
(
numrec
));
}
@Override
...
...
src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java
浏览文件 @
8c697950
package
com.alibaba.excel.analysis.v07
;
import
org.xml.sax.Attributes
;
import
org.xml.sax.helpers.DefaultHandler
;
import
com.alibaba.excel.cache.ReadCache
;
...
...
@@ -11,22 +12,40 @@ import com.alibaba.excel.cache.ReadCache;
*/
public
class
SharedStringsTableHandler
extends
DefaultHandler
{
private
static
final
String
T_TAG
=
"t"
;
private
static
final
String
SI_TAG
=
"si"
;
/**
* The final piece of data
*/
private
String
currentData
;
/**
* Current element data
*/
private
String
currentElementData
;
private
ReadCache
readCache
;
public
SharedStringsTableHandler
(
ReadCache
readCache
)
{
this
.
readCache
=
readCache
;
}
@Override
public
void
startElement
(
String
uri
,
String
localName
,
String
name
,
Attributes
attributes
)
{
if
(
SI_TAG
.
equals
(
name
))
{
currentData
=
""
;
}
}
@Override
public
void
endElement
(
String
uri
,
String
localName
,
String
name
)
{
if
(
T_TAG
.
equals
(
name
))
{
currentData
+=
currentElementData
;
}
else
if
(
SI_TAG
.
equals
(
name
))
{
readCache
.
put
(
currentData
);
}
}
@Override
public
void
characters
(
char
[]
ch
,
int
start
,
int
length
)
{
currentData
=
new
String
(
ch
,
start
,
length
);
current
Element
Data
=
new
String
(
ch
,
start
,
length
);
}
}
src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java
浏览文件 @
8c697950
...
...
@@ -3,6 +3,8 @@ package com.alibaba.excel.analysis.v07;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.apache.poi.xssf.model.StylesTable
;
import
com.alibaba.excel.analysis.v07.handlers.CountRowCellHandler
;
import
com.alibaba.excel.analysis.v07.handlers.DefaultCellHandler
;
import
com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler
;
...
...
@@ -10,14 +12,14 @@ import com.alibaba.excel.context.AnalysisContext;
/**
* Build handler
*
*
* @author Dan Zheng
*/
public
class
XlsxHandlerFactory
{
public
static
List
<
XlsxCellHandler
>
buildCellHandlers
(
AnalysisContext
analysisContext
)
{
public
static
List
<
XlsxCellHandler
>
buildCellHandlers
(
AnalysisContext
analysisContext
,
StylesTable
stylesTable
)
{
List
<
XlsxCellHandler
>
result
=
new
ArrayList
<
XlsxCellHandler
>();
result
.
add
(
new
CountRowCellHandler
(
analysisContext
));
DefaultCellHandler
defaultCellHandler
=
new
DefaultCellHandler
(
analysisContext
);
DefaultCellHandler
defaultCellHandler
=
new
DefaultCellHandler
(
analysisContext
,
stylesTable
);
result
.
add
(
defaultCellHandler
);
result
.
add
(
new
ProcessResultCellHandler
(
analysisContext
,
defaultCellHandler
));
return
result
;
...
...
src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java
浏览文件 @
8c697950
...
...
@@ -2,6 +2,7 @@ package com.alibaba.excel.analysis.v07;
import
java.util.List
;
import
org.apache.poi.xssf.model.StylesTable
;
import
org.xml.sax.Attributes
;
import
org.xml.sax.SAXException
;
import
org.xml.sax.helpers.DefaultHandler
;
...
...
@@ -17,8 +18,8 @@ public class XlsxRowHandler extends DefaultHandler {
private
List
<
XlsxCellHandler
>
cellHandlers
;
private
XlsxRowResultHolder
rowResultHolder
;
public
XlsxRowHandler
(
AnalysisContext
analysisContext
)
{
this
.
cellHandlers
=
XlsxHandlerFactory
.
buildCellHandlers
(
analysisContext
);
public
XlsxRowHandler
(
AnalysisContext
analysisContext
,
StylesTable
stylesTable
)
{
this
.
cellHandlers
=
XlsxHandlerFactory
.
buildCellHandlers
(
analysisContext
,
stylesTable
);
for
(
XlsxCellHandler
cellHandler
:
cellHandlers
)
{
if
(
cellHandler
instanceof
XlsxRowResultHolder
)
{
this
.
rowResultHolder
=
(
XlsxRowResultHolder
)
cellHandler
;
...
...
src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
浏览文件 @
8c697950
...
...
@@ -15,6 +15,7 @@ import javax.xml.parsers.SAXParserFactory;
import
org.apache.poi.openxml4j.opc.OPCPackage
;
import
org.apache.poi.openxml4j.opc.PackagePart
;
import
org.apache.poi.xssf.eventusermodel.XSSFReader
;
import
org.apache.poi.xssf.model.StylesTable
;
import
org.apache.poi.xssf.usermodel.XSSFRelation
;
import
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook
;
import
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr
;
...
...
@@ -47,6 +48,10 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
private
AnalysisContext
analysisContext
;
private
List
<
ReadSheet
>
sheetList
;
private
Map
<
Integer
,
InputStream
>
sheetMap
;
/**
* Current style information
*/
private
StylesTable
stylesTable
;
public
XlsxSaxAnalyser
(
AnalysisContext
analysisContext
)
throws
Exception
{
this
.
analysisContext
=
analysisContext
;
...
...
@@ -64,9 +69,9 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
analysisSharedStringsTable
(
sharedStringsTablePackagePart
.
getInputStream
(),
readWorkbookHolder
);
XSSFReader
xssfReader
=
new
XSSFReader
(
pkg
);
analysisUse1904WindowDate
(
xssfReader
,
readWorkbookHolder
);
stylesTable
=
xssfReader
.
getStylesTable
();
sheetList
=
new
ArrayList
<
ReadSheet
>();
sheetMap
=
new
HashMap
<
Integer
,
InputStream
>();
XSSFReader
.
SheetIterator
ite
=
(
XSSFReader
.
SheetIterator
)
xssfReader
.
getSheetsData
();
...
...
@@ -94,12 +99,12 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
}
if
(
size
<
USE_MAP_CACHE_SIZE
)
{
if
(
LOGGER
.
isDebugEnabled
())
{
LOGGER
.
info
(
"Use map cache.size:{}"
,
size
);
LOGGER
.
debug
(
"Use map cache.size:{}"
,
size
);
}
readWorkbookHolder
.
setReadCache
(
new
MapCache
());
}
else
{
if
(
LOGGER
.
isDebugEnabled
())
{
LOGGER
.
info
(
"Use ehcache.size:{}"
,
size
);
LOGGER
.
debug
(
"Use ehcache.size:{}"
,
size
);
}
readWorkbookHolder
.
setReadCache
(
new
Ehcache
());
}
...
...
@@ -178,7 +183,7 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
@Override
public
void
execute
()
{
parseXmlSource
(
sheetMap
.
get
(
analysisContext
.
readSheetHolder
().
getSheetNo
()),
new
XlsxRowHandler
(
analysisContext
));
new
XlsxRowHandler
(
analysisContext
,
stylesTable
));
}
}
src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
浏览文件 @
8c697950
package
com.alibaba.excel.analysis.v07.handlers
;
import
static
com
.
alibaba
.
excel
.
constant
.
ExcelXmlConstants
.
CELL_DATA_FORMAT_TAG
;
import
static
com
.
alibaba
.
excel
.
constant
.
ExcelXmlConstants
.
CELL_FORMULA_TAG
;
import
static
com
.
alibaba
.
excel
.
constant
.
ExcelXmlConstants
.
CELL_INLINE_STRING_VALUE_TAG
;
import
static
com
.
alibaba
.
excel
.
constant
.
ExcelXmlConstants
.
CELL_TAG
;
...
...
@@ -9,6 +10,9 @@ import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG;
import
java.util.Map
;
import
java.util.TreeMap
;
import
org.apache.poi.ss.usermodel.BuiltinFormats
;
import
org.apache.poi.xssf.model.StylesTable
;
import
org.apache.poi.xssf.usermodel.XSSFCellStyle
;
import
org.apache.poi.xssf.usermodel.XSSFRichTextString
;
import
org.xml.sax.Attributes
;
...
...
@@ -36,9 +40,14 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
private
int
curCol
;
private
Map
<
Integer
,
CellData
>
curRowContent
=
new
TreeMap
<
Integer
,
CellData
>();
private
CellData
currentCellData
;
/**
* Current style information
*/
private
StylesTable
stylesTable
;
public
DefaultCellHandler
(
AnalysisContext
analysisContext
)
{
public
DefaultCellHandler
(
AnalysisContext
analysisContext
,
StylesTable
stylesTable
)
{
this
.
analysisContext
=
analysisContext
;
this
.
stylesTable
=
stylesTable
;
}
@Override
...
...
@@ -75,6 +84,21 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
// t is null ,it's means Empty or Number
CellDataTypeEnum
type
=
CellDataTypeEnum
.
buildFromCellType
(
attributes
.
getValue
(
CELL_VALUE_TYPE_TAG
));
currentCellData
=
new
CellData
(
type
);
// Put in data transformation information
String
dateFormatIndex
=
attributes
.
getValue
(
CELL_DATA_FORMAT_TAG
);
if
(
dateFormatIndex
!=
null
)
{
int
dateFormatIndexInteger
=
Integer
.
parseInt
(
dateFormatIndex
);
XSSFCellStyle
xssfCellStyle
=
stylesTable
.
getStyleAt
(
dateFormatIndexInteger
);
int
dataFormat
=
xssfCellStyle
.
getDataFormat
();
String
dataFormatString
=
xssfCellStyle
.
getDataFormatString
();
currentCellData
.
setDataFormat
(
dataFormat
);
if
(
dataFormatString
==
null
)
{
currentCellData
.
setDataFormatString
(
BuiltinFormats
.
getBuiltinFormat
(
dataFormat
));
}
else
{
currentCellData
.
setDataFormatString
(
dataFormatString
);
}
}
}
// cell is formula
if
(
CELL_FORMULA_TAG
.
equals
(
name
))
{
...
...
src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java
浏览文件 @
8c697950
...
...
@@ -11,6 +11,10 @@ public class ExcelXmlConstants {
public
static
final
String
ROW_TAG
=
"row"
;
public
static
final
String
CELL_TAG
=
"c"
;
public
static
final
String
CELL_VALUE_TYPE_TAG
=
"t"
;
/**
* Number formatted label
*/
public
static
final
String
CELL_DATA_FORMAT_TAG
=
"s"
;
public
static
final
String
CELL_FORMULA_TAG
=
"f"
;
public
static
final
String
CELL_VALUE_TAG
=
"v"
;
/**
...
...
src/main/java/com/alibaba/excel/context/WriteContextImpl.java
浏览文件 @
8c697950
package
com.alibaba.excel.context
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -347,19 +346,29 @@ public class WriteContextImpl implements WriteContext {
try
{
writeWorkbookHolder
.
getWorkbook
().
write
(
writeWorkbookHolder
.
getOutputStream
());
writeWorkbookHolder
.
getWorkbook
().
close
();
if
(
writeWorkbookHolder
.
getAutoCloseStream
())
{
if
(
writeWorkbookHolder
.
getOutputStream
()
!=
null
)
{
writeWorkbookHolder
.
getOutputStream
().
close
();
}
if
(
writeWorkbookHolder
.
getTemplateInputStream
()
!=
null
)
{
writeWorkbookHolder
.
getTemplateInputStream
().
close
();
}
}
else
{
if
(
writeWorkbookHolder
.
getFile
()
!=
null
&&
writeWorkbookHolder
.
getOutputStream
()
!=
null
)
{
writeWorkbookHolder
.
getOutputStream
().
close
();
}
}
catch
(
Throwable
e
)
{
throw
new
ExcelGenerateException
(
"Can not close IO"
,
e
);
}
try
{
if
(
writeWorkbookHolder
.
getAutoCloseStream
()
&&
writeWorkbookHolder
.
getOutputStream
()
!=
null
)
{
writeWorkbookHolder
.
getOutputStream
().
close
();
}
}
catch
(
Throwable
e
)
{
throw
new
ExcelGenerateException
(
"Can not close IO"
,
e
);
}
try
{
if
(
writeWorkbookHolder
.
getAutoCloseStream
()
&&
writeWorkbookHolder
.
getTemplateInputStream
()
!=
null
)
{
writeWorkbookHolder
.
getTemplateInputStream
().
close
();
}
}
catch
(
Throwable
e
)
{
throw
new
ExcelGenerateException
(
"Can not close IO"
,
e
);
}
try
{
if
(!
writeWorkbookHolder
.
getAutoCloseStream
()
&&
writeWorkbookHolder
.
getFile
()
!=
null
&&
writeWorkbookHolder
.
getOutputStream
()
!=
null
)
{
writeWorkbookHolder
.
getOutputStream
().
close
();
}
}
catch
(
IOException
e
)
{
}
catch
(
Throwable
e
)
{
throw
new
ExcelGenerateException
(
"Can not close IO"
,
e
);
}
if
(
LOGGER
.
isDebugEnabled
())
{
...
...
src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
浏览文件 @
8c697950
package
com.alibaba.excel.converters.string
;
import
org.apache.poi.hssf.usermodel.HSSFDateUtil
;
import
org.apache.poi.ss.usermodel.DateUtil
;
import
com.alibaba.excel.converters.Converter
;
import
com.alibaba.excel.enums.CellDataTypeEnum
;
...
...
@@ -38,6 +39,19 @@ public class StringNumberConverter implements Converter<String> {
contentProperty
.
getDateTimeFormatProperty
().
getFormat
());
}
// If there are "NumberFormat", read as number
if
(
contentProperty
!=
null
&&
contentProperty
.
getNumberFormatProperty
()
!=
null
)
{
return
NumberUtils
.
format
(
cellData
.
getDoubleValue
(),
contentProperty
);
}
// Excel defines formatting
if
(
cellData
.
getDataFormat
()
!=
null
)
{
if
(
DateUtil
.
isADateFormat
(
cellData
.
getDataFormat
(),
cellData
.
getDataFormatString
()))
{
return
DateUtils
.
format
(
HSSFDateUtil
.
getJavaDate
(
cellData
.
getDoubleValue
(),
globalConfiguration
.
getUse1904windowing
(),
null
));
}
else
{
return
NumberUtils
.
format
(
cellData
.
getDoubleValue
(),
contentProperty
);
}
}
// Default conversion number
return
NumberUtils
.
format
(
cellData
.
getDoubleValue
(),
contentProperty
);
}
...
...
src/main/java/com/alibaba/excel/metadata/CellData.java
浏览文件 @
8c697950
...
...
@@ -23,6 +23,14 @@ public class CellData {
private
Boolean
booleanValue
;
private
Boolean
formula
;
private
String
formulaValue
;
/**
* The number formatting
*/
private
Integer
dataFormat
;
/**
* The string of number formatting
*/
private
String
dataFormatString
;
public
CellData
(
CellData
other
)
{
this
.
type
=
other
.
type
;
...
...
@@ -31,6 +39,8 @@ public class CellData {
this
.
booleanValue
=
other
.
booleanValue
;
this
.
formula
=
other
.
formula
;
this
.
formulaValue
=
other
.
formulaValue
;
this
.
dataFormat
=
other
.
dataFormat
;
this
.
dataFormatString
=
other
.
dataFormatString
;
}
public
CellData
(
String
stringValue
)
{
...
...
@@ -123,6 +133,22 @@ public class CellData {
this
.
formulaValue
=
formulaValue
;
}
public
Integer
getDataFormat
()
{
return
dataFormat
;
}
public
void
setDataFormat
(
Integer
dataFormat
)
{
this
.
dataFormat
=
dataFormat
;
}
public
String
getDataFormatString
()
{
return
dataFormatString
;
}
public
void
setDataFormatString
(
String
dataFormatString
)
{
this
.
dataFormatString
=
dataFormatString
;
}
@Override
public
String
toString
()
{
switch
(
type
)
{
...
...
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
浏览文件 @
8c697950
...
...
@@ -51,7 +51,7 @@ public class ExcelBuilderImpl implements ExcelBuilder {
}
catch
(
RuntimeException
e
)
{
finish
();
throw
e
;
}
catch
(
Exception
e
)
{
}
catch
(
Throwable
e
)
{
finish
();
throw
new
ExcelGenerateException
(
e
);
}
...
...
@@ -88,7 +88,7 @@ public class ExcelBuilderImpl implements ExcelBuilder {
}
catch
(
RuntimeException
e
)
{
finish
();
throw
e
;
}
catch
(
Exception
e
)
{
}
catch
(
Throwable
e
)
{
finish
();
throw
new
ExcelGenerateException
(
e
);
}
...
...
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java
浏览文件 @
8c697950
...
...
@@ -53,7 +53,7 @@ public class ConverterDataTest {
}
@Test
public
void
T0
3
ReadAllConverter03
()
{
public
void
T0
4
ReadAllConverter03
()
{
readAllConverter
(
"converter"
+
File
.
separator
+
"converter03.xls"
);
}
...
...
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java
浏览文件 @
8c697950
...
...
@@ -42,4 +42,5 @@ public class ReadAllConverterData {
private
String
stringError
;
private
String
stringFormulaNumber
;
private
String
stringFormulaString
;
private
String
stringNumberDate
;
}
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
浏览文件 @
8c697950
...
...
@@ -64,6 +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
(
data
.
getStringFormulaNumber
(),
"2.0"
);
Assert
.
assertEquals
(
data
.
getStringFormulaString
(),
"1测试"
);
LOGGER
.
debug
(
"First row:{}"
,
JSON
.
toJSONString
(
list
.
get
(
0
)));
...
...
src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java
0 → 100644
浏览文件 @
8c697950
package
com.alibaba.easyexcel.test.temp
;
import
java.io.FileInputStream
;
import
java.io.InputStream
;
import
java.util.Date
;
import
java.util.List
;
import
org.apache.poi.hssf.usermodel.HSSFDateUtil
;
import
org.apache.poi.hssf.usermodel.HSSFRow
;
import
org.apache.poi.hssf.usermodel.HSSFSheet
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.BuiltinFormats
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.DataFormatter
;
import
org.apache.poi.ss.usermodel.DateUtil
;
import
org.apache.poi.ss.usermodel.ExcelStyleDateFormatter
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.usermodel.WorkbookFactory
;
import
org.junit.Ignore
;
import
org.junit.Test
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.alibaba.excel.EasyExcel
;
import
com.alibaba.fastjson.JSON
;
/**
* 临时测试
*
* @author Jiaju Zhuang
**/
@Ignore
public
class
StyleTest
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
StyleTest
.
class
);
@Test
public
void
test
()
{
List
<
Object
>
list
=
EasyExcel
.
read
(
"D:\\test\\styleTest.xls"
).
sheet
().
headRowNumber
(
0
).
doReadSync
();
for
(
Object
data
:
list
)
{
LOGGER
.
info
(
"返回数据:{}"
,
JSON
.
toJSONString
(
data
));
}
}
@Test
public
void
poi
()
throws
Exception
{
InputStream
is
=
new
FileInputStream
(
"D:\\test\\styleTest.xls"
);
HSSFWorkbook
hssfWorkbook
=
new
HSSFWorkbook
(
is
);
HSSFSheet
hssfSheet
=
hssfWorkbook
.
getSheetAt
(
0
);
HSSFRow
hssfRow
=
hssfSheet
.
getRow
(
0
);
System
.
out
.
println
(
hssfRow
.
getCell
(
0
).
getCellStyle
().
getDataFormatString
());
DataFormatter
formatter
=
new
DataFormatter
();
System
.
out
.
println
(
hssfRow
.
getCell
(
0
).
getNumericCellValue
());
System
.
out
.
println
(
hssfRow
.
getCell
(
1
).
getNumericCellValue
());
System
.
out
.
println
(
hssfRow
.
getCell
(
2
).
getNumericCellValue
());
System
.
out
.
println
(
hssfRow
.
getCell
(
0
).
getCellStyle
().
getDataFormatString
());
System
.
out
.
println
(
hssfRow
.
getCell
(
1
).
getCellStyle
().
getDataFormatString
());
System
.
out
.
println
(
hssfRow
.
getCell
(
2
).
getCellStyle
().
getDataFormatString
());
}
@Test
public
void
poi07
()
throws
Exception
{
InputStream
is
=
new
FileInputStream
(
"D:\\test\\styleTest.xlsx"
);
Workbook
workbook
=
WorkbookFactory
.
create
(
is
);
// 这种方式 Excel 2003/2007/2010 都是可以处理的
Sheet
sheet
=
workbook
.
getSheetAt
(
0
);
Row
hssfRow
=
sheet
.
getRow
(
0
);
System
.
out
.
println
(
hssfRow
.
getCell
(
0
).
getCellStyle
().
getDataFormatString
());
DataFormatter
formatter
=
new
DataFormatter
();
System
.
out
.
println
(
hssfRow
.
getCell
(
0
).
getNumericCellValue
());
System
.
out
.
println
(
hssfRow
.
getCell
(
1
).
getNumericCellValue
());
System
.
out
.
println
(
hssfRow
.
getCell
(
2
).
getNumericCellValue
());
System
.
out
.
println
(
hssfRow
.
getCell
(
0
).
getCellStyle
().
getDataFormat
());
System
.
out
.
println
(
hssfRow
.
getCell
(
1
).
getCellStyle
().
getDataFormat
());
System
.
out
.
println
(
hssfRow
.
getCell
(
2
).
getCellStyle
().
getDataFormat
());
System
.
out
.
println
(
hssfRow
.
getCell
(
3
).
getCellStyle
().
getDataFormat
());
System
.
out
.
println
(
hssfRow
.
getCell
(
0
).
getCellStyle
().
getDataFormatString
());
System
.
out
.
println
(
hssfRow
.
getCell
(
1
).
getCellStyle
().
getDataFormatString
());
System
.
out
.
println
(
hssfRow
.
getCell
(
2
).
getCellStyle
().
getDataFormatString
());
System
.
out
.
println
(
hssfRow
.
getCell
(
3
).
getCellStyle
().
getDataFormatString
());
isDate
(
hssfRow
.
getCell
(
0
));
isDate
(
hssfRow
.
getCell
(
1
));
isDate
(
hssfRow
.
getCell
(
2
));
isDate
(
hssfRow
.
getCell
(
3
));
}
@Test
public
void
poi0701
()
throws
Exception
{
InputStream
is
=
new
FileInputStream
(
"D:\\test\\f1.xlsx"
);
Workbook
workbook
=
WorkbookFactory
.
create
(
is
);
// 这种方式 Excel 2003/2007/2010 都是可以处理的
Sheet
sheet
=
workbook
.
getSheetAt
(
0
);
print
(
sheet
.
getRow
(
0
).
getCell
(
0
));
print
(
sheet
.
getRow
(
1
).
getCell
(
0
));
print
(
sheet
.
getRow
(
2
).
getCell
(
0
));
print
(
sheet
.
getRow
(
3
).
getCell
(
0
));
}
private
void
print
(
Cell
cell
)
{
System
.
out
.
println
(
DateUtil
.
isADateFormat
(
cell
.
getCellStyle
().
getDataFormat
(),
cell
.
getCellStyle
().
getDataFormatString
()));
System
.
out
.
println
(
cell
.
getCellStyle
().
getDataFormat
());
System
.
out
.
println
(
cell
.
getCellStyle
().
getDataFormatString
());
DataFormatter
f
=
new
DataFormatter
();
System
.
out
.
println
(
f
.
formatCellValue
(
cell
));
if
(
cell
.
getCellStyle
().
getDataFormatString
()
!=
null
)
{
}
ExcelStyleDateFormatter
ff
=
new
ExcelStyleDateFormatter
(
cell
.
getCellStyle
().
getDataFormatString
());
}
@Test
public
void
testFormatter
()
throws
Exception
{
ExcelStyleDateFormatter
ff
=
new
ExcelStyleDateFormatter
(
"yyyy年m月d日"
);
System
.
out
.
println
(
ff
.
format
(
new
Date
()));
}
private
void
isDate
(
Cell
cell
)
{
System
.
out
.
println
(
DateUtil
.
isADateFormat
(
cell
.
getCellStyle
().
getDataFormat
(),
cell
.
getCellStyle
().
getDataFormatString
()));
System
.
out
.
println
(
HSSFDateUtil
.
isCellDateFormatted
(
cell
));
DataFormatter
f
=
new
DataFormatter
();
System
.
out
.
println
(
f
.
formatCellValue
(
cell
));
}
@Test
public
void
testBuiltinFormats
()
throws
Exception
{
System
.
out
.
println
(
BuiltinFormats
.
getBuiltinFormat
(
48
));
System
.
out
.
println
(
BuiltinFormats
.
getBuiltinFormat
(
57
));
System
.
out
.
println
(
BuiltinFormats
.
getBuiltinFormat
(
28
));
}
}
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java
0 → 100644
浏览文件 @
8c697950
package
com.alibaba.easyexcel.test.temp.poi
;
import
java.io.File
;
import
java.io.IOException
;
import
org.apache.poi.ss.usermodel.DateUtil
;
import
org.apache.poi.xssf.streaming.SXSSFRow
;
import
org.apache.poi.xssf.streaming.SXSSFSheet
;
import
org.apache.poi.xssf.streaming.SXSSFWorkbook
;
import
org.apache.poi.xssf.usermodel.XSSFRow
;
import
org.apache.poi.xssf.usermodel.XSSFSheet
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
org.junit.Ignore
;
import
org.junit.Test
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.alibaba.easyexcel.test.util.TestFileUtil
;
/**
* 测试poi
*
* @author Jiaju Zhuang
**/
@Ignore
public
class
PoiFormatTest
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
PoiFormatTest
.
class
);
@Test
public
void
lastRowNum
()
throws
IOException
{
String
file
=
TestFileUtil
.
getPath
()
+
"demo"
+
File
.
separator
+
"demo.xlsx"
;
SXSSFWorkbook
xssfWorkbook
=
new
SXSSFWorkbook
(
new
XSSFWorkbook
(
file
));
SXSSFSheet
xssfSheet
=
xssfWorkbook
.
getSheetAt
(
0
);
LOGGER
.
info
(
"一共行数:{}"
,
xssfSheet
.
getLastRowNum
());
SXSSFRow
row
=
xssfSheet
.
getRow
(
0
);
LOGGER
.
info
(
"第一行数据:{}"
,
row
);
xssfSheet
.
createRow
(
20
);
LOGGER
.
info
(
"一共行数:{}"
,
xssfSheet
.
getLastRowNum
());
}
@Test
public
void
lastRowNumXSSF
()
throws
IOException
{
String
file
=
TestFileUtil
.
getPath
()
+
"demo"
+
File
.
separator
+
"demo.xlsx"
;
XSSFWorkbook
xssfWorkbook
=
new
XSSFWorkbook
(
file
);
LOGGER
.
info
(
"一共:{}个sheet"
,
xssfWorkbook
.
getNumberOfSheets
());
XSSFSheet
xssfSheet
=
xssfWorkbook
.
getSheetAt
(
0
);
LOGGER
.
info
(
"一共行数:{}"
,
xssfSheet
.
getLastRowNum
());
XSSFRow
row
=
xssfSheet
.
getRow
(
0
);
LOGGER
.
info
(
"第一行数据:{}"
,
row
);
xssfSheet
.
createRow
(
20
);
LOGGER
.
info
(
"一共行数:{}"
,
xssfSheet
.
getLastRowNum
());
}
}
src/test/resources/converter/converter03.xls
浏览文件 @
8c697950
无法预览此类型文件
src/test/resources/converter/converter07.xlsx
浏览文件 @
8c697950
无法预览此类型文件
src/test/resources/simple/simple07.xlsx
浏览文件 @
8c697950
无法预览此类型文件
update.md
浏览文件 @
8c697950
...
...
@@ -5,6 +5,7 @@
*
极大优化读大文件的内存和效率
*
sheetNo 改成0开始
*
读支持指定列名
*
升级poi 到4.0.1
# 1.2.4
修复read()方法存在的bug
# 1.2.1
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录