Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Laubin729
easyexcel
提交
891c620a
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,体验更适合开发者的 AI 搜索 >>
提交
891c620a
编写于
7月 21, 2018
作者:
J
jipengfei.jpf
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修复前面7月20日前提到的bug
上级
e39bdb3a
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
187 addition
and
177 deletion
+187
-177
pom.xml
pom.xml
+1
-1
src/main/java/com/alibaba/excel/ExcelWriter.java
src/main/java/com/alibaba/excel/ExcelWriter.java
+2
-2
src/main/java/com/alibaba/excel/read/SaxAnalyserV07.java
src/main/java/com/alibaba/excel/read/SaxAnalyserV07.java
+36
-4
src/main/java/com/alibaba/excel/read/v07/XmlParserFactory.java
...ain/java/com/alibaba/excel/read/v07/XmlParserFactory.java
+12
-0
src/main/java/com/alibaba/excel/util/FileUtil.java
src/main/java/com/alibaba/excel/util/FileUtil.java
+12
-3
src/main/java/com/alibaba/excel/util/IndexValueConverter.java
...main/java/com/alibaba/excel/util/IndexValueConverter.java
+71
-73
src/test/java/javamodel/ExcelRowJavaModel1.java
src/test/java/javamodel/ExcelRowJavaModel1.java
+9
-94
src/test/java/read/v07/Read2007Xlsx.java
src/test/java/read/v07/Read2007Xlsx.java
+44
-0
src/test/resources/2007NoModelMultipleSheet.xlsx
src/test/resources/2007NoModelMultipleSheet.xlsx
+0
-0
src/test/resources/2007_1.xlsx
src/test/resources/2007_1.xlsx
+0
-0
src/test/resources/77.xlsx
src/test/resources/77.xlsx
+0
-0
src/test/resources/sss.xlsx
src/test/resources/sss.xlsx
+0
-0
未找到文件。
pom.xml
浏览文件 @
891c620a
...
...
@@ -4,7 +4,7 @@
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.alibaba
</groupId>
<artifactId>
easyexcel
</artifactId>
<version>
1.0.
1
</version>
<version>
1.0.
2
</version>
<packaging>
jar
</packaging>
<name>
easyexcel
</name>
<description>
easyexcel is a excel handle tools written in Java
</description>
...
...
src/main/java/com/alibaba/excel/ExcelWriter.java
浏览文件 @
891c620a
...
...
@@ -20,7 +20,7 @@ public class ExcelWriter {
private
ExcelBuilder
excelBuilder
;
/**
* 生成
小Excel低于2000行
* 生成
EXCEL
*
* @param outputStream 文件输出流
* @param typeEnum 输出文件类型03或07,强烈建议使用07版(可以输出超大excel而不内存溢出)
...
...
@@ -30,7 +30,7 @@ public class ExcelWriter {
}
/**
* 生成
小Excel低于2000行
* 生成
EXCEL
*
* @param outputStream 文件输出流
* @param typeEnum 输出文件类型03或07,强烈建议使用07版(可以输出超大excel而不内存溢出)
...
...
src/main/java/com/alibaba/excel/read/SaxAnalyserV07.java
浏览文件 @
891c620a
...
...
@@ -84,6 +84,7 @@ public class SaxAnalyserV07 extends BaseSaxAnalyser {
stop
();
throw
new
ExcelAnalysisException
(
e
);
}
finally
{
}
}
...
...
@@ -99,6 +100,16 @@ public class SaxAnalyserV07 extends BaseSaxAnalyser {
}
public
void
stop
()
{
for
(
SheetSource
sheet
:
sheetSourceList
)
{
if
(
sheet
.
getInputStream
()
!=
null
)
{
try
{
sheet
.
getInputStream
().
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
FileUtil
.
deletefile
(
path
);
}
...
...
@@ -164,16 +175,18 @@ public class SaxAnalyserV07 extends BaseSaxAnalyser {
this
.
sheetSourceList
=
new
ArrayList
<
SheetSource
>();
InputStream
workbookXml
=
new
FileInputStream
(
this
.
workBookXMLFilePath
);
XmlParserFactory
.
parse
(
workbookXml
,
new
DefaultHandler
()
{
private
int
id
=
0
;
@Override
public
void
startElement
(
String
uri
,
String
localName
,
String
qName
,
Attributes
attrs
)
throws
SAXException
{
if
(
qName
.
toLowerCase
(
Locale
.
US
).
equals
(
"sheet"
))
{
String
name
=
null
;
i
nt
id
=
0
;
i
d
++
;
for
(
int
i
=
0
;
i
<
attrs
.
getLength
();
i
++)
{
if
(
attrs
.
getLocalName
(
i
).
toLowerCase
(
Locale
.
US
).
equals
(
"name"
))
{
name
=
attrs
.
getValue
(
i
);
}
else
if
(
attrs
.
getLocalName
(
i
).
toLowerCase
(
Locale
.
US
).
equals
(
"r:id"
))
{
id
=
Integer
.
parseInt
(
attrs
.
getValue
(
i
).
replaceAll
(
"rId"
,
""
));
//
id = Integer.parseInt(attrs.getValue(i).replaceAll("rId", ""));
try
{
InputStream
inputStream
=
new
FileInputStream
(
XMLTempFile
.
getSheetFilePath
(
path
,
id
));
sheetSourceList
.
add
(
new
SheetSource
(
id
,
name
,
inputStream
));
...
...
@@ -210,9 +223,28 @@ public class SaxAnalyserV07 extends BaseSaxAnalyser {
//this.sharedStringsTable.readFrom(inputStream);
XmlParserFactory
.
parse
(
inputStream
,
new
DefaultHandler
()
{
int
lastElementPosition
=
-
1
;
int
lastHandledElementPosition
=
-
1
;
@Override
public
void
startElement
(
String
uri
,
String
localName
,
String
qName
,
Attributes
attributes
)
{
if
(
hasSkippedEmptySharedString
())
{
sharedStringList
.
add
(
""
);
}
if
(
"t"
.
equals
(
qName
))
{
lastElementPosition
++;
}
}
private
boolean
hasSkippedEmptySharedString
()
{
return
lastElementPosition
>
lastHandledElementPosition
;
}
@Override
public
void
characters
(
char
[]
ch
,
int
start
,
int
length
)
{
sharedStringList
.
add
(
new
String
(
ch
,
start
,
length
));
lastHandledElementPosition
++;
}
});
...
...
@@ -261,9 +293,9 @@ public class SaxAnalyserV07 extends BaseSaxAnalyser {
if
(
o
.
id
==
this
.
id
)
{
return
0
;
}
else
if
(
o
.
id
>
this
.
id
)
{
return
1
;
}
else
{
return
-
1
;
}
else
{
return
1
;
}
}
}
...
...
src/main/java/com/alibaba/excel/read/v07/XmlParserFactory.java
浏览文件 @
891c620a
...
...
@@ -18,10 +18,22 @@ import org.xml.sax.XMLReader;
*/
public
class
XmlParserFactory
{
/**
* xml解析
* @param inputStream
* @param contentHandler
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public
static
void
parse
(
InputStream
inputStream
,
ContentHandler
contentHandler
)
throws
ParserConfigurationException
,
SAXException
,
IOException
{
InputSource
sheetSource
=
new
InputSource
(
inputStream
);
SAXParserFactory
saxFactory
=
SAXParserFactory
.
newInstance
();
//防止XML实体注注入
saxFactory
.
setFeature
(
"http://apache.org/xml/features/disallow-doctype-decl"
,
true
);
saxFactory
.
setFeature
(
"http://xml.org/sax/features/external-general-entities"
,
false
);
saxFactory
.
setFeature
(
"http://xml.org/sax/features/external-parameter-entities"
,
false
);
SAXParser
saxParser
=
saxFactory
.
newSAXParser
();
XMLReader
xmlReader
=
saxParser
.
getXMLReader
();
xmlReader
.
setContentHandler
(
contentHandler
);
...
...
src/main/java/com/alibaba/excel/util/FileUtil.java
浏览文件 @
891c620a
...
...
@@ -66,12 +66,23 @@ public class FileUtil {
return
(
filePosi
==
-
1
)
?
""
:
filePath
.
substring
(
0
,
filePosi
);
}
/**
* 文件解压
* @param path
* @param file
* @return
* @throws IOException
*/
public
static
boolean
doUnZip
(
String
path
,
File
file
)
throws
IOException
{
ZipFile
zipFile
=
new
ZipFile
(
file
,
"utf-8"
);
Enumeration
<
ZipArchiveEntry
>
en
=
zipFile
.
getEntries
();
ZipArchiveEntry
ze
;
while
(
en
.
hasMoreElements
())
{
ze
=
en
.
nextElement
();
if
(
ze
.
getName
().
contains
(
"../"
)){
//防止目录穿越
throw
new
IllegalStateException
(
"unsecurity zipfile!"
);
}
File
f
=
new
File
(
path
,
ze
.
getName
());
if
(
ze
.
isDirectory
())
{
f
.
mkdirs
();
...
...
@@ -103,11 +114,9 @@ public class FileUtil {
deletefile
(
delpath
+
File
.
separator
+
filelist
[
i
]);
}
}
file
.
delete
();
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
File
.
separator
);
}
}
src/main/java/com/alibaba/excel/util/IndexValueConverter.java
浏览文件 @
891c620a
package
com.alibaba.excel.util
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
import
com.alibaba.excel.metadata.IndexValue
;
/**
* 去除空Cell
* @author jipengfei
*/
public
class
IndexValueConverter
{
public
static
List
<
String
>
converter
(
List
<
IndexValue
>
i_list
)
{
List
<
String
>
tem
=
new
ArrayList
<
String
>();
char
[]
start
=
{
'@'
};
int
j
=
0
;
for
(;
j
<
i_list
.
size
();
j
++)
{
IndexValue
currentIndexValue
=
i_list
.
get
(
j
);
char
[]
currentIndex
=
currentIndexValue
.
getV_index
().
replaceAll
(
"[0-9]"
,
""
).
toCharArray
();
if
(
j
>
0
)
{
start
=
i_list
.
get
(
j
-
1
).
getV_index
().
replaceAll
(
"[0-9]"
,
""
).
toCharArray
();
}
int
deep
=
subtraction26
(
currentIndex
,
start
);
int
k
=
0
;
for
(;
k
<
deep
-
1
;
k
++)
{
tem
.
add
(
null
);
}
tem
.
add
(
currentIndexValue
.
getV_value
());
}
return
tem
;
}
private
static
int
subtraction26
(
char
[]
currentIndex
,
char
[]
beforeIndex
)
{
int
result
=
0
;
Stack
<
Character
>
currentStack
=
new
Stack
<
Character
>();
Stack
<
Character
>
berforStack
=
new
Stack
<
Character
>();
for
(
int
i
=
0
;
i
<
currentIndex
.
length
;
i
++)
{
currentStack
.
push
(
currentIndex
[
i
]);
}
for
(
int
i
=
0
;
i
<
beforeIndex
.
length
;
i
++)
{
berforStack
.
push
(
beforeIndex
[
i
]);
}
int
i
=
0
;
char
beforechar
=
'@'
;
while
(!
currentStack
.
isEmpty
())
{
char
currentChar
=
currentStack
.
pop
();
if
(!
berforStack
.
isEmpty
())
{
beforechar
=
berforStack
.
pop
();
}
int
n
=
currentChar
-
beforechar
;
if
(
n
<
0
){
n
=
n
+
26
;
if
(!
currentStack
.
isEmpty
()){
char
borrow
=
currentStack
.
pop
();
char
newBorrow
=(
char
)(
borrow
-
1
);
currentStack
.
push
(
newBorrow
);
}
}
result
+=
n
*
Math
.
pow
(
26
,
i
);
i
++;
beforechar
=
'@'
;
}
return
result
;
}
}
//package com.alibaba.excel.util;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Stack;
//
//import com.alibaba.excel.metadata.IndexValue;
//
///**
// * 去除空Cell
// * @author jipengfei
// */
//public class IndexValueConverter {
// public static List<String> converter(List<IndexValue> i_list) {
//
// List<String> tem = new ArrayList<String>();
//
// char[] start = {'@'};
// int j = 0;
// for (; j < i_list.size(); j++) {
// IndexValue currentIndexValue = i_list.get(j);
// char[] currentIndex = currentIndexValue.getV_index().replaceAll("[0-9]", "").toCharArray();
// if (j > 0) {
// start = i_list.get(j - 1).getV_index().replaceAll("[0-9]", "").toCharArray();
// }
// int deep = subtraction26(currentIndex, start);
// int k = 0;
// for (; k < deep - 1; k++) {
// tem.add(null);
// }
// tem.add(currentIndexValue.getV_value());
// }
// return tem;
// }
//
// private static int subtraction26(char[] currentIndex, char[] beforeIndex) {
// int result = 0;
//
// Stack<Character> currentStack = new Stack<Character>();
// Stack<Character> beforeStack = new Stack<Character>();
//
// for (int i = 0; i < currentIndex.length; i++) {
// currentStack.push(currentIndex[i]);
// }
// for (int i = 0; i < beforeIndex.length; i++) {
// beforeStack.push(beforeIndex[i]);
// }
// int i = 0;
// char beforeChar = '@';
// while (!currentStack.isEmpty()) {
// char currentChar = currentStack.pop();
// if (!beforeStack.isEmpty()) {
// beforeChar = beforeStack.pop();
// }
// int n = currentChar - beforeChar;
// if(n<0){
// n = n+26;
// if(!currentStack.isEmpty()){
// char borrow = currentStack.pop();
// char newBorrow =(char)(borrow -1);
// currentStack.push(newBorrow);
// }
// }
// result += n * Math.pow(26, i);
// i++;
// beforeChar='@';
// }
//
// return result;
// }
//}
src/test/java/javamodel/ExcelRowJavaModel1.java
浏览文件 @
891c620a
...
...
@@ -14,117 +14,32 @@ import com.alibaba.excel.metadata.BaseRowModel;
public
class
ExcelRowJavaModel1
extends
BaseRowModel
{
@ExcelProperty
(
index
=
0
,
value
=
"银行放款编号"
)
private
int
num
;
private
String
num
;
@ExcelProperty
(
index
=
1
,
value
=
"code"
)
private
Lo
ng
code
;
private
Stri
ng
code
;
@ExcelProperty
(
index
=
2
,
value
=
"银行存放期期"
)
private
Date
endTime
;
@ExcelProperty
(
index
=
3
,
value
=
"测试1"
)
private
Double
money
;
@ExcelProperty
(
index
=
4
,
value
=
"测试2"
)
private
String
times
;
@ExcelProperty
(
index
=
5
,
value
=
"测试3"
)
private
int
activityCode
;
@ExcelProperty
(
index
=
6
,
value
=
"测试4"
)
private
Date
date
;
@ExcelProperty
(
index
=
7
,
value
=
"测试5"
)
private
Double
lx
;
@ExcelProperty
(
index
=
8
,
value
=
"测试6"
)
private
String
name
;
public
int
getNum
()
{
public
String
getNum
()
{
return
num
;
}
public
void
setNum
(
int
num
)
{
public
void
setNum
(
String
num
)
{
this
.
num
=
num
;
}
public
Lo
ng
getCode
()
{
public
Stri
ng
getCode
()
{
return
code
;
}
public
void
setCode
(
Lo
ng
code
)
{
public
void
setCode
(
Stri
ng
code
)
{
this
.
code
=
code
;
}
public
Date
getEndTime
()
{
return
endTime
;
}
public
void
setEndTime
(
Date
endTime
)
{
this
.
endTime
=
endTime
;
}
public
Double
getMoney
()
{
return
money
;
}
public
void
setMoney
(
Double
money
)
{
this
.
money
=
money
;
}
public
String
getTimes
()
{
return
times
;
}
public
void
setTimes
(
String
times
)
{
this
.
times
=
times
;
}
public
int
getActivityCode
()
{
return
activityCode
;
}
public
void
setActivityCode
(
int
activityCode
)
{
this
.
activityCode
=
activityCode
;
}
public
Date
getDate
()
{
return
date
;
}
public
void
setDate
(
Date
date
)
{
this
.
date
=
date
;
}
public
Double
getLx
()
{
return
lx
;
}
public
void
setLx
(
Double
lx
)
{
this
.
lx
=
lx
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
@Override
public
String
toString
()
{
return
"ExcelRowJavaModel{"
+
"num="
+
num
+
", code="
+
code
+
", endTime="
+
endTime
+
", money="
+
money
+
", times='"
+
times
+
'\''
+
", activityCode="
+
activityCode
+
", date="
+
date
+
", lx="
+
lx
+
", name='"
+
name
+
'\''
+
return
"ExcelRowJavaModel1{"
+
"num='"
+
num
+
'\''
+
", code='"
+
code
+
'\''
+
'}'
;
}
}
src/test/java/read/v07/Read2007Xlsx.java
浏览文件 @
891c620a
...
...
@@ -107,6 +107,7 @@ public class Read2007Xlsx {
});
reader
.
read
();
//reader.finish();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
@@ -161,6 +162,49 @@ public class Read2007Xlsx {
}
}
@Test
public
void
withModelMultipleSheet1
()
{
InputStream
inputStream
=
getInputStream
(
"sss.xlsx"
);
try
{
ExcelReader
reader
=
new
ExcelReader
(
inputStream
,
ExcelTypeEnum
.
XLSX
,
null
,
new
AnalysisEventListener
()
{
@Override
public
void
invoke
(
Object
object
,
AnalysisContext
context
)
{
ExcelRowJavaModel1
obj
=
null
;
if
(
context
.
getCurrentSheet
().
getSheetNo
()
==
1
)
{
obj
=
(
ExcelRowJavaModel1
)
object
;
}
//if (context.getCurrentSheet().getSheetNo() == 2) {
// obj = (ExcelRowJavaModel)object;
//}
System
.
out
.
println
(
"当前sheet:"
+
context
.
getCurrentSheet
().
getSheetNo
()
+
" 当前行:"
+
context
.
getCurrentRowNum
()
+
" data:"
+
obj
);
}
@Override
public
void
doAfterAllAnalysed
(
AnalysisContext
context
)
{
}
});
reader
.
read
(
new
Sheet
(
1
,
1
,
ExcelRowJavaModel1
.
class
));
// reader.read(new Sheet(2, 1, ExcelRowJavaModel1.class));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
inputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
//读取shets
@Test
public
void
getSheets
()
{
...
...
src/test/resources/2007NoModelMultipleSheet.xlsx
浏览文件 @
891c620a
无法预览此类型文件
src/test/resources/2007_1.xlsx
浏览文件 @
891c620a
无法预览此类型文件
src/test/resources/77.xlsx
浏览文件 @
891c620a
无法预览此类型文件
src/test/resources/sss.xlsx
0 → 100644
浏览文件 @
891c620a
文件已添加
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录