Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
liaochong committed Dec 23, 2023
1 parent 7c4fb22 commit 50e0c5e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>4.4.2</version>
<version>4.5.0</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,13 @@ public abstract class AbstractExcelFactory implements ExcelFactory {
* 生成sheet策略,默认生成多个sheet
*/
protected SheetStrategy sheetStrategy = SheetStrategy.MULTI_SHEET;

/**
* 用于保存名称管理
*/
protected Map<String, List<?>> nameMapping = Collections.emptyMap();

/**
* 用于保存下拉列表所需引用
*/
protected Map<String, CellAddress> referMapping = new HashMap<>();
/**
* 暂存单元格,由后续行认领
Expand Down Expand Up @@ -300,7 +304,6 @@ protected void createCell(Td td, Sheet sheet, Row currentRow) {
cell = currentRow.createCell(td.col, CellType.FORMULA);
cell.setCellFormula(td.content);
} else {
CellAddress cellAddress;
String content = td.content;
switch (td.tdContentType) {
case DOUBLE:
Expand All @@ -327,33 +330,21 @@ protected void createCell(Td td, Sheet sheet, Row currentRow) {
break;
case NUMBER_DROP_DOWN_LIST:
cell = currentRow.createCell(td.col, CellType.NUMERIC);
cellAddress = cell.getAddress();
if (td.dropdownList != null) {
referMapping.putIfAbsent(td.dropdownList.getName(), cellAddress);
}
String firstEle = setDropDownList(td, sheet, content, cellAddress);
String firstEle = this.process(td, sheet, cell, content);
if (firstEle != null) {
cell.setCellValue(Double.parseDouble(firstEle));
}
break;
case BOOLEAN_DROP_DOWN_LIST:
cell = currentRow.createCell(td.col, CellType.BOOLEAN);
cellAddress = cell.getAddress();
if (td.dropdownList != null) {
referMapping.putIfAbsent(td.dropdownList.getName(), cellAddress);
}
firstEle = setDropDownList(td, sheet, content, cellAddress);
firstEle = this.process(td, sheet, cell, content);
if (firstEle != null) {
cell.setCellValue(Boolean.parseBoolean(firstEle));
}
break;
case DROP_DOWN_LIST:
cell = currentRow.createCell(td.col, CellType.STRING);
cellAddress = cell.getAddress();
if (td.dropdownList != null) {
referMapping.putIfAbsent(td.dropdownList.getName(), cellAddress);
}
firstEle = setDropDownList(td, sheet, content, cellAddress);
firstEle = this.process(td, sheet, cell, content);
if (firstEle != null) {
cell.setCellValue(firstEle);
}
Expand Down Expand Up @@ -392,6 +383,14 @@ protected void createCell(Td td, Sheet sheet, Row currentRow) {
}
}

private String process(Td td, Sheet sheet, Cell cell, String content) {
CellAddress cellAddress = cell.getAddress();
if (td.dropdownList != null) {
referMapping.putIfAbsent(td.dropdownList.getName(), cellAddress);
}
return this.setDropDownList(td, sheet, content, cellAddress);
}

private void setComment(Td td, Sheet sheet, Cell cell) {
if (td.comment == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,49 +369,53 @@ private void setTdWidth(Map<Integer, Integer> colWidthMap, Td td) {
}

private void setFormula(int i, Td td) {
if (filteredFields.isEmpty()) {
ExcelColumnMapping excelColumnMapping = getExcelColumnMapping(i);
if (excelColumnMapping == null) {
return;
}
FieldDefinition fieldDefinition = filteredFields.get(i);
ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(fieldDefinition.getField());
if (excelColumnMapping != null && excelColumnMapping.formula) {
if (excelColumnMapping.formula) {
td.formula = true;
}
}

protected void setPrompt(Td td, int index) {
if (filteredFields.isEmpty()) {
ExcelColumnMapping excelColumnMapping = getExcelColumnMapping(index);
if (excelColumnMapping == null) {
return;
}
FieldDefinition fieldDefinition = filteredFields.get(index);
ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(fieldDefinition.getField());
if (excelColumnMapping != null && excelColumnMapping.promptContainer != null) {
if (excelColumnMapping.promptContainer != null) {
td.promptContainer = excelColumnMapping.promptContainer;
}
}

protected void setImage(Td td, int index) {
if (filteredFields.isEmpty()) {
ExcelColumnMapping excelColumnMapping = getExcelColumnMapping(index);
if (excelColumnMapping == null) {
return;
}
FieldDefinition fieldDefinition = filteredFields.get(index);
ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(fieldDefinition.getField());
if (excelColumnMapping != null && excelColumnMapping.image != null) {
if (excelColumnMapping.image != null) {
td.image = excelColumnMapping.image;
}
}

protected void setDropdownList(Td td, int index) {
if (filteredFields.isEmpty()) {
ExcelColumnMapping excelColumnMapping = getExcelColumnMapping(index);
if (excelColumnMapping == null) {
return;
}
FieldDefinition fieldDefinition = filteredFields.get(index);
ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(fieldDefinition.getField());
if (excelColumnMapping != null && excelColumnMapping.dropdownList != null) {
if (excelColumnMapping.dropdownList != null) {
td.dropdownList = excelColumnMapping.dropdownList;
}
}

private ExcelColumnMapping getExcelColumnMapping(int index) {
if (filteredFields.isEmpty()) {
return null;
}
FieldDefinition fieldDefinition = filteredFields.get(index);
return excelColumnMappingMap.get(fieldDefinition.getField());
}

private void setTdContent(Td td, Pair<? extends Class, ?> pair) {
Class fieldType = pair.getKey();
if (fieldType == NullType.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public class Configuration {
* 绑定的上下文,适用spring等容器环境
*/
public Map<Class<?>, Object> applicationBeans = Collections.emptyMap();

/**
* 名称管理映射
*/
public Map<String, List<?>> nameMapping = Collections.emptyMap();

public void setWidthStrategy(WidthStrategy widthStrategy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,10 @@
*/
Image image() default @Image();

/**
* 下拉列表配置
*
* @return 下拉列表配置
*/
DropdownList dropdownList() default @DropdownList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package com.github.liaochong.myexcel.core.parser;

/**
* 下拉列表配置
*
* @author liaochong
* @version 1.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ public class Td {
* 批注
*/
public Comment comment;

/**
* 图片配置
*/
public Image image;

/**
* 下拉列表配置
*/
public DropdownList dropdownList;

public Td(int row, int col) {
Expand Down

0 comments on commit 50e0c5e

Please sign in to comment.