Browse Source

修复当cell为空可能会抛出空指针的bug

bugfix
Jiaju Zhuang 5 years ago
parent
commit
1802500088
  1. 12
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java

12
src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java

@ -7,6 +7,8 @@ import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_TAG;
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG;
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -33,8 +35,7 @@ import com.alibaba.excel.util.StringUtils;
*/ */
public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder { public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder {
private final AnalysisContext analysisContext; private final AnalysisContext analysisContext;
private String currentTag; private Deque<String> currentTagDeque = new LinkedList<String>();
private String currentCellIndex;
private int curCol; private int curCol;
private Map<Integer, CellData> curRowContent = new TreeMap<Integer, CellData>(); private Map<Integer, CellData> curRowContent = new TreeMap<Integer, CellData>();
private CellData currentCellData; private CellData currentCellData;
@ -61,11 +62,10 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override @Override
public void startHandle(String name, Attributes attributes) { public void startHandle(String name, Attributes attributes) {
currentTag = name; currentTagDeque.push(name);
// start a cell // start a cell
if (CELL_TAG.equals(name)) { if (CELL_TAG.equals(name)) {
currentCellIndex = attributes.getValue(ExcelXmlConstants.POSITION); curCol = PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.POSITION));
curCol = PositionUtils.getCol(currentCellIndex);
// t="s" ,it's means String // t="s" ,it's means String
// t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml' // t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
@ -100,6 +100,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override @Override
public void endHandle(String name) { public void endHandle(String name) {
currentTagDeque.pop();
if (CELL_VALUE_TAG.equals(name)) { if (CELL_VALUE_TAG.equals(name)) {
// Have to go "sharedStrings.xml" and get it // Have to go "sharedStrings.xml" and get it
if (currentCellData.getType() == CellDataTypeEnum.STRING) { if (currentCellData.getType() == CellDataTypeEnum.STRING) {
@ -133,6 +134,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
if (StringUtils.isEmpty(currentCellValue)) { if (StringUtils.isEmpty(currentCellValue)) {
return; return;
} }
String currentTag = currentTagDeque.peek();
if (currentTag == null) { if (currentTag == null) {
return; return;
} }

Loading…
Cancel
Save