Skip to content

Commit 26c9b98

Browse files
authored
Merge pull request #385 from liaochong/Hotfix/4.3.0.RC2
Hotfix/4.3.0.rc2
2 parents cb2ba3c + 0f1a8e8 commit 26c9b98

3 files changed

Lines changed: 102 additions & 23 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>com.github.liaochong</groupId>
1313
<artifactId>myexcel</artifactId>
14-
<version>4.3.0.RC2</version>
14+
<version>4.3.0.RC3</version>
1515
<packaging>jar</packaging>
1616

1717
<name>myexcel</name>

src/main/java/com/github/liaochong/myexcel/core/AbstractExcelFactory.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.github.liaochong.myexcel.core;
1717

1818
import com.github.liaochong.myexcel.core.parser.ContentTypeEnum;
19+
import com.github.liaochong.myexcel.core.parser.DropDownLists;
1920
import com.github.liaochong.myexcel.core.parser.HtmlTableParser;
2021
import com.github.liaochong.myexcel.core.parser.Td;
2122
import com.github.liaochong.myexcel.core.parser.Tr;
@@ -564,29 +565,38 @@ private Cell setLink(Td td, Row currentRow, HyperlinkType hyperlinkType) {
564565
}
565566

566567
private String setDropDownList(Td td, Sheet sheet, String content) {
567-
if (content.length() > 250) {
568-
throw new IllegalArgumentException("The total number of words in the drop-down list should not exceed 250.");
569-
}
570-
CellRangeAddressList addressList = new CellRangeAddressList(
568+
if (content != null && !content.isEmpty()) {
569+
CellRangeAddressList addressList = new CellRangeAddressList(
571570
td.row, td.getRowBound(), td.col, td.getColBound());
572-
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
573-
String[] list = content.split(",");
574-
DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(list);
575-
DataValidation validation = dvHelper.createValidation(
576-
dvConstraint, addressList);
577-
if (td.promptContainer != null) {
578-
validation.createPromptBox(td.promptContainer.title, td.promptContainer.text);
579-
validation.setShowPromptBox(true);
580-
}
581-
if (validation instanceof XSSFDataValidation) {
582-
validation.setSuppressDropDownArrow(true);
583-
validation.setShowErrorBox(true);
584-
} else {
585-
validation.setSuppressDropDownArrow(false);
586-
}
587-
sheet.addValidationData(validation);
588-
if (list.length > 0) {
589-
return list[0];
571+
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
572+
String[] list;
573+
DataValidation validation;
574+
if (content.length() <= 250) {
575+
list = content.split(",");
576+
DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(list);
577+
validation = dvHelper.createValidation(
578+
dvConstraint, addressList);
579+
580+
} else {
581+
DropDownLists.Index index = DropDownLists.getHiddenSheetIndex(content, workbook);
582+
list = new String[]{index.firstLine};
583+
validation = dvHelper.createValidation(dvHelper.createFormulaListConstraint(index.path), addressList);
584+
}
585+
586+
if (td.promptContainer != null) {
587+
validation.createPromptBox(td.promptContainer.title, td.promptContainer.text);
588+
validation.setShowPromptBox(true);
589+
}
590+
if (validation instanceof XSSFDataValidation) {
591+
validation.setSuppressDropDownArrow(true);
592+
validation.setShowErrorBox(true);
593+
} else {
594+
validation.setSuppressDropDownArrow(false);
595+
}
596+
sheet.addValidationData(validation);
597+
if (list.length > 0) {
598+
return list[0];
599+
}
590600
}
591601
return null;
592602
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.liaochong.myexcel.core.parser;
2+
3+
import org.apache.poi.ss.usermodel.Cell;
4+
import org.apache.poi.ss.usermodel.Row;
5+
import org.apache.poi.ss.usermodel.Sheet;
6+
import org.apache.poi.ss.usermodel.Workbook;
7+
8+
import java.util.Map;
9+
import java.util.Objects;
10+
import java.util.concurrent.ConcurrentHashMap;
11+
12+
public final class DropDownLists {
13+
14+
private static final Map<String, Index> INDEX = new ConcurrentHashMap<>();
15+
16+
private DropDownLists() {
17+
}
18+
19+
public static Index getHiddenSheetIndex(String input, Workbook workbook) {
20+
return INDEX.computeIfAbsent(input, hash -> createAndWriteHiddenSheet(input, workbook));
21+
}
22+
23+
private static Index createAndWriteHiddenSheet(String input, Workbook workbook) {
24+
String sheetName = "MyExcel_HiddenDat@List-0";
25+
Sheet sheet = workbook.getSheet(sheetName);
26+
if (sheet == null) {
27+
sheet = workbook.createSheet(sheetName);
28+
int index = workbook.getSheetIndex(sheet);
29+
workbook.setSheetHidden(index, true);
30+
}
31+
int rowNum = sheet.getLastRowNum() + 1;
32+
Row row = sheet.createRow(rowNum);
33+
String[] list = input.split(",");
34+
for (int i = 0; i < list.length; i++) {
35+
Cell cell = row.createCell(i);
36+
cell.setCellValue(list[i]);
37+
}
38+
int displayRowNum = rowNum + 1;
39+
return new Index(list[0], rowNum, "'" + sheetName + "'!$" + displayRowNum + ":$" + displayRowNum);
40+
}
41+
42+
public static class Index {
43+
44+
public String firstLine;
45+
46+
public int rowNum;
47+
48+
public String path;
49+
50+
public Index(String firstLine, int rowNum, String path) {
51+
this.firstLine = firstLine;
52+
this.rowNum = rowNum;
53+
this.path = path;
54+
}
55+
56+
@Override
57+
public boolean equals(Object o) {
58+
if (this == o) return true;
59+
if (!(o instanceof Index)) return false;
60+
Index index = (Index) o;
61+
return rowNum == index.rowNum && Objects.equals(firstLine, index.firstLine) && Objects.equals(path, index.path);
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return Objects.hash(firstLine, rowNum, path);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)