Skip to content

Commit

Permalink
support linkage
Browse files Browse the repository at this point in the history
  • Loading branch information
liaochong committed Dec 15, 2023
1 parent bc0d196 commit 92730f2
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
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 Down Expand Up @@ -147,6 +148,10 @@ 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 +228,12 @@ public ExcelFactory sheetStrategy(SheetStrategy sheetStrategy) {
return this;
}

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

protected String getRealSheetName(String sheetName) {
if (sheetName == null) {
sheetName = "Sheet";
Expand Down Expand Up @@ -289,6 +300,7 @@ 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 @@ -315,21 +327,33 @@ 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);
cellAddress = cell.getAddress();
if (td.dropdownList != null) {
referMapping.putIfAbsent(td.dropdownList.getName(), cellAddress);
}
String firstEle = setDropDownList(td, sheet, content, cellAddress);
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);
cellAddress = cell.getAddress();
if (td.dropdownList != null) {
referMapping.putIfAbsent(td.dropdownList.getName(), cellAddress);
}
firstEle = setDropDownList(td, sheet, content, cellAddress);
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);
cellAddress = cell.getAddress();
if (td.dropdownList != null) {
referMapping.putIfAbsent(td.dropdownList.getName(), cellAddress);
}
firstEle = setDropDownList(td, sheet, content, cellAddress);
if (firstEle != null) {
cell.setCellValue(firstEle);
}
Expand Down Expand Up @@ -563,25 +587,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 +614,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 Down Expand Up @@ -399,6 +401,17 @@ protected void setImage(Td td, int index) {
}
}

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

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 @@ -107,6 +108,8 @@ public class Configuration {
*/
public Map<Class<?>, Object> applicationBeans = Collections.emptyMap();

public Map<String, List<?>> nameMapping = Collections.emptyMap();

public void setWidthStrategy(WidthStrategy widthStrategy) {
this.widthStrategy = widthStrategy;
this.computeAutoWidth = WidthStrategy.isComputeAutoWidth(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.nameMapping(configuration.nameMapping);
if (workbook == null) {
htmlToExcelStreamFactory.workbookType(configuration.workbookType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.liaochong.myexcel.core.constant.FileType;
import com.github.liaochong.myexcel.core.constant.LinkType;
import com.github.liaochong.myexcel.core.converter.CustomWriteConverter;
import com.github.liaochong.myexcel.core.parser.DropdownList;
import com.github.liaochong.myexcel.core.parser.Image;
import com.github.liaochong.myexcel.utils.StringUtil;

Expand Down Expand Up @@ -104,6 +105,8 @@ public final class ExcelColumnMapping {

public Image image;

public DropdownList dropdownList;

public static ExcelColumnMapping mapping(ExcelColumn excelColumn) {
ExcelColumnMapping result = new ExcelColumnMapping();
result.title = excelColumn.title();
Expand Down Expand Up @@ -154,6 +157,11 @@ public static ExcelColumnMapping mapping(ExcelColumn excelColumn) {
if (image.height() > 0) {
result.image.setHeight(image.height());
}
com.github.liaochong.myexcel.core.annotation.DropdownList dr = excelColumn.dropdownList();
DropdownList drList = new DropdownList();
drList.setName(dr.name());
drList.setParent(dr.parent());
result.dropdownList = drList;
return result;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/github/liaochong/myexcel/core/ExcelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.github.liaochong.myexcel.core.strategy.WidthStrategy;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.List;
import java.util.Map;

/**
* @author liaochong
* @version 1.0
Expand Down Expand Up @@ -71,6 +74,16 @@ public interface ExcelFactory {
*/
ExcelFactory sheetStrategy(SheetStrategy sheetStrategy);

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

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

import com.github.liaochong.myexcel.core.constant.Constants;
import com.github.liaochong.myexcel.core.parser.DropDownLists;
import com.github.liaochong.myexcel.core.parser.StyleParser;
import com.github.liaochong.myexcel.core.parser.Table;
import com.github.liaochong.myexcel.core.parser.Td;
Expand All @@ -25,6 +26,7 @@
import com.github.liaochong.myexcel.utils.StringUtil;
import com.github.liaochong.myexcel.utils.TdUtil;
import com.github.liaochong.myexcel.utils.TempFileOperator;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
Expand Down Expand Up @@ -150,6 +152,8 @@ private void receive() {
if (this.workbook == null) {
workbookType(WorkbookType.SXLSX);
}
// 构建名称管理器
this.createNameManager();
if (isHssf) {
maxRowCountOfSheet = XLS_MAX_ROW_COUNT;
}
Expand Down Expand Up @@ -209,6 +213,19 @@ private void receive() {
}
}

private 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);
}
}

private void createNextSheet() {
if (rowNum >= maxRowCountOfSheet) {
sheetNum++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.liaochong.myexcel.core.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author liaochong
* @version 1.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
@Documented
public @interface DropdownList {

String name() default "";

String parent() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,6 @@
* @return 图片配置
*/
Image image() default @Image();

DropdownList dropdownList() default @DropdownList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 liaochong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.liaochong.myexcel.core.parser;

/**
* @author liaochong
* @version 1.0
*/
public class DropdownList {

private String name;

private String parent;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getParent() {
return parent;
}

public void setParent(String parent) {
this.parent = parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public class Td {

public Image image;

public DropdownList dropdownList;

public Td(int row, int col) {
this.row = row;
this.col = col;
Expand Down

0 comments on commit 92730f2

Please sign in to comment.