Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Drop down list using Apache POI

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

}
like image 480
Vineet Avatar asked May 10 '12 12:05

Vineet


People also ask

How do I create a drop down list in Excel using Apache POI?

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 .

Does Apache POI support XLS?

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.


2 Answers

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.

like image 144
meadlai Avatar answered Oct 17 '22 16:10

meadlai


Here is my code:

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

//2 is the 2nd cell in my case
like image 36
user9962613 Avatar answered Oct 17 '22 15:10

user9962613