作者 RuoYi

兼容Excel下拉框内容过多无法显示的问题(I5XB6I)

@@ -48,6 +48,7 @@ import org.apache.poi.ss.usermodel.FillPatternType; @@ -48,6 +48,7 @@ import org.apache.poi.ss.usermodel.FillPatternType;
48 import org.apache.poi.ss.usermodel.Font; 48 import org.apache.poi.ss.usermodel.Font;
49 import org.apache.poi.ss.usermodel.HorizontalAlignment; 49 import org.apache.poi.ss.usermodel.HorizontalAlignment;
50 import org.apache.poi.ss.usermodel.IndexedColors; 50 import org.apache.poi.ss.usermodel.IndexedColors;
  51 +import org.apache.poi.ss.usermodel.Name;
51 import org.apache.poi.ss.usermodel.PictureData; 52 import org.apache.poi.ss.usermodel.PictureData;
52 import org.apache.poi.ss.usermodel.Row; 53 import org.apache.poi.ss.usermodel.Row;
53 import org.apache.poi.ss.usermodel.Sheet; 54 import org.apache.poi.ss.usermodel.Sheet;
@@ -982,8 +983,16 @@ public class ExcelUtil<T> @@ -982,8 +983,16 @@ public class ExcelUtil<T>
982 } 983 }
983 if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0) 984 if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0)
984 { 985 {
985 - // 提示信息或只能选择不能输入的列内容.  
986 - setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column); 986 + if (attr.combo().length > 15 || StringUtils.join(attr.combo()).length() > 255)
  987 + {
  988 + // 如果下拉数大于15或字符串长度大于255,则使用一个新sheet存储,避免生成的模板下拉值获取不到
  989 + setXSSFValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
  990 + }
  991 + else
  992 + {
  993 + // 提示信息或只能选择不能输入的列内容.
  994 + setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
  995 + }
987 } 996 }
988 } 997 }
989 998
@@ -1088,6 +1097,58 @@ public class ExcelUtil<T> @@ -1088,6 +1097,58 @@ public class ExcelUtil<T>
1088 } 1097 }
1089 1098
1090 /** 1099 /**
  1100 + * 设置某些列的值只能输入预制的数据,显示下拉框(兼容超出一定数量的下拉框).
  1101 + *
  1102 + * @param sheet 要设置的sheet.
  1103 + * @param textlist 下拉框显示的内容
  1104 + * @param promptContent 提示内容
  1105 + * @param firstRow 开始行
  1106 + * @param endRow 结束行
  1107 + * @param firstCol 开始列
  1108 + * @param endCol 结束列
  1109 + */
  1110 + public void setXSSFValidationWithHidden(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow, int firstCol, int endCol)
  1111 + {
  1112 + String hideSheetName = "combo_" + firstCol + "_" + endCol;
  1113 + Sheet hideSheet = wb.createSheet(hideSheetName); // 用于存储 下拉菜单数据
  1114 + for (int i = 0; i < textlist.length; i++)
  1115 + {
  1116 + hideSheet.createRow(i).createCell(0).setCellValue(textlist[i]);
  1117 + }
  1118 + // 创建名称,可被其他单元格引用
  1119 + Name name = wb.createName();
  1120 + name.setNameName(hideSheetName + "_data");
  1121 + name.setRefersToFormula(hideSheetName + "!$A$1:$A$" + textlist.length);
  1122 + DataValidationHelper helper = sheet.getDataValidationHelper();
  1123 + // 加载下拉列表内容
  1124 + DataValidationConstraint constraint = helper.createFormulaListConstraint(hideSheetName + "_data");
  1125 + // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
  1126 + CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
  1127 + // 数据有效性对象
  1128 + DataValidation dataValidation = helper.createValidation(constraint, regions);
  1129 + if (StringUtils.isNotEmpty(promptContent))
  1130 + {
  1131 + // 如果设置了提示信息则鼠标放上去提示
  1132 + dataValidation.createPromptBox("", promptContent);
  1133 + dataValidation.setShowPromptBox(true);
  1134 + }
  1135 + // 处理Excel兼容性问题
  1136 + if (dataValidation instanceof XSSFDataValidation)
  1137 + {
  1138 + dataValidation.setSuppressDropDownArrow(true);
  1139 + dataValidation.setShowErrorBox(true);
  1140 + }
  1141 + else
  1142 + {
  1143 + dataValidation.setSuppressDropDownArrow(false);
  1144 + }
  1145 +
  1146 + sheet.addValidationData(dataValidation);
  1147 + // 设置hiddenSheet隐藏
  1148 + wb.setSheetHidden(wb.getSheetIndex(hideSheet), true);
  1149 + }
  1150 +
  1151 + /**
1091 * 解析导出值 0=男,1=女,2=未知 1152 * 解析导出值 0=男,1=女,2=未知
1092 * 1153 *
1093 * @param propertyValue 参数值 1154 * @param propertyValue 参数值