diff --git a/pom.xml b/pom.xml
index b37aa52..fcfcc3f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
4.0.0
com.alibaba
easyexcel
- 1.0.1
+ 1.0.2
jar
easyexcel
easyexcel is a excel handle tools written in Java
diff --git a/src/main/java/com/alibaba/excel/ExcelWriter.java b/src/main/java/com/alibaba/excel/ExcelWriter.java
index 2fa58c4..fa0525f 100644
--- a/src/main/java/com/alibaba/excel/ExcelWriter.java
+++ b/src/main/java/com/alibaba/excel/ExcelWriter.java
@@ -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而不内存溢出)
diff --git a/src/main/java/com/alibaba/excel/read/SaxAnalyserV07.java b/src/main/java/com/alibaba/excel/read/SaxAnalyserV07.java
index 5047f09..a60771e 100644
--- a/src/main/java/com/alibaba/excel/read/SaxAnalyserV07.java
+++ b/src/main/java/com/alibaba/excel/read/SaxAnalyserV07.java
@@ -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();
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;
- int id = 0;
+ id++;
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;
}
}
}
diff --git a/src/main/java/com/alibaba/excel/read/v07/XmlParserFactory.java b/src/main/java/com/alibaba/excel/read/v07/XmlParserFactory.java
index 40b66a0..5b4b113 100644
--- a/src/main/java/com/alibaba/excel/read/v07/XmlParserFactory.java
+++ b/src/main/java/com/alibaba/excel/read/v07/XmlParserFactory.java
@@ -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);
diff --git a/src/main/java/com/alibaba/excel/util/FileUtil.java b/src/main/java/com/alibaba/excel/util/FileUtil.java
index a790613..15408e5 100644
--- a/src/main/java/com/alibaba/excel/util/FileUtil.java
+++ b/src/main/java/com/alibaba/excel/util/FileUtil.java
@@ -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 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);
- }
}
diff --git a/src/main/java/com/alibaba/excel/util/IndexValueConverter.java b/src/main/java/com/alibaba/excel/util/IndexValueConverter.java
index a0c1529..ca7e2e2 100644
--- a/src/main/java/com/alibaba/excel/util/IndexValueConverter.java
+++ b/src/main/java/com/alibaba/excel/util/IndexValueConverter.java
@@ -1,73 +1,71 @@
-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 converter(List i_list) {
-
- List tem = new ArrayList();
-
- 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 currentStack = new Stack();
- Stack berforStack = new Stack();
-
- 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 converter(List i_list) {
+//
+// List tem = new ArrayList();
+//
+// 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 currentStack = new Stack();
+// Stack beforeStack = new Stack();
+//
+// 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;
+// }
+//}
diff --git a/src/test/java/javamodel/ExcelRowJavaModel1.java b/src/test/java/javamodel/ExcelRowJavaModel1.java
index 764270e..0ca6bac 100644
--- a/src/test/java/javamodel/ExcelRowJavaModel1.java
+++ b/src/test/java/javamodel/ExcelRowJavaModel1.java
@@ -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 Long code;
+ private String 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 Long getCode() {
+ public String getCode() {
return code;
}
- public void setCode(Long code) {
+ public void setCode(String 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 + '\'' +
'}';
}
}
diff --git a/src/test/java/read/v07/Read2007Xlsx.java b/src/test/java/read/v07/Read2007Xlsx.java
index 4fe0156..b9cb256 100644
--- a/src/test/java/read/v07/Read2007Xlsx.java
+++ b/src/test/java/read/v07/Read2007Xlsx.java
@@ -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() {
diff --git a/src/test/resources/2007NoModelMultipleSheet.xlsx b/src/test/resources/2007NoModelMultipleSheet.xlsx
index 48fb870..45a2a74 100644
Binary files a/src/test/resources/2007NoModelMultipleSheet.xlsx and b/src/test/resources/2007NoModelMultipleSheet.xlsx differ
diff --git a/src/test/resources/2007_1.xlsx b/src/test/resources/2007_1.xlsx
index 5aaf0f0..b782ca0 100644
Binary files a/src/test/resources/2007_1.xlsx and b/src/test/resources/2007_1.xlsx differ
diff --git a/src/test/resources/77.xlsx b/src/test/resources/77.xlsx
index f746d39..800cacc 100644
Binary files a/src/test/resources/77.xlsx and b/src/test/resources/77.xlsx differ
diff --git a/src/test/resources/sss.xlsx b/src/test/resources/sss.xlsx
new file mode 100644
index 0000000..e308851
Binary files /dev/null and b/src/test/resources/sss.xlsx differ