提交 891c620a 编写于 作者: J jipengfei.jpf

修复前面7月20日前提到的bug

上级 e39bdb3a
......@@ -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>
......
......@@ -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而不内存溢出)
......
......@@ -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;
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;
}
}
}
......
......@@ -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);
......
......@@ -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);
}
}
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;
// }
//}
......@@ -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 + '\'' +
'}';
}
}
......@@ -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() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册