17

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

}
4

2 回答 2

7

要设置默认值,只需 setCellValue("first_item_value");

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

我这样做是因为面临同样的问题。

于 2012-07-16T05:47:03.323 回答
1

这是我的代码:

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

//2 is the 2nd cell in my case
于 2018-06-19T14:23:42.323 回答