Apache POI从下拉菜单中选择值

时间:2018-06-25 06:47:42

标签: excel drop-down-menu apache-poi xssf

我的应用程序中有一个模板Excel文件,该文件用于根据所选值报告信息。现在,模板在不同的单元格中具有作为下拉菜单的值,如您所见here

我想使用POI框架从下拉菜单中按索引选择值。谁能给我提供一些示例,如何使用 XSSFWorkbook 来实现?

1 个答案:

答案 0 :(得分:0)

您需要使用DataValidation

尝试此代码

public static void main(String[] args) throws IOException {

    DataValidation dataValidation = null;
    DataValidationConstraint constraint = null;
    DataValidationHelper validationHelper = null;

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet) wb.createSheet("sheet");

    validationHelper = new XSSFDataValidationHelper(sheet);
    CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
    constraint = validationHelper.createExplicitListConstraint(new String[]{"YES", "NO", "MAYBE"});
    dataValidation = validationHelper.createValidation(constraint, addressList);
    dataValidation.setSuppressDropDownArrow(true);
    sheet1.addValidationData(dataValidation);

    FileOutputStream fileOut = new FileOutputStream(/* your path file */);
    wb.write(fileOut);
    fileOut.close();
}