Browse Source

Merge pull request #732 from alibaba/2.1.x

2.1.x
developing
Jiaju Zhuang 5 years ago committed by GitHub
parent
commit
e8502bc9c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pom.xml
  2. 44
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  3. 5
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
  4. 2
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  5. 40
      src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java
  6. 16
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
  7. 5
      update.md

2
pom.xml

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

44
src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -279,7 +279,12 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (cell == null) {
continue;
}
boolean needFill =
prepareData(cell.getStringCellValue(), analysisCellList, collectionAnalysisCellList, i, j);
// Prevent empty data from not being replaced
if (needFill) {
cell.setCellValue(StringUtils.EMPTY);
}
}
}
templateAnalysisCache.put(sheetNo, analysisCellList);
@ -287,10 +292,20 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
return analysisCache.get(sheetNo);
}
private void prepareData(String value, List<AnalysisCell> analysisCellList,
/**
* To prepare data
*
* @param value
* @param analysisCellList
* @param collectionAnalysisCellList
* @param rowIndex
* @param columnIndex
* @return Is a cell to be filled
*/
private boolean prepareData(String value, List<AnalysisCell> analysisCellList,
List<AnalysisCell> collectionAnalysisCellList, int rowIndex, int columnIndex) {
if (StringUtils.isEmpty(value)) {
return;
return false;
}
AnalysisCell analysisCell = null;
int startIndex = 0;
@ -322,15 +337,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
}
if (analysisCell == null) {
analysisCell = new AnalysisCell();
analysisCell.setRowIndex(rowIndex);
analysisCell.setColumnIndex(columnIndex);
analysisCell.setOnlyOneVariable(Boolean.TRUE);
List<String> variableList = new ArrayList<String>();
analysisCell.setVariableList(variableList);
List<String> prepareDataList = new ArrayList<String>();
analysisCell.setPrepareDataList(prepareDataList);
analysisCell.setCellType(WriteTemplateAnalysisCellTypeEnum.COMMON);
analysisCell = initAnalysisCell(rowIndex, columnIndex);
}
String variable = value.substring(prefixIndex + 1, suffixIndex);
if (StringUtils.isEmpty(variable)) {
@ -365,7 +372,22 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
} else {
collectionAnalysisCellList.add(analysisCell);
}
return true;
}
return false;
}
private AnalysisCell initAnalysisCell(Integer rowIndex, Integer columnIndex) {
AnalysisCell analysisCell = new AnalysisCell();
analysisCell.setRowIndex(rowIndex);
analysisCell.setColumnIndex(columnIndex);
analysisCell.setOnlyOneVariable(Boolean.TRUE);
List<String> variableList = new ArrayList<String>();
analysisCell.setVariableList(variableList);
List<String> prepareDataList = new ArrayList<String>();
analysisCell.setPrepareDataList(prepareDataList);
analysisCell.setCellType(WriteTemplateAnalysisCellTypeEnum.COMMON);
return analysisCell;
}
private String convertPrepareData(String prepareData) {

5
src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java

@ -156,6 +156,11 @@ public class WriteSheetHolder extends AbstractWriteHolder {
int newRowIndex = 0;
switch (writeLastRowTypeEnum) {
case TEMPLATE_EMPTY:
newRowIndex = Math.max(sheet.getLastRowNum(), cachedSheet.getLastRowNum());
if (newRowIndex != 0 || cachedSheet.getRow(0) != null) {
newRowIndex++;
}
break;
case HAS_DATA:
newRowIndex = Math.max(sheet.getLastRowNum(), cachedSheet.getLastRowNum());
newRowIndex++;

2
src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java

@ -146,7 +146,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
byte[] templateFileByte = null;
if (writeWorkbook.getTemplateFile() != null) {
templateFileByte = FileUtils.readFileToByteArray(writeWorkbook.getTemplateFile());
} else if (writeWorkbook.getTemplateInputStream() == null) {
} else if (writeWorkbook.getTemplateInputStream() != null) {
try {
templateFileByte = IoUtils.toByteArray(writeWorkbook.getTemplateInputStream());
} finally {

40
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java

@ -96,43 +96,23 @@ public class PoiTest {
@Test
public void cp() throws IOException, InvalidFormatException {
String file = "d://test/tt.xlsx";
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new File(file));
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
XSSFRow row = xssfSheet.getRow(2);
xssfSheet.removeRow(row);
// Row r2= xssfSheet.createRow(2);
// r2.createCell(1);
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(xssfWorkbook);
SXSSFSheet sxssfSheet = sxssfWorkbook.getSheetAt(0);
sxssfSheet.createRow(2);
FileOutputStream fileout = new FileOutputStream("d://test/r2" + System.currentTimeMillis() + ".xlsx");
sxssfWorkbook.write(fileout);
sxssfWorkbook.dispose();
sxssfWorkbook.close();
xssfWorkbook.close();
SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file));
SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum());
SXSSFRow row = xssfSheet.getRow(0);
LOGGER.info("第一行数据:{}", row);
xssfSheet.createRow(20);
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum());
}
@Test
public void lastRowNum233443() throws IOException, InvalidFormatException {
String file = "d://test/tt.xlsx";
String file = "d://test/em0.xlsx";
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new File(file));
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(xssfWorkbook);
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
XSSFRow row = xssfSheet.getRow(2);
xssfSheet.removeRow(row);
new CellCopyPolicy().createBuilder().build();
System.out.println(xssfSheet.getLastRowNum());
System.out.println(xssfSheet.getRow(0));
FileOutputStream fileout = new FileOutputStream("d://test/r2" + System.currentTimeMillis() + ".xlsx");
sxssfWorkbook.write(fileout);
sxssfWorkbook.dispose();
sxssfWorkbook.close();
xssfWorkbook.close();
}
@Test

16
src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java

@ -5,12 +5,15 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.large.LargeData;
import com.alibaba.easyexcel.test.core.simple.SimpleData;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
@ -18,6 +21,8 @@ import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import net.sf.cglib.beans.BeanMap;
/**
* 测试poi
*
@ -27,6 +32,17 @@ import com.alibaba.fastjson.JSON;
public class Wirte {
private static final Logger LOGGER = LoggerFactory.getLogger(Wirte.class);
@Test
public void simpleWrite1() {
LargeData ss = new LargeData();
ss.setStr23("ttt");
Map map = BeanMap.create(ss);
System.out.println(map.containsKey("str23"));
System.out.println(map.containsKey("str22"));
System.out.println(map.get("str23"));
System.out.println(map.get("str22"));
}
@Test
public void simpleWrite() {
// 写法1

5
update.md

@ -1,3 +1,8 @@
# 2.1.0-beta2
* 修改模板通过流创建报错的bug
* 修复空数据未替换掉的bug
* 修复空模板会空一行的bug
# 2.1.0-beta1
* 新增支持导入、导出支持公式
* 新增支持读取单元格类型、写入指定单元格类型

Loading…
Cancel
Save