I need to create a drop down list (Data Validation) on a particular cell in an Excel sheet and read them back.
With the help of tutorials provided by Apache POI
, I am able to create a Drop-down list in an Excel sheet, but I also need to read the drop-down list content when reading that again, so that I can render a similar drop-down list on a UI.
Any suggestions?
I can't seem to find any mechanism in HSSF to retrieve the
DataValidation
s from theHSSFSheet
. So if you have a .xls file, you're out of luck.However, if you have a .xlsx file, then
XSSFSheet
supplies a method to retrieve a list of allXSSFDataValidation
s on the sheet:getDataValidations
, which returns aList<XSSFDataValidation>
.You'll need to loop over them all, calling
regions()
to examine theCellRangeAddressList
to see if it applies to your cell. If it does, then callgetValidationConstraint
to access theDataValidationConstraint
object, on which you can callgetExplicitListValues
to get the array of strings back.DataValidation is stored even in HSSF workbook, but it is in
Internal Sheet
of the library and since it isprivate
so access to it is not given to application programmer. I have used Java Reflection API to access internal sheet. This code is working fine for me.