使用Apache POI Excel的下拉列表(Excel Drop down list using

2019-06-24 08:01发布

我需要创建使用Apache POI的下拉列表中的Excel文件。 而我能做到这一点这样,但我不能够使下拉列表中的默认项第一项。

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

}

Answer 1:

设置默认值,只是setCellValue(“first_item_value”);

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

我已经做到了为面临同样的问题。



Answer 2:

这里是我的代码:

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

//2 is the 2nd cell in my case


文章来源: Excel Drop down list using Apache POI