|
@@ -1,7 +1,9 @@
|
|
|
package cn.iocoder.yudao.framework.excel.core.handler;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
|
|
+import cn.iocoder.yudao.framework.excel.core.enums.ExcelColumn;
|
|
|
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
|
|
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
|
|
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
|
@@ -11,6 +13,8 @@ import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
|
import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
* 基于固定 sheet 实现下拉框
|
|
@@ -20,18 +24,20 @@ import java.util.List;
|
|
|
public class SelectSheetWriteHandler implements SheetWriteHandler {
|
|
|
|
|
|
private static final String DICT_SHEET_NAME = "字典sheet";
|
|
|
+ public static final int FIRST_ROW = 1;
|
|
|
+ public static final int LAST_ROW = 2000;
|
|
|
+ private final List<KeyValue<ExcelColumn, List<String>>> selectMap;
|
|
|
|
|
|
-
|
|
|
- private final List<KeyValue<Integer, List<String>>> selectMap;
|
|
|
-
|
|
|
- private static final char[] ALPHABET = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
|
|
|
- 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
|
|
|
-
|
|
|
- public SelectSheetWriteHandler(List<KeyValue<Integer, List<String>>> selectMap) {
|
|
|
+ public SelectSheetWriteHandler(List<KeyValue<ExcelColumn, List<String>>> selectMap) {
|
|
|
if (CollUtil.isEmpty(selectMap)) {
|
|
|
this.selectMap = null;
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ Map<String, Long> nameCounts = selectMap.stream()
|
|
|
+ .collect(Collectors.groupingBy(item -> item.getKey().name(), Collectors.counting()));
|
|
|
+ Assert.isFalse(nameCounts.entrySet().stream().allMatch(entry -> entry.getValue() > 1), "下拉数据 key 重复请排查!!!");
|
|
|
+
|
|
|
selectMap.sort(Comparator.comparing(item -> item.getValue().size()));
|
|
|
this.selectMap = selectMap;
|
|
|
}
|
|
@@ -41,16 +47,14 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
|
|
|
if (CollUtil.isEmpty(selectMap)) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- Sheet currentSheet = writeSheetHolder.getSheet();
|
|
|
- DataValidationHelper helper = currentSheet.getDataValidationHelper();
|
|
|
- Workbook workbook = writeWorkbookHolder.getWorkbook();
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ DataValidationHelper helper = writeSheetHolder.getSheet().getDataValidationHelper();
|
|
|
+ Workbook workbook = writeWorkbookHolder.getWorkbook();
|
|
|
+
|
|
|
+
|
|
|
Sheet dictSheet = workbook.createSheet(DICT_SHEET_NAME);
|
|
|
- for (KeyValue<Integer, List<String>> keyValue : selectMap) {
|
|
|
-
|
|
|
- CellRangeAddressList rangeAddressList = new CellRangeAddressList(1, 65533, keyValue.getKey(), keyValue.getKey());
|
|
|
+ for (KeyValue<ExcelColumn, List<String>> keyValue : selectMap) {
|
|
|
int rowLen = keyValue.getValue().size();
|
|
|
|
|
|
for (int i = 0; i < rowLen; i++) {
|
|
@@ -58,58 +62,42 @@ public class SelectSheetWriteHandler implements SheetWriteHandler {
|
|
|
if (row == null) {
|
|
|
row = dictSheet.createRow(i);
|
|
|
}
|
|
|
- row.createCell(keyValue.getKey()).setCellValue(keyValue.getValue().get(i));
|
|
|
+ row.createCell(keyValue.getKey().getColNum()).setCellValue(keyValue.getValue().get(i));
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- Name name = workbook.createName();
|
|
|
-
|
|
|
- String excelColumn = getExcelColumn(keyValue.getKey());
|
|
|
- String refers = DICT_SHEET_NAME + "!$" + excelColumn + "$1:$" + excelColumn + "$" + rowLen;
|
|
|
- name.setNameName("dict" + keyValue.getKey());
|
|
|
- name.setRefersToFormula(refers);
|
|
|
-
|
|
|
-
|
|
|
- DataValidationConstraint constraint = helper.createFormulaListConstraint("dict" + keyValue.getKey());
|
|
|
- DataValidation validation = helper.createValidation(constraint, rangeAddressList);
|
|
|
- if (validation instanceof HSSFDataValidation) {
|
|
|
- validation.setSuppressDropDownArrow(false);
|
|
|
- } else {
|
|
|
- validation.setSuppressDropDownArrow(true);
|
|
|
- validation.setShowErrorBox(true);
|
|
|
- }
|
|
|
-
|
|
|
- validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
|
|
|
- validation.createErrorBox("提示", "此值不存在于下拉选择中!");
|
|
|
-
|
|
|
- writeSheetHolder.getSheet().addValidationData(validation);
|
|
|
+
|
|
|
+ setColSelect(writeSheetHolder, workbook, helper, keyValue);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- * 将数字列转化成为字母列
|
|
|
- *
|
|
|
- * @param num 数字
|
|
|
- * @return 字母
|
|
|
+ * 设置单元格下拉选择
|
|
|
*/
|
|
|
-
|
|
|
- private String getExcelColumn(int num) {
|
|
|
- String column;
|
|
|
- int len = ALPHABET.length - 1;
|
|
|
- int first = num / len;
|
|
|
- int second = num % len;
|
|
|
- if (num <= len) {
|
|
|
- column = ALPHABET[num] + "";
|
|
|
+ private static void setColSelect(WriteSheetHolder writeSheetHolder, Workbook workbook, DataValidationHelper helper,
|
|
|
+ KeyValue<ExcelColumn, List<String>> keyValue) {
|
|
|
+
|
|
|
+ Name name = workbook.createName();
|
|
|
+ String excelColumn = keyValue.getKey().name();
|
|
|
+
|
|
|
+ String refers = DICT_SHEET_NAME + "!$" + excelColumn + "$1:$" + excelColumn + "$" + keyValue.getValue().size();
|
|
|
+ name.setNameName("dict" + keyValue.getKey());
|
|
|
+ name.setRefersToFormula(refers);
|
|
|
+
|
|
|
+ DataValidationConstraint constraint = helper.createFormulaListConstraint("dict" + keyValue.getKey());
|
|
|
+
|
|
|
+ CellRangeAddressList rangeAddressList = new CellRangeAddressList(FIRST_ROW, LAST_ROW,
|
|
|
+ keyValue.getKey().getColNum(), keyValue.getKey().getColNum());
|
|
|
+ DataValidation validation = helper.createValidation(constraint, rangeAddressList);
|
|
|
+ if (validation instanceof HSSFDataValidation) {
|
|
|
+ validation.setSuppressDropDownArrow(false);
|
|
|
} else {
|
|
|
- column = ALPHABET[first - 1] + "";
|
|
|
- if (second == 0) {
|
|
|
- column = column + ALPHABET[len];
|
|
|
- } else {
|
|
|
- column = column + ALPHABET[second - 1];
|
|
|
- }
|
|
|
+ validation.setSuppressDropDownArrow(true);
|
|
|
+ validation.setShowErrorBox(true);
|
|
|
}
|
|
|
- return column;
|
|
|
+
|
|
|
+ validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
|
|
|
+ validation.createErrorBox("提示", "此值不存在于下拉选择中!");
|
|
|
+
|
|
|
+ writeSheetHolder.getSheet().addValidationData(validation);
|
|
|
}
|
|
|
|
|
|
}
|