Excel Drop down list using Apache POI

2020-02-04 11:20发布

I need to create a drop down list in excel file using Apache POI. and I am able to do that so But I am not able to make first item in drop down list as default Item.

public class sd {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

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

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


    validationHelper=new XSSFDataValidationHelper(sheet1);
    CellRangeAddressList addressList = new  CellRangeAddressList(0,5,0,0);
    constraint =validationHelper.createExplicitListConstraint(new String[]{"SELECT","10", "20", "30"});
    dataValidation = validationHelper.createValidation(constraint, addressList);
    dataValidation.setSuppressDropDownArrow(true);      
    sheet1.addValidationData(dataValidation);

    FileOutputStream fileOut = new FileOutputStream("c:\\temp\\vineet.xlsx");
    wb.write(fileOut);
    fileOut.close();
}

}

2条回答
Lonely孤独者°
2楼-- · 2020-02-04 11:35

to set a default value, just setCellValue("first_item_value");

sheet.getRow(1).getCell(index).setCellValue("my_default_value");

I have did it as facing the same problem.

查看更多
何必那么认真
3楼-- · 2020-02-04 11:44

Here is my code:

Cell cell = row.createCell(2);
cell.setCellValue("SELECT");

//2 is the 2nd cell in my case
查看更多
登录 后发表回答