|
|
@@ -667,17 +667,10 @@ public class ExcelUtil<T>
|
|
667
|
667
|
// 设置列宽
|
|
668
|
668
|
sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
|
|
669
|
669
|
}
|
|
670
|
|
- // 如果设置了提示信息则鼠标放上去提示.
|
|
671
|
|
- if (StringUtils.isNotEmpty(attr.prompt()))
|
|
|
670
|
+ if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0)
|
|
672
|
671
|
{
|
|
673
|
|
- // 这里默认设了2-101列提示.
|
|
674
|
|
- setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
|
|
675
|
|
- }
|
|
676
|
|
- // 如果设置了combo属性则本列只能选择不能输入
|
|
677
|
|
- if (attr.combo().length > 0)
|
|
678
|
|
- {
|
|
679
|
|
- // 这里默认设了2-101列只能选择不能输入.
|
|
680
|
|
- setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
|
|
|
672
|
+ // 提示信息或只能选择不能输入的列内容.
|
|
|
673
|
+ setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
|
|
681
|
674
|
}
|
|
682
|
675
|
}
|
|
683
|
676
|
|
|
|
@@ -736,48 +729,29 @@ public class ExcelUtil<T>
|
|
736
|
729
|
}
|
|
737
|
730
|
|
|
738
|
731
|
/**
|
|
739
|
|
- * 设置 POI XSSFSheet 单元格提示
|
|
740
|
|
- *
|
|
|
732
|
+ * 设置 POI XSSFSheet 单元格提示或选择框
|
|
|
733
|
+ *
|
|
741
|
734
|
* @param sheet 表单
|
|
742
|
|
- * @param promptTitle 提示标题
|
|
|
735
|
+ * @param textlist 下拉框显示的内容
|
|
743
|
736
|
* @param promptContent 提示内容
|
|
744
|
737
|
* @param firstRow 开始行
|
|
745
|
738
|
* @param endRow 结束行
|
|
746
|
739
|
* @param firstCol 开始列
|
|
747
|
740
|
* @param endCol 结束列
|
|
748
|
741
|
*/
|
|
749
|
|
- public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
|
|
|
742
|
+ public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
|
|
750
|
743
|
int firstCol, int endCol)
|
|
751
|
744
|
{
|
|
752
|
745
|
DataValidationHelper helper = sheet.getDataValidationHelper();
|
|
753
|
|
- DataValidationConstraint constraint = helper.createCustomConstraint("DD1");
|
|
|
746
|
+ DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
|
|
754
|
747
|
CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
|
|
755
|
748
|
DataValidation dataValidation = helper.createValidation(constraint, regions);
|
|
756
|
|
- dataValidation.createPromptBox(promptTitle, promptContent);
|
|
757
|
|
- dataValidation.setShowPromptBox(true);
|
|
758
|
|
- sheet.addValidationData(dataValidation);
|
|
759
|
|
- }
|
|
760
|
|
-
|
|
761
|
|
- /**
|
|
762
|
|
- * 设置某些列的值只能输入预制的数据,显示下拉框.
|
|
763
|
|
- *
|
|
764
|
|
- * @param sheet 要设置的sheet.
|
|
765
|
|
- * @param textlist 下拉框显示的内容
|
|
766
|
|
- * @param firstRow 开始行
|
|
767
|
|
- * @param endRow 结束行
|
|
768
|
|
- * @param firstCol 开始列
|
|
769
|
|
- * @param endCol 结束列
|
|
770
|
|
- * @return 设置好的sheet.
|
|
771
|
|
- */
|
|
772
|
|
- public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol)
|
|
773
|
|
- {
|
|
774
|
|
- DataValidationHelper helper = sheet.getDataValidationHelper();
|
|
775
|
|
- // 加载下拉列表内容
|
|
776
|
|
- DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);
|
|
777
|
|
- // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
|
|
778
|
|
- CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
|
|
779
|
|
- // 数据有效性对象
|
|
780
|
|
- DataValidation dataValidation = helper.createValidation(constraint, regions);
|
|
|
749
|
+ if (StringUtils.isNotEmpty(promptContent))
|
|
|
750
|
+ {
|
|
|
751
|
+ // 如果设置了提示信息则鼠标放上去提示
|
|
|
752
|
+ dataValidation.createPromptBox("", promptContent);
|
|
|
753
|
+ dataValidation.setShowPromptBox(true);
|
|
|
754
|
+ }
|
|
781
|
755
|
// 处理Excel兼容性问题
|
|
782
|
756
|
if (dataValidation instanceof XSSFDataValidation)
|
|
783
|
757
|
{
|
|
|
@@ -788,7 +762,6 @@ public class ExcelUtil<T>
|
|
788
|
762
|
{
|
|
789
|
763
|
dataValidation.setSuppressDropDownArrow(false);
|
|
790
|
764
|
}
|
|
791
|
|
-
|
|
792
|
765
|
sheet.addValidationData(dataValidation);
|
|
793
|
766
|
}
|
|
794
|
767
|
|