使用Apache POI下拉列表。

时间:2023-01-23 20:23:02

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.

我需要使用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();
}

}

2 个解决方案

#1


7  

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

要设置一个默认值,只需setCellValue(“first_item_value”);

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

I have did it as facing the same problem.

我做那件事是为了面对同样的问题。

#2


0  

Here is my code:

这是我的代码:

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

//2 is the 2nd cell in my case

#1


7  

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

要设置一个默认值,只需setCellValue(“first_item_value”);

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

I have did it as facing the same problem.

我做那件事是为了面对同样的问题。

#2


0  

Here is my code:

这是我的代码:

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

//2 is the 2nd cell in my case