string path = "D:\\success.xlsx"; string sheetname = "Sheetlist"; xssfworkbook wb = null; XSSFSheet sheetlist = null; file inputfile = new file (PATH); if (Inputfile.exists ()) { wb = new xssfworkbook (New fileinputstream (path)); } else { wb = new xssfworkbook ();// excel file Object } if (Wb.getsheet (sheetname) == null) { &nbsP; sheetlist = wb.createsheet (SheetName) ;// Sheet Objects } else { sheetlist = wb.getsheet (sheetName);// Sheet Object } Datavalidationhelper helper = sheetlist.getdatavalidationhelper (); List<XSSFDataValidation> dataValidations = Sheetlist.getdatavalidations (); for (XSSFDataValidation dv : datavalidations) { // Existing Validation } // cellrangEaddresslist dstaddrlist = new cellrangeaddresslist (0, 500, 0, 0);// Rule one cell range String[] textlist = { "List 1", "List 2", "List 3", "List 4", "List 5" }; Datavalidation dstdatavalidation = helper.createvalidation (Helper.createexplicitlistconstraint ( textlist), dstaddrlist); dstdatavalidation.createpromptbox ("Cue header", " Prompt content "); dstdatavalidation.setshowerrorbox (True); dstdatavalidation.setshowpromptbox (True); dstdatavalidation.setemptycellallowed (False); sheetlist.addvalidationdata (DstdataValidation); cellrangeaddresslist dstaddrlist2 = new cellrangeaddresslist (0, 500, 1, 1);// rule two cell range datavalidationconstraint dvc = helper.createnumericconstraint ( dvconstraint.validationtype.integer, DVConstraint.OperatorType.BETWEEN, "0", "9999999999"); datavalidation dstdatavalidation2 = helper.createvalidation (DVC, &NBSP;DSTADDRLIST2); dstdatavalidation2.createerrorbox ("Fill the wrong and lose!") ", " can only fill that what what what "); dstdatavalidation2.setemptycellallowed ( FALSE); dstdatavalidation2.setshowerrorbox (True); sheetliSt.addvalidationdata (dstDataValidation2); fileoutputstream out = new fileoutputstream (path); wb.write (out); out.close ();
Java adds data validation to excel with POI