Browse Source

修复表头问题

developing
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); eventListener, trim);
} }
private BaseSaxAnalyser getSaxAnalyser() { private BaseSaxAnalyser getSaxAnalyser() {
if (saxAnalyser != null) { if (saxAnalyser != null) {
return this.saxAnalyser; return this.saxAnalyser;
@ -50,14 +49,14 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
if (!analysisContext.getInputStream().markSupported()) { if (!analysisContext.getInputStream().markSupported()) {
throw new ExcelAnalysisException( throw new ExcelAnalysisException(
"Xls must be available markSupported,you can do like this <code> new " "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); this.saxAnalyser = new XlsSaxAnalyser(analysisContext);
} }
} }
} catch (Exception e) { } catch (Exception e) {
throw new ExcelAnalysisException("File type error,io must be available markSupported,you can do like " 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; return this.saxAnalyser;
} }

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

@ -46,11 +46,12 @@ public class ExcelHeadProperty {
if (this.headClazz != null) { if (this.headClazz != null) {
List<Field> fieldList = new ArrayList<Field>(); List<Field> fieldList = new ArrayList<Field>();
Class tempClass = this.headClazz; 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) { 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(); tempClass = tempClass.getSuperclass();
//得到父类,然后赋给自己
} }
List<List<String>> headList = new ArrayList<List<String>>(); List<List<String>> headList = new ArrayList<List<String>>();
for (Field f : fieldList) { for (Field f : fieldList) {
@ -187,19 +188,20 @@ public class ExcelHeadProperty {
* @return the last consecutive string position * @return the last consecutive string position
*/ */
private int getLastRangNum(int j, String value, List<String> values) { private int getLastRangNum(int j, String value, List<String> values) {
int lastRow = -1; if (value == null) {
if (StringUtils.isEmpty(value)) {
return -1; return -1;
} }
for (int i = 0; i < values.size(); i++) { if (j > 0) {
String current = values.get(i); String preValue = values.get(j - 1);
if (value.equals(current)) { if (value.equals(preValue)) {
if (i >= j) {
lastRow = i;
} else {
//if i<j && value.equals(current) Indicates that a merger has occurred
return -1; 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 { } else {
// if i>j && !value.equals(current) Indicates that the continuous range is exceeded // if i>j && !value.equals(current) Indicates that the continuous range is exceeded
if (i > j) { if (i > j) {
@ -207,7 +209,7 @@ public class ExcelHeadProperty {
} }
} }
} }
return lastRow; return last;
} }

Loading…
Cancel
Save