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 50e0c5e commit ceedf3c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
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 nameMapping(Map<String, List<?>> nameMapping) {
htmlToExcelFactory.nameMapping(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,6 +56,7 @@
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;
Expand Down Expand Up @@ -88,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 @@ -238,6 +241,20 @@ public ExcelFactory nameMapping(Map<String, List<?>> 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
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 nameMapping(Map<String, List<?>> nameMapping) {
return this;
}

/**
* 构建
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ Workbook build(List<Table> tables) {
} else {
buildTablesWithOneSheet(tables);
}
// 3.创建名称管理
this.createNameManager();
log.info("Build excel takes {} ms", System.currentTimeMillis() - startTime);
return workbook;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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 @@ -26,7 +25,6 @@
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 @@ -213,19 +211,6 @@ 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
Expand Up @@ -385,8 +385,21 @@ private void setTdContent(Element tdElement, Td td) {
td.link = link;
return;
}
if (tdElement.hasAttr("dropDownList")) {
if (tdElement.hasAttr("dropdownList") || tdElement.hasAttr("dropDownList")) {
td.tdContentType = ContentTypeEnum.DROP_DOWN_LIST;
String dropdownListName = tdElement.attr("dropdownList-name");
if (StringUtil.isNotBlank(dropdownListName)) {
DropdownList dropdownList = new DropdownList();
dropdownList.setName(dropdownListName);
td.dropdownList = dropdownList;
}
String dropdownListParent = tdElement.attr("dropdownList-parent");
if (StringUtil.isNotBlank(dropdownListParent)) {
if (td.dropdownList == null) {
td.dropdownList = new DropdownList();
}
td.dropdownList.setParent(dropdownListParent);
}
return;
}
if (Constants.TRUE.equals(content) || Constants.FALSE.equals(content)) {
Expand Down

0 comments on commit ceedf3c

Please sign in to comment.