作者 RuoYi

修复Excel注解prompt/combo同时使用不生效问题

@@ -786,17 +786,10 @@ public class ExcelUtil<T> @@ -786,17 +786,10 @@ public class ExcelUtil<T>
786 // 设置列宽 786 // 设置列宽
787 sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256)); 787 sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
788 } 788 }
789 - // 如果设置了提示信息则鼠标放上去提示.  
790 - if (StringUtils.isNotEmpty(attr.prompt())) 789 + if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0)
791 { 790 {
792 - // 这里默认设了2-101列提示.  
793 - setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);  
794 - }  
795 - // 如果设置了combo属性则本列只能选择不能输入  
796 - if (attr.combo().length > 0)  
797 - {  
798 - // 这里默认设了2-101列只能选择不能输入.  
799 - setXSSFValidation(sheet, attr.combo(), 1, 100, column, column); 791 + // 提示信息或只能选择不能输入的列内容.
  792 + setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
800 } 793 }
801 } 794 }
802 795
@@ -860,48 +853,29 @@ public class ExcelUtil<T> @@ -860,48 +853,29 @@ public class ExcelUtil<T>
860 } 853 }
861 854
862 /** 855 /**
863 - * 设置 POI XSSFSheet 单元格提示 856 + * 设置 POI XSSFSheet 单元格提示或选择框
864 * 857 *
865 * @param sheet 表单 858 * @param sheet 表单
866 - * @param promptTitle 提示标题 859 + * @param textlist 下拉框显示的内容
867 * @param promptContent 提示内容 860 * @param promptContent 提示内容
868 * @param firstRow 开始行 861 * @param firstRow 开始行
869 * @param endRow 结束行 862 * @param endRow 结束行
870 * @param firstCol 开始列 863 * @param firstCol 开始列
871 * @param endCol 结束列 864 * @param endCol 结束列
872 */ 865 */
873 - public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow, 866 + public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
874 int firstCol, int endCol) 867 int firstCol, int endCol)
875 { 868 {
876 DataValidationHelper helper = sheet.getDataValidationHelper(); 869 DataValidationHelper helper = sheet.getDataValidationHelper();
877 - DataValidationConstraint constraint = helper.createCustomConstraint("DD1"); 870 + DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
878 CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); 871 CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
879 DataValidation dataValidation = helper.createValidation(constraint, regions); 872 DataValidation dataValidation = helper.createValidation(constraint, regions);
880 - dataValidation.createPromptBox(promptTitle, promptContent);  
881 - dataValidation.setShowPromptBox(true);  
882 - sheet.addValidationData(dataValidation);  
883 - }  
884 -  
885 - /**  
886 - * 设置某些列的值只能输入预制的数据,显示下拉框.  
887 - *  
888 - * @param sheet 要设置的sheet.  
889 - * @param textlist 下拉框显示的内容  
890 - * @param firstRow 开始行  
891 - * @param endRow 结束行  
892 - * @param firstCol 开始列  
893 - * @param endCol 结束列  
894 - * @return 设置好的sheet.  
895 - */  
896 - public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol)  
897 - {  
898 - DataValidationHelper helper = sheet.getDataValidationHelper();  
899 - // 加载下拉列表内容  
900 - DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);  
901 - // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列  
902 - CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);  
903 - // 数据有效性对象  
904 - DataValidation dataValidation = helper.createValidation(constraint, regions); 873 + if (StringUtils.isNotEmpty(promptContent))
  874 + {
  875 + // 如果设置了提示信息则鼠标放上去提示
  876 + dataValidation.createPromptBox("", promptContent);
  877 + dataValidation.setShowPromptBox(true);
  878 + }
905 // 处理Excel兼容性问题 879 // 处理Excel兼容性问题
906 if (dataValidation instanceof XSSFDataValidation) 880 if (dataValidation instanceof XSSFDataValidation)
907 { 881 {
@@ -912,7 +886,6 @@ public class ExcelUtil<T> @@ -912,7 +886,6 @@ public class ExcelUtil<T>
912 { 886 {
913 dataValidation.setSuppressDropDownArrow(false); 887 dataValidation.setSuppressDropDownArrow(false);
914 } 888 }
915 -  
916 sheet.addValidationData(dataValidation); 889 sheet.addValidationData(dataValidation);
917 } 890 }
918 891