Apache POI - DROP DOWN值从数据中删除'00000'

时间:2013-06-26 20:17:48

标签: apache apache-poi

我正在通过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从下拉。

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

1 个答案:

答案 0 :(得分: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);