我需要使用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();
}
}
最佳答案
设置默认值,只需setCellValue(“ first_item_value”);
sheet.getRow(1).getCell(index).setCellValue("my_default_value");
我这样做是面对同样的问题。
关于excel - 使用Apache POI的Excel下拉列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10534095/