From 40b8f50a73a3eaf06613b20288b63f50d4e8ddd4 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 3 Dec 2007 14:14:02 +0000 Subject: [PATCH] Usermodel support for the datavalidation code, and tests for it git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@600544 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 3 +- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/usermodel/HSSFSheet.java | 93 ++ .../poi/hssf/data/TestDataValidation.xls | Bin 0 -> 21504 bytes .../hssf/usermodel/TestDataValidation.java | 910 ++++++++++++++++++ 5 files changed, 1006 insertions(+), 1 deletion(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/TestDataValidation.xls create mode 100644 src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index f429eb361..829fb4d18 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,7 +36,8 @@ - 43877 and 39512 - Fix for handling mixed OBJ and CONTINUE records. + 27511 - [PATCH] Support for data validation, via DVRecord and DVALRecord + 43877 and 39512 - Fix for handling mixed OBJ and CONTINUE records. 43807 - Throw an IllegalArgumentException if asked to create a merged region with invalid columns or rows, rather than writing out a corrupt file 43837 - [PATCH] Support for unicode NameRecords 43721 - [PATCH] Support for Chart Title Format records diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index e4ed1065f..18a305017 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + 27511 - [PATCH] Support for data validation, via DVRecord and DVALRecord 43877 - Fix for handling mixed OBJ and CONTINUE records 39512 - Fix for handling mixed OBJ and CONTINUE records 43837 - [PATCH] Support for unicode NameRecords diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 91098c2dc..b52fa1e5b 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -23,9 +23,13 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.ddf.EscherRecord; +import org.apache.poi.hssf.model.FormulaParser; import org.apache.poi.hssf.model.Sheet; import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.*; +import org.apache.poi.hssf.record.formula.Ptg; +import org.apache.poi.hssf.util.HSSFCellRangeAddress; +import org.apache.poi.hssf.util.HSSFDataValidation; import org.apache.poi.hssf.util.Region; import org.apache.poi.hssf.util.PaneInformation; import org.apache.poi.util.POILogFactory; @@ -35,6 +39,7 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Stack; import java.util.TreeMap; import java.text.AttributedString; import java.text.NumberFormat; @@ -353,6 +358,94 @@ public class HSSFSheet { return lastrow; } + + /** + * Creates a data validation object + * @param obj_validation The Data validation object settings + */ + public void addValidationData(HSSFDataValidation obj_validation) + { + if ( obj_validation == null ) + { + return; + } + DVALRecord dvalRec = (DVALRecord)sheet.findFirstRecordBySid( DVALRecord.sid ); + int eofLoc = sheet.findFirstRecordLocBySid( EOFRecord.sid ); + if ( dvalRec == null ) + { + dvalRec = new DVALRecord(); + sheet.getRecords().add( eofLoc, dvalRec ); + } + int curr_dvRecNo = dvalRec.getDVRecNo(); + dvalRec.setDVRecNo(curr_dvRecNo+1); + + //create dv record + DVRecord dvRecord = new DVRecord(); + + //dv record's option flags + dvRecord.setDataType( obj_validation.getDataValidationType() ); + dvRecord.setErrorStyle(obj_validation.getErrorStyle()); + dvRecord.setEmptyCellAllowed(obj_validation.getEmptyCellAllowed()); + dvRecord.setSurppresDropdownArrow(obj_validation.getSurppressDropDownArrow()); + dvRecord.setShowPromptOnCellSelected(obj_validation.getShowPromptBox()); + dvRecord.setShowErrorOnInvalidValue(obj_validation.getShowErrorBox()); + dvRecord.setConditionOperator(obj_validation.getOperator()); + + //string fields + dvRecord.setStringField( DVRecord.STRING_PROMPT_TITLE,obj_validation.getPromptBoxTitle()); + dvRecord.setStringField( DVRecord.STRING_PROMPT_TEXT, obj_validation.getPromptBoxText()); + dvRecord.setStringField( DVRecord.STRING_ERROR_TITLE, obj_validation.getErrorBoxTitle()); + dvRecord.setStringField( DVRecord.STRING_ERROR_TEXT, obj_validation.getErrorBoxText()); + + //formula fields ( size and data ) + String str_formula = obj_validation.getFirstFormula(); + FormulaParser fp = new FormulaParser(str_formula+";",book); + fp.parse(); + Stack ptg_arr = new Stack(); + Ptg[] ptg = fp.getRPNPtg(); + int size = 0; + for (int k = 0; k < ptg.length; k++) + { + if ( ptg[k] instanceof org.apache.poi.hssf.record.formula.AreaPtg ) + { + //we should set ptgClass to Ptg.CLASS_REF and explicit formula string to false + ptg[k].setClass(Ptg.CLASS_REF); + obj_validation.setExplicitListFormula(false); + } + size += ptg[k].getSize(); + ptg_arr.push(ptg[k]); + } + dvRecord.setFirstFormulaRPN(ptg_arr); + dvRecord.setFirstFormulaSize((short)size); + + dvRecord.setListExplicitFormula(obj_validation.getExplicitListFormula()); + + if ( obj_validation.getSecondFormula() != null ) + { + str_formula = obj_validation.getSecondFormula(); + fp = new FormulaParser(str_formula+";",book); + fp.parse(); + ptg_arr = new Stack(); + ptg = fp.getRPNPtg(); + size = 0; + for (int k = 0; k < ptg.length; k++) + { + size += ptg[k].getSize(); + ptg_arr.push(ptg[k]); + } + dvRecord.setSecFormulaRPN(ptg_arr); + dvRecord.setSecFormulaSize((short)size); + } + + //dv records cell range field + HSSFCellRangeAddress cell_range = new HSSFCellRangeAddress(); + cell_range.addADDRStructure(obj_validation.getFirstRow(), obj_validation.getFirstColumn(), obj_validation.getLastRow(), obj_validation.getLastColumn()); + dvRecord.setCellRangeAddress(cell_range); + + //add dv record + eofLoc = sheet.findFirstRecordLocBySid( EOFRecord.sid ); + sheet.getRecords().add( eofLoc, dvRecord ); + } /** * Get the visibility state for a given column. diff --git a/src/testcases/org/apache/poi/hssf/data/TestDataValidation.xls b/src/testcases/org/apache/poi/hssf/data/TestDataValidation.xls new file mode 100644 index 0000000000000000000000000000000000000000..556ea8192758d22c8b5427a379673771298ee08d GIT binary patch literal 21504 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: TestDataValidation

+ *

Description: Class for testing Excel's data validation mechanism + * Second test : + * - + *

+ * @author Dragos Buleandra ( dragos.buleandra@trade2b.ro ) + */ +public class TestDataValidation extends TestCase +{ + public TestDataValidation(String name) + { + super(name); + } + + protected void setUp() + { + String filename = System.getProperty("HSSF.testdata.path"); + if (filename == null) + { + System.setProperty("HSSF.testdata.path", "src/testcases/org/apache/poi/hssf/data"); + } + } + + public void testDataValidation() throws Exception + { + System.out.println("\nTest no. 2 - Test Excel's Data validation mechanism"); + String resultFile = System.getProperty("HSSF.testdata.path")+"/TestDataValidation.xls"; + HSSFWorkbook wb = new HSSFWorkbook(); + + HSSFCellStyle style_1 = this.createStyle( wb, HSSFCellStyle.ALIGN_LEFT ); + HSSFCellStyle style_2 = this.createStyle( wb, HSSFCellStyle.ALIGN_CENTER ); + HSSFCellStyle style_3 = this.createStyle( wb, HSSFCellStyle.ALIGN_CENTER, HSSFColor.GREY_25_PERCENT.index, true ); + HSSFCellStyle style_4 = this.createHeaderStyle(wb); + HSSFDataValidation data_validation = null; + + //data validation's number types + System.out.print(" Create sheet for Data Validation's number types ... "); + HSSFSheet fSheet = wb.createSheet("Number types"); + + //"Whole number" validation type + this.createDVTypeRow( wb, 0, style_3, "Whole number"); + this.createHeaderRow( wb, 0, style_4 ); + + short start_row = (short)fSheet.getPhysicalNumberOfRows(); + data_validation = new HSSFDataValidation((short)(start_row),(short)0,(short)(start_row),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_INTEGER); + data_validation.setOperator(HSSFDataValidation.OPERATOR_BETWEEN); + data_validation.setFirstFormula("2"); + data_validation.setSecondFormula("6"); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Between 2 and 6 ", true, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)(start_row+1)); + data_validation.setLastRow((short)(start_row+1)); + data_validation.setEmptyCellAllowed(false); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_BETWEEN); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_INFO); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not between 2 and 6 ", false, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = INFO" ); + + data_validation.setFirstRow((short)(start_row+2)); + data_validation.setLastRow((short)(start_row+2)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_EQUAL); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_WARNING); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Equal to 3", false, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = WARNING" ); + + data_validation.setFirstRow((short)(start_row+3)); + data_validation.setLastRow((short)(start_row+3)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not equal to 3", false, false, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+4)); + data_validation.setLastRow((short)(start_row+4)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than 3", true, false, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+5)); + data_validation.setLastRow((short)(start_row+5)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than 3", true, true, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+6)); + data_validation.setLastRow((short)(start_row+6)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_STOP); + data_validation.setShowErrorBox(true); + data_validation.setFirstFormula("4"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than or equal to 4", true, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)(start_row+7)); + data_validation.setLastRow((short)(start_row+7)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("4"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than or equal to 4", false, true, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + //"Decimal" validation type + this.createDVTypeRow( wb, 0, style_3, "Decimal"); + this.createHeaderRow( wb, 0, style_4 ); + + start_row += (short)(8+4); + data_validation = new HSSFDataValidation((short)(start_row),(short)0,(short)(start_row),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_DECIMAL); + data_validation.setOperator(HSSFDataValidation.OPERATOR_BETWEEN); + data_validation.setFirstFormula("2"); + data_validation.setSecondFormula("6"); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Between 2 and 6 ", true, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)(start_row+1)); + data_validation.setLastRow((short)(start_row+1)); + data_validation.setEmptyCellAllowed(false); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_BETWEEN); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_INFO); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not between 2 and 6 ", false, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = INFO" ); + + data_validation.setFirstRow((short)(start_row+2)); + data_validation.setLastRow((short)(start_row+2)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_EQUAL); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_WARNING); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Equal to 3", false, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = WARNING" ); + + data_validation.setFirstRow((short)(start_row+3)); + data_validation.setLastRow((short)(start_row+3)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not equal to 3", false, false, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+4)); + data_validation.setLastRow((short)(start_row+4)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than 3", true, false, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+5)); + data_validation.setLastRow((short)(start_row+5)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than 3", true, true, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+6)); + data_validation.setLastRow((short)(start_row+6)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_STOP); + data_validation.setShowErrorBox(true); + data_validation.setFirstFormula("4"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than or equal to 4", true, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)(start_row+7)); + data_validation.setLastRow((short)(start_row+7)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("4"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than or equal to 4", false, true, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + System.out.println("done !"); + + //"List" Data Validation type + /** @todo List*/ + System.out.print(" Create sheet for 'List' Data Validation type ... "); + fSheet = wb.createSheet("Lists"); + + this.createDVTypeRow( wb, 1, style_3, "Explicit lists - list items are explicitly provided"); + this.createDVDeescriptionRow( wb, 1, style_3, "Disadvantage - sum of item's length should be less than 255 characters"); + this.createHeaderRow( wb, 1, style_4 ); + + start_row = (short)fSheet.getPhysicalNumberOfRows(); + data_validation = new HSSFDataValidation((short)(start_row),(short)0,(short)(start_row),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_LIST); + data_validation.setFirstFormula("1+2+3"); + data_validation.setSecondFormula(null); + data_validation.setSurppressDropDownArrow(false); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "POIFS,HSSF,HWPF,HPSF", true, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type=STOP ; In-cell dropdown=yes" ); + + data_validation = new HSSFDataValidation((short)(start_row+1),(short)0,(short)(start_row+1),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_LIST); + data_validation.setFirstFormula("4+5+6+7"); + data_validation.setSecondFormula(null); + data_validation.setSurppressDropDownArrow(false); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "POIFS,HSSF,HWPF,HPSF", false, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type=STOP ; In-cell dropdown=yes" ); + + data_validation = new HSSFDataValidation((short)(start_row+2),(short)0,(short)(start_row+2),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_LIST); + data_validation.setFirstFormula("7+21"); + data_validation.setSecondFormula(null); + data_validation.setSurppressDropDownArrow(true); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "POIFS,HSSF,HWPF,HPSF", true, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type=STOP ; In-cell dropdown=no" ); + + data_validation = new HSSFDataValidation((short)(start_row+3),(short)0,(short)(start_row+3),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_LIST); + data_validation.setFirstFormula("8/2"); + data_validation.setSecondFormula(null); + data_validation.setSurppressDropDownArrow(true); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "POIFS,HSSF,HWPF,HPSF", false, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type=STOP ; In-cell dropdown=no" ); + + this.createDVTypeRow( wb, 1, style_3, "Reference lists - list items are taken from others cells"); + this.createDVDeescriptionRow( wb, 1, style_3, "Advantage - no restriction regarding the sum of item's length"); + this.createHeaderRow( wb, 1, style_4 ); + + start_row += (short)(4+5); + String cellStrValue = "a b c d e f g h i j k l m n o p r s t u v x y z w 0 1 2 3 4 "+ + "a b c d e f g h i j k l m n o p r s t u v x y z w 0 1 2 3 4 "+ + "a b c d e f g h i j k l m n o p r s t u v x y z w 0 1 2 3 4 "+ + "a b c d e f g h i j k l m n o p r s t u v x y z w 0 1 2 3 4 "; + + String strFormula = "$A$100:$A$120"; + data_validation = new HSSFDataValidation((short)(start_row),(short)0,(short)(start_row),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_LIST); + data_validation.setFirstFormula(strFormula); + data_validation.setSecondFormula(null); + data_validation.setSurppressDropDownArrow(false); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, strFormula, true, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type=STOP ; In-cell dropdown=yes" ); + + data_validation = new HSSFDataValidation((short)(start_row+1),(short)0,(short)(start_row+1),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_LIST); + data_validation.setFirstFormula(strFormula); + data_validation.setSecondFormula(null); + data_validation.setSurppressDropDownArrow(false); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, strFormula, false, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type=STOP ; In-cell dropdown=yes" ); + + data_validation = new HSSFDataValidation((short)(start_row+2),(short)0,(short)(start_row+2),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_LIST); + data_validation.setFirstFormula(strFormula); + data_validation.setSecondFormula(null); + data_validation.setSurppressDropDownArrow(true); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, strFormula, true, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type=STOP ; In-cell dropdown=no" ); + + data_validation = new HSSFDataValidation((short)(start_row+3),(short)0,(short)(start_row+3),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_LIST); + data_validation.setFirstFormula(strFormula); + data_validation.setSecondFormula(null); + data_validation.setSurppressDropDownArrow(true); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, strFormula, false, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type=STOP ; In-cell dropdown=no" ); + + for (int i=100; i<=120; i++) + { + HSSFRow currRow = fSheet.createRow(i); + currRow.createCell((short)0).setCellValue(cellStrValue); +// currRow.hide( true ); + } + + System.out.println("done !"); + + //Date/Time Validation type + System.out.print(" Create sheet for 'Date' and 'Time' Data Validation types ... "); + fSheet = wb.createSheet("Date_Time"); + SimpleDateFormat df = new SimpleDateFormat("m/d/yyyy"); + HSSFDataFormat dataFormat = wb.createDataFormat(); + short fmtDate = dataFormat.getFormat("m/d/yyyy"); + short fmtTime = dataFormat.getFormat("h:mm"); + HSSFCellStyle cellStyle_data = wb.createCellStyle(); + cellStyle_data.setDataFormat(fmtDate); + HSSFCellStyle cellStyle_time = wb.createCellStyle(); + cellStyle_time.setDataFormat(fmtTime); + + this.createDVTypeRow( wb, 2, style_3, "Date ( cells are already formated as date - m/d/yyyy)"); + this.createHeaderRow( wb, 2, style_4 ); + + start_row = (short)fSheet.getPhysicalNumberOfRows(); + data_validation = new HSSFDataValidation((short)(start_row),(short)0,(short)(start_row),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_DATE); + data_validation.setOperator(HSSFDataValidation.OPERATOR_BETWEEN); + + data_validation.setFirstFormula( String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("1/2/2004"))) ); + data_validation.setSecondFormula( String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("1/6/2004"))) ); + + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Between 1/2/2004 and 1/6/2004 ", true, true, true ); + this.setCellFormat( fSheet, cellStyle_data ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)(start_row+1)); + data_validation.setLastRow((short)(start_row+1)); + data_validation.setEmptyCellAllowed(false); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_BETWEEN); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_INFO); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not between 1/2/2004 and 1/6/2004 ", false, true, true ); + this.setCellFormat( fSheet, cellStyle_data ); + this.writeOtherSettings( fSheet, style_1, "Error box type = INFO" ); + + data_validation.setFirstRow((short)(start_row+2)); + data_validation.setLastRow((short)(start_row+2)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setFirstFormula(String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("3/2/2004")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_EQUAL); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_WARNING); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Equal to 3/2/2004", false, false, true ); + this.setCellFormat( fSheet, cellStyle_data ); + this.writeOtherSettings( fSheet, style_1, "Error box type = WARNING" ); + + data_validation.setFirstRow((short)(start_row+3)); + data_validation.setLastRow((short)(start_row+3)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula(String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("3/2/2004")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not equal to 3/2/2004", false, false, false ); + this.setCellFormat( fSheet, cellStyle_data ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+4)); + data_validation.setLastRow((short)(start_row+4)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula(String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("3/2/2004")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than 3/2/2004", true, false, false ); + this.setCellFormat( fSheet, cellStyle_data ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+5)); + data_validation.setLastRow((short)(start_row+5)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula(String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("3/2/2004")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than 3/2/2004", true, true, false ); + this.setCellFormat( fSheet, cellStyle_data ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+6)); + data_validation.setLastRow((short)(start_row+6)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_STOP); + data_validation.setShowErrorBox(true); + data_validation.setFirstFormula(String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("3/2/2004")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than or equal to 3/2/2004", true, false, true ); + this.setCellFormat( fSheet, cellStyle_data ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)(start_row+7)); + data_validation.setLastRow((short)(start_row+7)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula(String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("3/4/2004")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than or equal to 3/4/2004", false, true, false ); + this.setCellFormat( fSheet, cellStyle_data ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + //"Time" validation type + this.createDVTypeRow( wb, 2, style_3, "Time ( cells are already formated as time - h:mm)"); + this.createHeaderRow( wb, 2, style_4 ); + + df = new SimpleDateFormat("hh:mm"); + + start_row += (short)(8+4); + data_validation = new HSSFDataValidation((short)(start_row),(short)0,(short)(start_row),(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_TIME); + data_validation.setOperator(HSSFDataValidation.OPERATOR_BETWEEN); + data_validation.setFirstFormula(String.valueOf(HSSFDateUtil.getExcelDate(df.parse("12:00")))); + data_validation.setSecondFormula(String.valueOf(HSSFDateUtil.getExcelDate(df.parse("16:00")))); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Between 12:00 and 16:00 ", true, true, true ); + this.setCellFormat( fSheet, cellStyle_time ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)(start_row+1)); + data_validation.setLastRow((short)(start_row+1)); + data_validation.setEmptyCellAllowed(false); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_BETWEEN); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_INFO); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not between 12:00 and 16:00 ", false, true, true ); + this.setCellFormat( fSheet, cellStyle_time ); + this.writeOtherSettings( fSheet, style_1, "Error box type = INFO" ); + + data_validation.setFirstRow((short)(start_row+2)); + data_validation.setLastRow((short)(start_row+2)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setFirstFormula(String.valueOf((int)HSSFDateUtil.getExcelDate(df.parse("13:35")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_EQUAL); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_WARNING); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Equal to 13:35", false, false, true ); + this.setCellFormat( fSheet, cellStyle_time ); + this.writeOtherSettings( fSheet, style_1, "Error box type = WARNING" ); + + data_validation.setFirstRow((short)(start_row+3)); + data_validation.setLastRow((short)(start_row+3)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula(String.valueOf(HSSFDateUtil.getExcelDate(df.parse("13:35")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not equal to 13:35", false, false, false ); + this.setCellFormat( fSheet, cellStyle_time ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+4)); + data_validation.setLastRow((short)(start_row+4)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula(String.valueOf(HSSFDateUtil.getExcelDate(df.parse("12:00")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than 12:00", true, false, false ); + this.setCellFormat( fSheet, cellStyle_time ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+5)); + data_validation.setLastRow((short)(start_row+5)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula(String.valueOf(HSSFDateUtil.getExcelDate(df.parse("12:00")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than 12:00", true, true, false ); + this.setCellFormat( fSheet, cellStyle_time ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)(start_row+6)); + data_validation.setLastRow((short)(start_row+6)); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_STOP); + data_validation.setShowErrorBox(true); + data_validation.setFirstFormula(String.valueOf(HSSFDateUtil.getExcelDate(df.parse("14:00")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than or equal to 14:00", true, false, true ); + this.setCellFormat( fSheet, cellStyle_time ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)(start_row+7)); + data_validation.setLastRow((short)(start_row+7)); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula(String.valueOf(HSSFDateUtil.getExcelDate(df.parse("14:00")))); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than or equal to 14:00", false, true, false ); + this.setCellFormat( fSheet, cellStyle_time ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + System.out.println("done !"); + + //"Text length" validation type + System.out.print(" Create sheet for 'Text length' Data Validation type... "); + fSheet = wb.createSheet("Text length"); + this.createHeaderRow( wb, 3, style_4 ); + + data_validation = new HSSFDataValidation((short)1,(short)0,(short)1,(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_TEXT_LENGTH); + data_validation.setOperator(HSSFDataValidation.OPERATOR_BETWEEN); + data_validation.setFirstFormula("2"); + data_validation.setSecondFormula("6"); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Between 2 and 6 ", true, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)2); + data_validation.setLastRow((short)2); + data_validation.setEmptyCellAllowed(false); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_BETWEEN); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_INFO); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not between 2 and 6 ", false, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = INFO" ); + + data_validation.setFirstRow((short)3); + data_validation.setLastRow((short)3); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_EQUAL); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_WARNING); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Equal to 3", false, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = WARNING" ); + + data_validation.setFirstRow((short)4); + data_validation.setLastRow((short)4); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_NOT_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Not equal to 3", false, false, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)5); + data_validation.setLastRow((short)5); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than 3", true, false, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)6); + data_validation.setLastRow((short)6); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("3"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_THAN); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than 3", true, true, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + + data_validation.setFirstRow((short)7); + data_validation.setLastRow((short)7); + data_validation.setEmptyCellAllowed(true); + data_validation.setShowPromptBox(false); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_STOP); + data_validation.setShowErrorBox(true); + data_validation.setFirstFormula("4"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_GREATER_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Greater than or equal to 4", true, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation.setFirstRow((short)8); + data_validation.setLastRow((short)8); + data_validation.setEmptyCellAllowed(false); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(false); + data_validation.setFirstFormula("4"); + data_validation.setSecondFormula(null); + data_validation.setOperator(HSSFDataValidation.OPERATOR_LESS_OR_EQUAL); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "Less than or equal to 4", false, true, false ); + this.writeOtherSettings( fSheet, style_1, "-" ); + System.out.println("done !"); + + //Custom Validation type + System.out.print(" Create sheet for 'Custom' Data Validation type ... "); + fSheet = wb.createSheet("Custom"); + this.createHeaderRow( wb, 4, style_4 ); + + data_validation = new HSSFDataValidation((short)1,(short)0,(short)1,(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_FORMULA); + data_validation.setFirstFormula("ISNUMBER($A2)"); + data_validation.setSecondFormula(null); + data_validation.setShowPromptBox(true); + data_validation.setShowErrorBox(true); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.createPromptBox("Hi , dear user !", "So , you just selected me ! Thanks !"); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "ISNUMBER(A2)", true, true, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = STOP" ); + + data_validation = new HSSFDataValidation((short)2,(short)0,(short)2,(short)0); + data_validation.setDataValidationType(HSSFDataValidation.DATA_TYPE_FORMULA); + data_validation.setFirstFormula("IF(SUM(A2:A3)=5,TRUE,FALSE)"); + data_validation.setSecondFormula(null); + data_validation.setShowPromptBox(false); + data_validation.setShowErrorBox(true); + data_validation.setErrorStyle(HSSFDataValidation.ERROR_STYLE_WARNING); + data_validation.createErrorBox("Invalid input !", "Something is wrong ; check condition !"); + data_validation.setEmptyCellAllowed(false); + fSheet.addValidationData(data_validation); + this.writeDataValidationSettings( fSheet, style_1, style_2, "IF(SUM(A2:A3)=5,TRUE,FALSE)", false, false, true ); + this.writeOtherSettings( fSheet, style_1, "Error box type = WARNING" ); + + System.out.println("done !"); + + //so , everything it's ok for now ; it remains for you to open the file + System.out.println("\n Everything it's ok since we've got so far -:) !\n"+ + " In order to complete the test , it remains for you to open the file \n"+ + " and see if there are four sheets , as described !"); + System.out.println(" File was saved in \""+resultFile+"\""); + + FileOutputStream fileOut = new FileOutputStream(resultFile); + wb.write(fileOut); + fileOut.close(); + } + + private void createDVTypeRow( HSSFWorkbook wb, int sheetNo , HSSFCellStyle cellStyle, String strTypeDescription) + { + HSSFSheet sheet = wb.getSheetAt(sheetNo); + HSSFRow row = sheet.createRow(sheet.getPhysicalNumberOfRows()); + row = sheet.createRow(sheet.getPhysicalNumberOfRows()); + sheet.addMergedRegion(new Region((short)(sheet.getPhysicalNumberOfRows()-1),(short)0,(short)(sheet.getPhysicalNumberOfRows()-1),(short)5)); + HSSFCell cell = row.createCell((short)0); + cell.setCellValue(strTypeDescription); + cell.setCellStyle(cellStyle); + row = sheet.createRow(sheet.getPhysicalNumberOfRows()); + } + + private void createDVDeescriptionRow( HSSFWorkbook wb, int sheetNo , HSSFCellStyle cellStyle, String strTypeDescription ) + { + HSSFSheet sheet = wb.getSheetAt(sheetNo); + HSSFRow row = sheet.getRow(sheet.getPhysicalNumberOfRows()-1); + sheet.addMergedRegion(new Region((short)(sheet.getPhysicalNumberOfRows()-1),(short)0,(short)(sheet.getPhysicalNumberOfRows()-1),(short)5)); + HSSFCell cell = row.createCell((short)0); + cell.setCellValue(strTypeDescription); + cell.setCellStyle(cellStyle); + row = sheet.createRow(sheet.getPhysicalNumberOfRows()); + } + + private void createHeaderRow( HSSFWorkbook wb, int sheetNo , HSSFCellStyle cellStyle ) + { + HSSFSheet sheet = wb.getSheetAt(sheetNo); + HSSFRow row = sheet.createRow(sheet.getPhysicalNumberOfRows()); + row.setHeight((short)400); + for ( int i=0; i<6; i++ ) + { + row.createCell((short)i).setCellStyle( cellStyle ); + if ( i==2 || i==3 || i==4 ) + { + sheet.setColumnWidth( (short) i, (short) 3500); + } + else if ( i== 5) + { + sheet.setColumnWidth( (short) i, (short) 10000); + } + else + { + sheet.setColumnWidth( (short) i, (short) 8000); + } + } + HSSFCell cell = row.getCell((short)0); + cell.setCellValue("Data validation cells"); + cell = row.getCell((short)1); + cell.setCellValue("Condition"); + cell = row.getCell((short)2); + cell.setCellValue("Allow blank"); + cell = row.getCell((short)3); + cell.setCellValue("Prompt box"); + cell = row.getCell((short)4); + cell.setCellValue("Error box"); + cell = row.getCell((short)5); + cell.setCellValue("Other settings"); + } + + private HSSFCellStyle createHeaderStyle(HSSFWorkbook wb) + { + HSSFFont font = wb.createFont(); + font.setColor( HSSFColor.WHITE.index ); + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + + HSSFCellStyle cellStyle = wb.createCellStyle(); + cellStyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index); + cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); + cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); + cellStyle.setLeftBorderColor(HSSFColor.WHITE.index); + cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); + cellStyle.setTopBorderColor(HSSFColor.WHITE.index); + cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); + cellStyle.setRightBorderColor(HSSFColor.WHITE.index); + cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); + cellStyle.setBottomBorderColor(HSSFColor.WHITE.index); + cellStyle.setFont(font); + return cellStyle; + } + + private HSSFCellStyle createStyle( HSSFWorkbook wb, short h_align, short color, boolean bold ) + { + HSSFFont font = wb.createFont(); + if ( bold ) + { + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + } + + HSSFCellStyle cellStyle = wb.createCellStyle(); + cellStyle.setFont(font); + cellStyle.setFillForegroundColor(color); + cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + cellStyle.setAlignment(h_align); + cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); + cellStyle.setLeftBorderColor(HSSFColor.BLACK.index); + cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); + cellStyle.setTopBorderColor(HSSFColor.BLACK.index); + cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); + cellStyle.setRightBorderColor(HSSFColor.BLACK.index); + cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); + cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); + + return cellStyle; + } + + private HSSFCellStyle createStyle( HSSFWorkbook wb, short h_align ) + { + return this.createStyle(wb, h_align, HSSFColor.WHITE.index, false); + } + + private void writeDataValidationSettings( HSSFSheet sheet, HSSFCellStyle style_1, HSSFCellStyle style_2, String strCondition, boolean allowEmpty, boolean inputBox, boolean errorBox ) + { + HSSFRow row = sheet.createRow( sheet.getPhysicalNumberOfRows() ); + //condition's string + HSSFCell cell = row.createCell((short)1); + cell.setCellStyle(style_1); + cell.setCellValue(strCondition); + //allow empty cells + cell = row.createCell((short)2); + cell.setCellStyle(style_2); + cell.setCellValue( ((allowEmpty) ? "yes" : "no") ); + //show input box + cell = row.createCell((short)3); + cell.setCellStyle(style_2); + cell.setCellValue( ((inputBox) ? "yes" : "no") ); + //show error box + cell = row.createCell((short)4); + cell.setCellStyle(style_2); + cell.setCellValue( ((errorBox) ? "yes" : "no") ); + } + + private void setCellFormat( HSSFSheet sheet, HSSFCellStyle cell_style ) + { + HSSFRow row = sheet.getRow( sheet.getPhysicalNumberOfRows() -1 ); + HSSFCell cell = row.createCell((short)0); + cell.setCellStyle(cell_style); + } + + private void writeOtherSettings( HSSFSheet sheet, HSSFCellStyle style, String strStettings ) + { + HSSFRow row = sheet.getRow( sheet.getPhysicalNumberOfRows() -1 ); + HSSFCell cell = row.createCell((short)5); + cell.setCellStyle(style); + cell.setCellValue(strStettings); + } + + public static void main(String[] args) + { + junit.textui.TestRunner.run(TestDataValidation.class); + } +}