-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from liaochong/Hotfix/4.3.0.RC2
Hotfix/4.3.0.rc2
- Loading branch information
Showing
3 changed files
with
102 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/github/liaochong/myexcel/core/parser/DropDownLists.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.github.liaochong.myexcel.core.parser; | ||
|
||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public final class DropDownLists { | ||
|
||
private static final Map<String, Index> INDEX = new ConcurrentHashMap<>(); | ||
|
||
private DropDownLists() { | ||
} | ||
|
||
public static Index getHiddenSheetIndex(String input, Workbook workbook) { | ||
return INDEX.computeIfAbsent(input, hash -> createAndWriteHiddenSheet(input, workbook)); | ||
} | ||
|
||
private static Index createAndWriteHiddenSheet(String input, Workbook workbook) { | ||
String sheetName = "MyExcel_HiddenDat@List-0"; | ||
Sheet sheet = workbook.getSheet(sheetName); | ||
if (sheet == null) { | ||
sheet = workbook.createSheet(sheetName); | ||
int index = workbook.getSheetIndex(sheet); | ||
workbook.setSheetHidden(index, true); | ||
} | ||
int rowNum = sheet.getLastRowNum() + 1; | ||
Row row = sheet.createRow(rowNum); | ||
String[] list = input.split(","); | ||
for (int i = 0; i < list.length; i++) { | ||
Cell cell = row.createCell(i); | ||
cell.setCellValue(list[i]); | ||
} | ||
int displayRowNum = rowNum + 1; | ||
return new Index(list[0], rowNum, "'" + sheetName + "'!$" + displayRowNum + ":$" + displayRowNum); | ||
} | ||
|
||
public static class Index { | ||
|
||
public String firstLine; | ||
|
||
public int rowNum; | ||
|
||
public String path; | ||
|
||
public Index(String firstLine, int rowNum, String path) { | ||
this.firstLine = firstLine; | ||
this.rowNum = rowNum; | ||
this.path = path; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Index)) return false; | ||
Index index = (Index) o; | ||
return rowNum == index.rowNum && Objects.equals(firstLine, index.firstLine) && Objects.equals(path, index.path); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(firstLine, rowNum, path); | ||
} | ||
} | ||
} |