Lycone
Lycone

Reputation: 650

Apache POI select values from drop down menu

I got a template Excel-file in my application, which i use to report information depending on the selected values. Now the template has got values as a drop down menu in different cells as you can see here.

I want to select the values by index from the drop down menu with the POI framework. Can anyone provide me with some examples, how do I implement that using XSSFWorkbook ?

Upvotes: 4

Views: 5195

Answers (1)

carmelolg
carmelolg

Reputation: 503

You need use DataValidation.

Try this code

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();
}

Upvotes: 3

Related Questions