You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Mead Lai <la...@gmail.com> on 2012/07/16 06:32:46 UTC

How to set default value in DropDownList cell?

Hello all,

I am using poi3.7 to create a dropdown-list column, to let user select the
value.
but how to set a default value in every cell in advance?
Use setCellValue("defaultValue"). it doesn't work at all.
Could anyone give me a hand? or someone face the thing problem?
--code--
Row ddlrow = sheet.getRow(1);
*if*(ddlrow == *null*)}
sheet.createRow(1).createCell(index);
sheet.getRow(1).getCell(index).setCellValue("MY_DEFAULT_VALUE");
}

--code--
//create data referenc
 HSSFName namedRange = workbook.createName();
          namedRange.setNameName(dicName);
          namedRange.setRefersToFormula(region);
          //create drop-down region
          CellRangeAddressList addressList = *new* CellRangeAddressList(1,
EXPORT_BLANK_LINES, index,            index);
         DVConstraint dvConstraint = DVConstraint.*
createFormulaListConstraint*(dicName);
         DataValidation dataValidation =
*new*HSSFDataValidation(addressList, dvConstraint);
         dataValidation.setSuppressDropDownArrow(*false*);
         sheet.addValidationData(dataValidation);

Regards,
Mead

Re: How to set default value in DropDownList cell?

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
Not sure I understand what the problem really is here. I am assuming that you
are populating the data validation using POI and that you know in advance
what value you want to be displayed to the user when the sheet is opened. If
so, then surely, all you need to do is set the cell to that value before you
save the sheet away. Think of it not as a default value for the validation
but a default value for the cell. All the validation is really doing is
prescribing the range of values the user can enter and as long as you try to
set the cell to one of the values from it's list, all should be well; you
will need to set the value after adding the validation to the sheet of
course.

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-set-default-value-in-DropDownList-cell-tp5710452p5710453.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org