Browse Source

Merge pull request #1050 from alibaba/2.1.fix

2.1.fix
pull/1121/head v2.1.4
Jiaju Zhuang 5 years ago committed by GitHub
parent
commit
617d2b5698
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pom.xml
  2. 6
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  3. 5
      src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java
  4. 62
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  5. 25
      src/main/java/com/alibaba/excel/util/PositionUtils.java
  6. 9
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
  7. 25
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  8. 25
      src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
  9. BIN
      src/test/resources/fill/byName.xls
  10. BIN
      src/test/resources/fill/byName.xlsx
  11. 4
      update.md

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>2.1.4</version> <version>2.1.5</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

6
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 { public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder {
private final AnalysisContext analysisContext; private final AnalysisContext analysisContext;
private Deque<String> currentTagDeque = new LinkedList<String>(); private Deque<String> currentTagDeque = new LinkedList<String>();
private int curCol; private int curCol = -1;
private Map<Integer, CellData> curRowContent = new LinkedHashMap<Integer, CellData>(); private Map<Integer, CellData> curRowContent = new LinkedHashMap<Integer, CellData>();
private CellData currentCellData; private CellData currentCellData;
private StringBuilder dataStringBuilder; private StringBuilder dataStringBuilder;
@ -55,6 +55,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override @Override
public void clearResult() { public void clearResult() {
curRowContent = new LinkedHashMap<Integer, CellData>(); curRowContent = new LinkedHashMap<Integer, CellData>();
curCol=-1;
} }
@Override @Override
@ -68,7 +69,8 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
currentTagDeque.push(name); currentTagDeque.push(name);
// start a cell // start a cell
if (CELL_TAG.equals(name)) { 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="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'

5
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 { public class ProcessResultCellHandler implements XlsxCellHandler {
private AnalysisContext analysisContext; private AnalysisContext analysisContext;
private XlsxRowResultHolder rowResultHandler; private XlsxRowResultHolder rowResultHandler;
private int currentRow = -1;
public ProcessResultCellHandler(AnalysisContext analysisContext, XlsxRowResultHolder rowResultHandler) { public ProcessResultCellHandler(AnalysisContext analysisContext, XlsxRowResultHolder rowResultHandler) {
this.analysisContext = analysisContext; this.analysisContext = analysisContext;
@ -33,9 +34,9 @@ public class ProcessResultCellHandler implements XlsxCellHandler {
@Override @Override
public void startHandle(String name, Attributes attributes) { public void startHandle(String name, Attributes attributes) {
currentRow = PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION),currentRow);
analysisContext.readRowHolder( analysisContext.readRowHolder(
new ReadRowHolder(PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION)), new ReadRowHolder(currentRow, analysisContext.readSheetHolder().getGlobalConfiguration()));
analysisContext.readSheetHolder().getGlobalConfiguration()));
} }
@Override @Override

62
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -106,34 +106,39 @@ public class WriteContextImpl implements WriteContext {
if (writeSheet == null) { if (writeSheet == null) {
throw new IllegalArgumentException("Sheet argument cannot be null"); throw new IllegalArgumentException("Sheet argument cannot be null");
} }
if (writeSheet.getSheetNo() == null || writeSheet.getSheetNo() <= 0) { if (selectSheetFromCache(writeSheet)) {
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");
}
return; return;
} }
initCurrentSheetHolder(writeSheet); initCurrentSheetHolder(writeSheet);
WriteHandlerUtils.beforeSheetCreate(this);
// Initialization current sheet // Initialization current sheet
initSheet(writeType); 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) { private void initCurrentSheetHolder(WriteSheet writeSheet) {
writeSheetHolder = new WriteSheetHolder(writeSheet, writeWorkbookHolder); writeSheetHolder = new WriteSheetHolder(writeSheet, writeWorkbookHolder);
writeWorkbookHolder.getHasBeenInitializedSheet().put(writeSheet.getSheetNo(), writeSheetHolder);
writeTableHolder = null; writeTableHolder = null;
currentWriteHolder = writeSheetHolder; currentWriteHolder = writeSheetHolder;
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@ -142,15 +147,26 @@ public class WriteContextImpl implements WriteContext {
} }
private void initSheet(WriteTypeEnum writeType) { private void initSheet(WriteTypeEnum writeType) {
WriteHandlerUtils.beforeSheetCreate(this);
Sheet currentSheet; Sheet currentSheet;
try { try {
currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(writeSheetHolder.getSheetNo()); if (writeSheetHolder.getSheetNo() != null) {
writeSheetHolder currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(writeSheetHolder.getSheetNo());
.setCachedSheet(writeWorkbookHolder.getCachedWorkbook().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) { } catch (Exception e) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Can not find sheet:{} ,now create it", writeSheetHolder.getSheetNo()); 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()); currentSheet = WorkBookUtil.createSheet(writeWorkbookHolder.getWorkbook(), writeSheetHolder.getSheetName());
writeSheetHolder.setCachedSheet(currentSheet); writeSheetHolder.setCachedSheet(currentSheet);
} }
@ -160,6 +176,8 @@ public class WriteContextImpl implements WriteContext {
// Initialization head // Initialization head
initHead(writeSheetHolder.excelWriteHeadProperty()); initHead(writeSheetHolder.excelWriteHeadProperty());
} }
writeWorkbookHolder.getHasBeenInitializedSheetIndexMap().put(writeSheetHolder.getSheetNo(), writeSheetHolder);
writeWorkbookHolder.getHasBeenInitializedSheetNameMap().put(writeSheetHolder.getSheetName(), writeSheetHolder);
} }
public void initHead(ExcelWriteHeadProperty excelWriteHeadProperty) { public void initHead(ExcelWriteHeadProperty excelWriteHeadProperty) {

25
src/main/java/com/alibaba/excel/util/PositionUtils.java

@ -14,6 +14,16 @@ public class PositionUtils {
} }
return row; 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) { public static int getRow(String currentCellIndex) {
int row = 0; int row = 0;
@ -35,4 +45,19 @@ public class PositionUtils {
} }
return col - 1; 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;
}
}
} }

9
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.HolderEnum;
import com.alibaba.excel.enums.WriteLastRowTypeEnum; import com.alibaba.excel.enums.WriteLastRowTypeEnum;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
/** /**
@ -67,12 +68,12 @@ public class WriteSheetHolder extends AbstractWriteHolder {
public WriteSheetHolder(WriteSheet writeSheet, WriteWorkbookHolder writeWorkbookHolder) { public WriteSheetHolder(WriteSheet writeSheet, WriteWorkbookHolder writeWorkbookHolder) {
super(writeSheet, writeWorkbookHolder, writeWorkbookHolder.getWriteWorkbook().getConvertAllFiled()); super(writeSheet, writeWorkbookHolder, writeWorkbookHolder.getWriteWorkbook().getConvertAllFiled());
this.writeSheet = writeSheet; this.writeSheet = writeSheet;
this.sheetNo = writeSheet.getSheetNo(); if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) {
if (writeSheet.getSheetName() == null) { this.sheetNo = 0;
this.sheetName = writeSheet.getSheetNo().toString();
} else { } else {
this.sheetName = writeSheet.getSheetName(); this.sheetNo = writeSheet.getSheetNo();
} }
this.sheetName = writeSheet.getSheetName();
this.parentWriteWorkbookHolder = writeWorkbookHolder; this.parentWriteWorkbookHolder = writeWorkbookHolder;
this.hasBeenInitializedTable = new HashMap<Integer, WriteTableHolder>(); this.hasBeenInitializedTable = new HashMap<Integer, WriteTableHolder>();
if (writeWorkbookHolder.getTempTemplateInputStream() != null) { if (writeWorkbookHolder.getTempTemplateInputStream() != null) {

25
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 * prevent duplicate creation of sheet objects
*/ */
private Map<Integer, WriteSheetHolder> hasBeenInitializedSheet; private Map<Integer, WriteSheetHolder> hasBeenInitializedSheetIndexMap;
/**
* prevent duplicate creation of sheet objects
*/
private Map<String, WriteSheetHolder> hasBeenInitializedSheetNameMap;
/** /**
* Whether the encryption * Whether the encryption
*/ */
@ -148,7 +152,8 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
} else { } else {
this.mandatoryUseInputStream = writeWorkbook.getMandatoryUseInputStream(); this.mandatoryUseInputStream = writeWorkbook.getMandatoryUseInputStream();
} }
this.hasBeenInitializedSheet = new HashMap<Integer, WriteSheetHolder>(); this.hasBeenInitializedSheetIndexMap = new HashMap<Integer, WriteSheetHolder>();
this.hasBeenInitializedSheetNameMap = new HashMap<String, WriteSheetHolder>();
this.password = writeWorkbook.getPassword(); this.password = writeWorkbook.getPassword();
if (writeWorkbook.getInMemory() == null) { if (writeWorkbook.getInMemory() == null) {
this.inMemory = Boolean.FALSE; this.inMemory = Boolean.FALSE;
@ -197,12 +202,20 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
this.cachedWorkbook = cachedWorkbook; this.cachedWorkbook = cachedWorkbook;
} }
public Map<Integer, WriteSheetHolder> getHasBeenInitializedSheet() { public Map<Integer, WriteSheetHolder> getHasBeenInitializedSheetIndexMap() {
return hasBeenInitializedSheet; return hasBeenInitializedSheetIndexMap;
}
public void setHasBeenInitializedSheetIndexMap(Map<Integer, WriteSheetHolder> hasBeenInitializedSheetIndexMap) {
this.hasBeenInitializedSheetIndexMap = hasBeenInitializedSheetIndexMap;
}
public Map<String, WriteSheetHolder> getHasBeenInitializedSheetNameMap() {
return hasBeenInitializedSheetNameMap;
} }
public void setHasBeenInitializedSheet(Map<Integer, WriteSheetHolder> hasBeenInitializedSheet) { public void setHasBeenInitializedSheetNameMap(Map<String, WriteSheetHolder> hasBeenInitializedSheetNameMap) {
this.hasBeenInitializedSheet = hasBeenInitializedSheet; this.hasBeenInitializedSheetNameMap = hasBeenInitializedSheetNameMap;
} }
public WriteWorkbook getWriteWorkbook() { public WriteWorkbook getWriteWorkbook() {

25
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 horizontalFillTemplate07;
private static File fileHorizontal03; private static File fileHorizontal03;
private static File horizontalFillTemplate03; private static File horizontalFillTemplate03;
private static File byName07;
private static File byName03;
private static File byNameTemplate07;
private static File byNameTemplate03;
@BeforeClass @BeforeClass
public static void init() { public static void init() {
@ -54,6 +58,10 @@ public class FillDataTest {
horizontalFillTemplate07 = TestFileUtil.readFile("fill" + File.separator + "horizontal.xlsx"); horizontalFillTemplate07 = TestFileUtil.readFile("fill" + File.separator + "horizontal.xlsx");
fileHorizontal03 = TestFileUtil.createNewFile("fillHorizontal03.xls"); fileHorizontal03 = TestFileUtil.createNewFile("fillHorizontal03.xls");
horizontalFillTemplate03 = TestFileUtil.readFile("fill" + File.separator + "horizontal.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 @Test
@ -86,6 +94,23 @@ public class FillDataTest {
horizontalFill(fileHorizontal03, horizontalFillTemplate03); 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) { private void horizontalFill(File file, File template) {
ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build(); ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet = EasyExcel.writerSheet().build();

BIN
src/test/resources/fill/byName.xls

Binary file not shown.

BIN
src/test/resources/fill/byName.xlsx

Binary file not shown.

4
update.md

@ -1,3 +1,7 @@
# 2.1.5
* 修复部分xlsx没有行号读取异常
* 填充时候支持根据`sheetName`定位`sheet`
# 2.1.4 # 2.1.4
* 新增参数`useDefaultListener` 可以排除默认对象转换 * 新增参数`useDefaultListener` 可以排除默认对象转换

Loading…
Cancel
Save