Browse Source

修复表头问题

bugfix
jipengfei.jpf 6 years ago
parent
commit
b28a117eac
  1. 5
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  2. 28
      src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java

5
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java

@ -28,7 +28,6 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
eventListener, trim);
}
private BaseSaxAnalyser getSaxAnalyser() {
if (saxAnalyser != null) {
return this.saxAnalyser;
@ -50,14 +49,14 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
if (!analysisContext.getInputStream().markSupported()) {
throw new ExcelAnalysisException(
"Xls must be available markSupported,you can do like this <code> new "
+ "BufferedInputStream(new FileInputStream(\"/xxxx/xxx/77.xlsx\"))</code> ");
+ "BufferedInputStream(new FileInputStream(\"/xxxx\"))</code> ");
}
this.saxAnalyser = new XlsSaxAnalyser(analysisContext);
}
}
} catch (Exception e) {
throw new ExcelAnalysisException("File type error,io must be available markSupported,you can do like "
+ "this <code> new BufferedInputStream(new FileInputStream(\\\"/xxxx/xxx/77.xlsx\\\"))</code> \"", e);
+ "this <code> new BufferedInputStream(new FileInputStream(\\\"/xxxx\\\"))</code> \"", e);
}
return this.saxAnalyser;
}

28
src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java

@ -46,11 +46,12 @@ public class ExcelHeadProperty {
if (this.headClazz != null) {
List<Field> fieldList = new ArrayList<Field>();
Class tempClass = this.headClazz;
//当父类为null的时候说明到达了最上层的父类(Object类).
//When the parent class is null, it indicates that the parent class (Object class) has reached the top
// level.
while (tempClass != null) {
fieldList.addAll(Arrays.asList(tempClass .getDeclaredFields()));
fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
//Get the parent class and give it to yourself
tempClass = tempClass.getSuperclass();
//得到父类,然后赋给自己
}
List<List<String>> headList = new ArrayList<List<String>>();
for (Field f : fieldList) {
@ -187,19 +188,20 @@ public class ExcelHeadProperty {
* @return the last consecutive string position
*/
private int getLastRangNum(int j, String value, List<String> values) {
int lastRow = -1;
if (StringUtils.isEmpty(value)) {
if (value == null) {
return -1;
}
for (int i = 0; i < values.size(); i++) {
String current = values.get(i);
if (value.equals(current)) {
if (i >= j) {
lastRow = i;
} else {
//if i<j && value.equals(current) Indicates that a merger has occurred
if (j > 0) {
String preValue = values.get(j - 1);
if (value.equals(preValue)) {
return -1;
}
}
int last = j;
for (int i = last + 1; i < values.size(); i++) {
String current = values.get(i);
if (value.equals(current)) {
last = i;
} else {
// if i>j && !value.equals(current) Indicates that the continuous range is exceeded
if (i > j) {
@ -207,7 +209,7 @@ public class ExcelHeadProperty {
}
}
}
return lastRow;
return last;
}

Loading…
Cancel
Save