0

我正在通过java代码构建动态下拉列表,它工作得很好。我正在运行的代码是:

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Data Validation");
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint)
dvHelper.createExplicitListConstraint(new String[]{"0000011", "0000021", "0000031"});
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
XSSFDataValidation validation = (XSSFDataValidation)dvHelper.createValidation(
dvConstraint, addressList);
validation.setShowErrorBox(true);
validation.setSuppressDropDownArrow(true);
sheet.addValidationData(validation);

下拉菜单正常出现,但是当我从下拉菜单中选择任何一个值时,00000会自动删除并且只显示 11,但我希望0000011从下拉菜单中选择值。

drop 值显示为,0000011但在选择后显示11。可能是如果我们可以将单元格类型更改为文本会有所帮助或其他方式,但如何做到这一点?

4

1 回答 1

1

经过大量研究,我自己解决了这个问题。所以我想发布答案来帮助别人。

// setting cell type as string to avoid removing 00000 from drop down
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat((short)BuiltinFormats.getBuiltinFormat("text")); 
sheet.setDefaultColumnStyle(0, textStyle);
于 2013-06-27T09:12:40.087 回答