diff --git a/pom.xml b/pom.xml
index 415b45a8..844f70c4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
4.0.0
com.alibaba
easyexcel
- 2.1.4
+ 2.1.5
jar
easyexcel
diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
index e0b9c413..e53c3cca 100644
--- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
+++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
@@ -36,7 +36,7 @@ import com.alibaba.excel.util.PositionUtils;
public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder {
private final AnalysisContext analysisContext;
private Deque currentTagDeque = new LinkedList();
- private int curCol;
+ private int curCol = -1;
private Map curRowContent = new LinkedHashMap();
private CellData currentCellData;
private StringBuilder dataStringBuilder;
@@ -55,6 +55,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override
public void clearResult() {
curRowContent = new LinkedHashMap();
+ curCol=-1;
}
@Override
@@ -68,7 +69,8 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
currentTagDeque.push(name);
// start a cell
if (CELL_TAG.equals(name)) {
- curCol = PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.POSITION));
+ curCol = PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.POSITION),curCol);
+
// t="s" ,it's means String
// t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java
index 95816463..c8542428 100644
--- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java
+++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java
@@ -20,6 +20,7 @@ import com.alibaba.excel.util.PositionUtils;
public class ProcessResultCellHandler implements XlsxCellHandler {
private AnalysisContext analysisContext;
private XlsxRowResultHolder rowResultHandler;
+ private int currentRow = -1;
public ProcessResultCellHandler(AnalysisContext analysisContext, XlsxRowResultHolder rowResultHandler) {
this.analysisContext = analysisContext;
@@ -33,9 +34,9 @@ public class ProcessResultCellHandler implements XlsxCellHandler {
@Override
public void startHandle(String name, Attributes attributes) {
+ currentRow = PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION),currentRow);
analysisContext.readRowHolder(
- new ReadRowHolder(PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION)),
- analysisContext.readSheetHolder().getGlobalConfiguration()));
+ new ReadRowHolder(currentRow, analysisContext.readSheetHolder().getGlobalConfiguration()));
}
@Override
diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
index a07ad4ce..df4840d0 100644
--- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
+++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
@@ -106,34 +106,39 @@ public class WriteContextImpl implements WriteContext {
if (writeSheet == null) {
throw new IllegalArgumentException("Sheet argument cannot be null");
}
- if (writeSheet.getSheetNo() == null || writeSheet.getSheetNo() <= 0) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Sheet number is null");
- }
- writeSheet.setSheetNo(0);
- }
- if (writeWorkbookHolder.getHasBeenInitializedSheet().containsKey(writeSheet.getSheetNo())) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Sheet:{} is already existed", writeSheet.getSheetNo());
- }
- writeSheetHolder = writeWorkbookHolder.getHasBeenInitializedSheet().get(writeSheet.getSheetNo());
- writeSheetHolder.setNewInitialization(Boolean.FALSE);
- writeTableHolder = null;
- currentWriteHolder = writeSheetHolder;
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("CurrentConfiguration is writeSheetHolder");
- }
+ if (selectSheetFromCache(writeSheet)) {
return;
}
initCurrentSheetHolder(writeSheet);
- WriteHandlerUtils.beforeSheetCreate(this);
// Initialization current sheet
initSheet(writeType);
}
+ private boolean selectSheetFromCache(WriteSheet writeSheet) {
+ writeSheetHolder = null;
+ if (writeSheet.getSheetNo() != null) {
+ writeSheetHolder = writeWorkbookHolder.getHasBeenInitializedSheetIndexMap().get(writeSheet.getSheetNo());
+ }
+ if (writeSheetHolder == null && !StringUtils.isEmpty(writeSheet.getSheetName())) {
+ writeSheetHolder = writeWorkbookHolder.getHasBeenInitializedSheetNameMap().get(writeSheet.getSheetName());
+ }
+ if (writeSheetHolder == null) {
+ return false;
+ }
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("Sheet:{} is already existed", writeSheet.getSheetNo());
+ }
+ writeSheetHolder.setNewInitialization(Boolean.FALSE);
+ writeTableHolder = null;
+ currentWriteHolder = writeSheetHolder;
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("CurrentConfiguration is writeSheetHolder");
+ }
+ return true;
+ }
+
private void initCurrentSheetHolder(WriteSheet writeSheet) {
writeSheetHolder = new WriteSheetHolder(writeSheet, writeWorkbookHolder);
- writeWorkbookHolder.getHasBeenInitializedSheet().put(writeSheet.getSheetNo(), writeSheetHolder);
writeTableHolder = null;
currentWriteHolder = writeSheetHolder;
if (LOGGER.isDebugEnabled()) {
@@ -142,15 +147,26 @@ public class WriteContextImpl implements WriteContext {
}
private void initSheet(WriteTypeEnum writeType) {
+ WriteHandlerUtils.beforeSheetCreate(this);
Sheet currentSheet;
try {
- currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(writeSheetHolder.getSheetNo());
- writeSheetHolder
- .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheetAt(writeSheetHolder.getSheetNo()));
+ if (writeSheetHolder.getSheetNo() != null) {
+ currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(writeSheetHolder.getSheetNo());
+ writeSheetHolder
+ .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheetAt(writeSheetHolder.getSheetNo()));
+ } else {
+ // sheet name must not null
+ currentSheet = writeWorkbookHolder.getWorkbook().getSheet(writeSheetHolder.getSheetName());
+ writeSheetHolder
+ .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheet(writeSheetHolder.getSheetName()));
+ }
} catch (Exception e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Can not find sheet:{} ,now create it", writeSheetHolder.getSheetNo());
}
+ if (StringUtils.isEmpty(writeSheetHolder.getSheetName())) {
+ writeSheetHolder.setSheetName(writeSheetHolder.getSheetNo().toString());
+ }
currentSheet = WorkBookUtil.createSheet(writeWorkbookHolder.getWorkbook(), writeSheetHolder.getSheetName());
writeSheetHolder.setCachedSheet(currentSheet);
}
@@ -160,6 +176,8 @@ public class WriteContextImpl implements WriteContext {
// Initialization head
initHead(writeSheetHolder.excelWriteHeadProperty());
}
+ writeWorkbookHolder.getHasBeenInitializedSheetIndexMap().put(writeSheetHolder.getSheetNo(), writeSheetHolder);
+ writeWorkbookHolder.getHasBeenInitializedSheetNameMap().put(writeSheetHolder.getSheetName(), writeSheetHolder);
}
public void initHead(ExcelWriteHeadProperty excelWriteHeadProperty) {
diff --git a/src/main/java/com/alibaba/excel/util/PositionUtils.java b/src/main/java/com/alibaba/excel/util/PositionUtils.java
index f135e473..b85018f9 100644
--- a/src/main/java/com/alibaba/excel/util/PositionUtils.java
+++ b/src/main/java/com/alibaba/excel/util/PositionUtils.java
@@ -14,6 +14,16 @@ public class PositionUtils {
}
return row;
}
+ public static int getRowByRowTagt(String rowTagt,int before) {
+ int row ;
+ if (rowTagt != null) {
+ row = Integer.parseInt(rowTagt) - 1;
+ return row;
+ }else {
+ return before + 1;
+ }
+
+ }
public static int getRow(String currentCellIndex) {
int row = 0;
@@ -35,4 +45,19 @@ public class PositionUtils {
}
return col - 1;
}
+
+ public static int getCol(String currentCellIndex,int before) {
+ int col = 0;
+ if (currentCellIndex != null) {
+
+ char[] currentIndex = currentCellIndex.replaceAll("[0-9]", "").toCharArray();
+ for (int i = 0; i < currentIndex.length; i++) {
+ col += (currentIndex[i] - '@') * Math.pow(26, (currentIndex.length - i - 1));
+ }
+ return col - 1;
+ }else {
+ return before +1;
+ }
+ }
+
}
diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
index 90030f4f..56fc7e4e 100644
--- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
+++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
@@ -10,6 +10,7 @@ import org.apache.poi.xssf.usermodel.XSSFSheet;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.enums.WriteLastRowTypeEnum;
+import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
/**
@@ -67,12 +68,12 @@ public class WriteSheetHolder extends AbstractWriteHolder {
public WriteSheetHolder(WriteSheet writeSheet, WriteWorkbookHolder writeWorkbookHolder) {
super(writeSheet, writeWorkbookHolder, writeWorkbookHolder.getWriteWorkbook().getConvertAllFiled());
this.writeSheet = writeSheet;
- this.sheetNo = writeSheet.getSheetNo();
- if (writeSheet.getSheetName() == null) {
- this.sheetName = writeSheet.getSheetNo().toString();
+ if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) {
+ this.sheetNo = 0;
} else {
- this.sheetName = writeSheet.getSheetName();
+ this.sheetNo = writeSheet.getSheetNo();
}
+ this.sheetName = writeSheet.getSheetName();
this.parentWriteWorkbookHolder = writeWorkbookHolder;
this.hasBeenInitializedTable = new HashMap();
if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
index c8f7555f..be557ebd 100644
--- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
+++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
@@ -92,7 +92,11 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
/**
* prevent duplicate creation of sheet objects
*/
- private Map hasBeenInitializedSheet;
+ private Map hasBeenInitializedSheetIndexMap;
+ /**
+ * prevent duplicate creation of sheet objects
+ */
+ private Map hasBeenInitializedSheetNameMap;
/**
* Whether the encryption
*/
@@ -148,7 +152,8 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
} else {
this.mandatoryUseInputStream = writeWorkbook.getMandatoryUseInputStream();
}
- this.hasBeenInitializedSheet = new HashMap();
+ this.hasBeenInitializedSheetIndexMap = new HashMap();
+ this.hasBeenInitializedSheetNameMap = new HashMap();
this.password = writeWorkbook.getPassword();
if (writeWorkbook.getInMemory() == null) {
this.inMemory = Boolean.FALSE;
@@ -197,12 +202,20 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
this.cachedWorkbook = cachedWorkbook;
}
- public Map getHasBeenInitializedSheet() {
- return hasBeenInitializedSheet;
+ public Map getHasBeenInitializedSheetIndexMap() {
+ return hasBeenInitializedSheetIndexMap;
+ }
+
+ public void setHasBeenInitializedSheetIndexMap(Map hasBeenInitializedSheetIndexMap) {
+ this.hasBeenInitializedSheetIndexMap = hasBeenInitializedSheetIndexMap;
+ }
+
+ public Map getHasBeenInitializedSheetNameMap() {
+ return hasBeenInitializedSheetNameMap;
}
- public void setHasBeenInitializedSheet(Map hasBeenInitializedSheet) {
- this.hasBeenInitializedSheet = hasBeenInitializedSheet;
+ public void setHasBeenInitializedSheetNameMap(Map hasBeenInitializedSheetNameMap) {
+ this.hasBeenInitializedSheetNameMap = hasBeenInitializedSheetNameMap;
}
public WriteWorkbook getWriteWorkbook() {
diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
index 04a8bd1e..fe021633 100644
--- a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
+++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
@@ -39,6 +39,10 @@ public class FillDataTest {
private static File horizontalFillTemplate07;
private static File fileHorizontal03;
private static File horizontalFillTemplate03;
+ private static File byName07;
+ private static File byName03;
+ private static File byNameTemplate07;
+ private static File byNameTemplate03;
@BeforeClass
public static void init() {
@@ -54,6 +58,10 @@ public class FillDataTest {
horizontalFillTemplate07 = TestFileUtil.readFile("fill" + File.separator + "horizontal.xlsx");
fileHorizontal03 = TestFileUtil.createNewFile("fillHorizontal03.xls");
horizontalFillTemplate03 = TestFileUtil.readFile("fill" + File.separator + "horizontal.xls");
+ byName07 = TestFileUtil.createNewFile("byName07.xlsx");
+ byNameTemplate07 = TestFileUtil.readFile("fill" + File.separator + "byName.xlsx");
+ byName03 = TestFileUtil.createNewFile("byName03.xls");
+ byNameTemplate03 = TestFileUtil.readFile("fill" + File.separator + "byName.xls");
}
@Test
@@ -86,6 +94,23 @@ public class FillDataTest {
horizontalFill(fileHorizontal03, horizontalFillTemplate03);
}
+ @Test
+ public void t07ByNameFill07() {
+ byNameFill(byName07, byNameTemplate07);
+ }
+
+ @Test
+ public void t08ByNameFill03() {
+ byNameFill(byName03, byNameTemplate03);
+ }
+
+ private void byNameFill(File file, File template) {
+ FillData fillData = new FillData();
+ fillData.setName("张三");
+ fillData.setNumber(5.2);
+ EasyExcel.write(file, FillData.class).withTemplate(template).sheet("Sheet2").doFill(fillData);
+ }
+
private void horizontalFill(File file, File template) {
ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
diff --git a/src/test/resources/fill/byName.xls b/src/test/resources/fill/byName.xls
new file mode 100644
index 00000000..e07fd503
Binary files /dev/null and b/src/test/resources/fill/byName.xls differ
diff --git a/src/test/resources/fill/byName.xlsx b/src/test/resources/fill/byName.xlsx
new file mode 100644
index 00000000..327e0557
Binary files /dev/null and b/src/test/resources/fill/byName.xlsx differ
diff --git a/update.md b/update.md
index 830a7d17..35ecf499 100644
--- a/update.md
+++ b/update.md
@@ -1,3 +1,7 @@
+# 2.1.5
+* 修复部分xlsx没有行号读取异常
+* 填充时候支持根据`sheetName`定位`sheet`
+
# 2.1.4
* 新增参数`useDefaultListener` 可以排除默认对象转换