2012-05-10 33 views
11

Necesito crear una lista desplegable en el archivo de Excel usando Apache POI. y puedo hacerlo, pero No puedo hacer el primer elemento en la lista desplegable como Artículo predeterminado.Lista desplegable de Excel usando Apache POI

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

} 

Respuesta

7

para establecer un valor predeterminado, simplemente configureCellValue ("first_item_value");

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

Lo he hecho como si tuviera el mismo problema.

+1

Pero esto no viene como una de las opciones en la lista desplegable. ¿Qué pasa si lo necesitamos en la lista? Diga "SELECCIONAR" debería aparecer por defecto en la lista. – zeewagon

Cuestiones relacionadas