Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/4.5.0 #412

Merged
merged 10 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -24,6 +24,7 @@
import org.apache.poi.ss.usermodel.Workbook;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -90,6 +91,12 @@ public AbstractExcelBuilder freezePanes(FreezePane... freezePanes) {
return this;
}

@Override
public ExcelBuilder nameManager(Map<String, List<?>> nameMapping) {
htmlToExcelFactory.nameManager(nameMapping);
return this;
}

/**
* 构建
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.constant.Constants;
import com.github.liaochong.myexcel.core.parser.ContentTypeEnum;
import com.github.liaochong.myexcel.core.parser.DropDownLists;
import com.github.liaochong.myexcel.core.parser.HtmlTableParser;
Expand Down Expand Up @@ -55,12 +56,14 @@
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.util.IOUtils;
Expand All @@ -87,6 +90,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author liaochong
Expand Down Expand Up @@ -147,6 +151,14 @@ 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 @@ -223,6 +235,26 @@ public ExcelFactory sheetStrategy(SheetStrategy sheetStrategy) {
return this;
}

@Override
public ExcelFactory nameManager(Map<String, List<?>> nameMapping) {
this.nameMapping = nameMapping;
return this;
}

protected void createNameManager() {
if (nameMapping.isEmpty()) {
return;
}
for (Map.Entry<String, List<?>> entry : nameMapping.entrySet()) {
Name name = workbook.createName();
name.setNameName(entry.getKey());
String content = entry.getValue().stream().map(String::valueOf).collect(Collectors.joining(Constants.COMMA));
DropDownLists.Index index = DropDownLists.getHiddenSheetIndex(content, workbook);
name.setRefersToFormula(index.path);
}
}


protected String getRealSheetName(String sheetName) {
if (sheetName == null) {
sheetName = "Sheet";
Expand Down Expand Up @@ -315,21 +347,21 @@ protected void createCell(Td td, Sheet sheet, Row currentRow) {
break;
case NUMBER_DROP_DOWN_LIST:
cell = currentRow.createCell(td.col, CellType.NUMERIC);
String firstEle = setDropDownList(td, sheet, content);
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);
firstEle = setDropDownList(td, sheet, content);
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);
firstEle = setDropDownList(td, sheet, content);
firstEle = this.process(td, sheet, cell, content);
if (firstEle != null) {
cell.setCellValue(firstEle);
}
Expand Down Expand Up @@ -368,6 +400,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 Expand Up @@ -525,6 +565,9 @@ private void setImage(Td td, Sheet sheet) {
ClientAnchor anchor = createHelper.createClientAnchor();
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
Image image = td.getImage();
if (image == null) {
image = new Image();
}
anchor.setDx1(isHssf ? (image.getMarginLeft() > 0 ? image.getMarginLeft() : 2) : Units.pixelToEMU(image.getMarginLeft() > 0 ? image.getMarginLeft() : 3));
anchor.setDy1(isHssf ? (image.getMarginTop() > 0 ? image.getMarginTop() : 1) : Units.pixelToEMU(image.getMarginTop() > 0 ? image.getMarginTop() : 3));
anchor.setCol1(td.col);
Expand Down Expand Up @@ -563,25 +606,22 @@ private Cell setLink(Td td, Row currentRow, HyperlinkType hyperlinkType) {
return cell;
}

private String setDropDownList(Td td, Sheet sheet, String content) {
private String setDropDownList(Td td, Sheet sheet, String content, CellAddress cellAddress) {
if (content != null && !content.isEmpty()) {
CellRangeAddressList addressList = new CellRangeAddressList(
td.row, td.getRowBound(), td.col, td.getColBound());
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
String[] list;
DropDownLists.Index index = DropDownLists.getHiddenSheetIndex(content, workbook);
String[] list = new String[]{index.firstLine};
DataValidation validation;
if (content.length() <= 250) {
list = content.split(",");
DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(list);
validation = dvHelper.createValidation(
dvConstraint, addressList);

boolean linkage = td.dropdownList != null && StringUtil.isNotBlank(td.dropdownList.getParent());
if (linkage) {
CellAddress parentCellAddress = referMapping.get(td.dropdownList.getParent());
String refer = new CellAddress(cellAddress.getRow(), parentCellAddress.getColumn()).formatAsString();
validation = dvHelper.createValidation(dvHelper.createFormulaListConstraint("=INDIRECT($" + refer + ")"), addressList);
} else {
DropDownLists.Index index = DropDownLists.getHiddenSheetIndex(content, workbook);
list = new String[]{index.firstLine};
validation = dvHelper.createValidation(dvHelper.createFormulaListConstraint(index.path), addressList);
}

if (td.promptContainer != null) {
validation.createPromptBox(td.promptContainer.title, td.promptContainer.text);
validation.setShowPromptBox(true);
Expand All @@ -593,9 +633,7 @@ private String setDropDownList(Td td, Sheet sheet, String content) {
validation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(validation);
if (list.length > 0) {
return list[0];
}
return linkage ? null : list[0];
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ protected List<Tr> createThead() {
td.content = multiTitles[j];
this.setPrompt(td, i);
this.setImage(td, i);
this.setDropdownList(td, i);
tds.add(td);
}
tdLists.add(tds);
Expand Down Expand Up @@ -342,6 +343,7 @@ protected Tr createTr(List<Pair<? extends Class, ?>> contents) {
this.setFormula(index, td);
this.setPrompt(td, index);
this.setImage(td, index);
this.setDropdownList(td, index);
}
this.setTdWidth(tr.colWidthMap, td);
return td;
Expand All @@ -367,38 +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) {
ExcelColumnMapping excelColumnMapping = getExcelColumnMapping(index);
if (excelColumnMapping == null) {
return;
}
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 @@ -19,6 +19,7 @@

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -106,6 +107,10 @@ public class Configuration {
* 绑定的上下文,适用spring等容器环境
*/
public Map<Class<?>, Object> applicationBeans = Collections.emptyMap();
/**
* 名称管理映射
*/
public Map<String, List<?>> nameMapping = Collections.emptyMap();

public void setWidthStrategy(WidthStrategy widthStrategy) {
this.widthStrategy = widthStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ public DefaultStreamExcelBuilder<T> autoMerge() {
return this;
}

public DefaultStreamExcelBuilder<T> nameManager(Map<String, List<?>> nameMapping) {
this.configuration.nameMapping = nameMapping;
return this;
}

/**
* 流式构建启动,包含一些初始化操作
*
Expand All @@ -320,6 +325,7 @@ public DefaultStreamExcelBuilder<T> start() {
context.styleParser = styleParser;
htmlToExcelStreamFactory = new HtmlToExcelStreamFactory(context);
htmlToExcelStreamFactory.widthStrategy(configuration.widthStrategy);
htmlToExcelStreamFactory.nameManager(configuration.nameMapping);
if (workbook == null) {
htmlToExcelStreamFactory.workbookType(configuration.workbookType);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/github/liaochong/myexcel/core/ExcelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.poi.ss.usermodel.Workbook;

import java.io.Closeable;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -112,6 +113,16 @@ public interface ExcelBuilder extends Closeable {
*/
ExcelBuilder fileTemplate(String dirPath, String fileName);

/**
* 指定名称管理器
*
* @param nameMapping 名称映射
* @return ExcelFactory
*/
default ExcelBuilder nameManager(Map<String, List<?>> nameMapping) {
return this;
}

/**
* 构建
*
Expand Down
Loading
Loading