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();
}
}
The dependency of dropdown lists must be managed in Excel s GUI where the generated file is running in. Apache poi only can create the Excel file so that this is possible then. One approach is using named ranges for the data validation lists who's names are then got using INDIRECT .
Note − Older versions of POI support binary file formats such as doc, xls, ppt, etc. Version 3.5 onwards, POI supports OOXML file formats of MS-Office such as docx, xlsx, pptx, etc.
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.
Here is my code:
Cell cell = row.createCell(2);
cell.setCellValue("SELECT");
//2 is the 2nd cell in my case
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With