2

I am reading one excel file and converting it to a XML file using apache poi 3.17. I am using getCelltype for getting the CELLTYPE of the current Cell. But I am getting "getCellType() is depreciated". I searched for other questions but what I found that I should use "CellType getCellTypeEnum()". But that is also showing the same warning. I searched in the Apache POI documentation but its written that "getCellTypeEnum()" is depreciated and "use getCellType instead". I can't understand what should I use to remove the warning. If anyone can help what are other alternatives for getCellType() that are usable in this context.

try {
            log.info("Getting the path for Input Excel File");
           // String excelPath = "D:\\Tools\\BaNCS_Dictionary_Specification_1.0_Latest.xlsx";
            String excelPath=InputExcel.inputExcel();
        FileInputStream fileInputStream = new FileInputStream(new File(excelPath));

        // Create Workbook instance holding .xls file
        log.info("Creating workbook instance for XSSFWorkbook");
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

        // Get the first worksheet
        log.info("Creating the sheet of XSSFSheet");
        XSSFSheet sheet = workbook.getSheet("DictionarySpecification");
        int i=InputExcelValidator.DictionarySpecificationRowsValidator(sheet);

        // Iterate through each rows
        log.info("Creating RowIterator to iterate through the rows");
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            // Get Each Row
            log.info("Getting each row one by one");
            Row row = rowIterator.next();
            log.info("Assigning the value of tempForEmptyRowCount2 to tempForEmptyRowCount1");
            int tempForEmptyRowCount2=tempForEmptyRowCount1;
            log.info("Getting the rowNumber of Current Iteration");
            tempForEmptyRowCount1=row.getRowNum();
            if(row.getRowNum()==0){
                log.info("Skipping The first row of the Excel File");
                continue;
            }
            if (row.getRowNum() <= 25 && row.getRowNum()>0) {
                log.info("Adding the Dictionary Specification Values and calling addDictionarySpecifications");
                addDictionarySpecifications(tempForEmptyRowCount2, rowIterator, row);
                continue;
            }
            if(row.getRowNum()-tempForEmptyRowCount2>2){
                log.error("Please correct the format to input table details");
                throw new TableInputFormatException("Please correct the format to input table details");
            }

            if (row.getRowNum()-tempForEmptyRowCount2>1) {
                log.info("Skipping Empty Row");
                tempForAddingValuesToTable1 = 1;
                tempForAddingValuesToTable2 = false;
                tempForTableCount++;
                continue;
            }
            log.info("Creating CellIterator inatance");
            Iterator<Cell> cellIterator = row.cellIterator();
            if (tempForAddingValuesToTable1 == 1 && tempForAddingValuesToTable2 == false) {
                log.info("Adding the table values");
                Table table = new Table();
                log.info("Creating an empty arrayList in Table for fields");
                table.setFieldList(new ArrayList<Field>());
                while (cellIterator.hasNext()) {
                    log.info("Iterating the cells for table values");
                    Cell cell = cellIterator.next();
                    int columnIndex = cell.getColumnIndex();
                    switch (columnIndex + 1) {
                        case 1:
                            log.info("Adding the table name");
                            table.setName(cell.getStringCellValue().trim());
                            break;
                        case 2:
                            log.info("Adding the table annotation");
                            table.setAnnotation(cell.getStringCellValue().trim());
                            break;
                        case 3:
                            log.info("Adding the table Identifier");
                            table.setIdentifier(cell.getStringCellValue().trim());
                            break;
                        case 4:
                            log.info("Adding the table FieldPosition");
                            switch (cell.getCellType()){
                                case Cell.CELL_TYPE_STRING:
                                    table.setFieldPosition(Integer.parseInt( cell.getStringCellValue().trim()));
                                    break;
                                case Cell.CELL_TYPE_NUMERIC:
                                    table.setFieldPosition((int) cell.getNumericCellValue());
                                    break;
                            }

                            break;
                        case 5:
                            log.info("Adding the table RecordType");
                            switch (cell.getCellType()){
                                case Cell.CELL_TYPE_STRING:
                                    table.setRecordType( cell.getStringCellValue());
                                    break;
                                case Cell.CELL_TYPE_NUMERIC:
                                    table.setRecordType( String.valueOf(cell.getNumericCellValue()));
                                    break;
                            }
                            break;
                    }
                }
                log.info("Adding the table to Tables");
                tables.getTableList().add(table);
                tempForAddingValuesToTable2=true;
                continue;
            }
            if (tempForAddingValuesToTable1 == 1 && tempForAddingValuesToTable2 == true) {
                log.info("Skipping Empty Row");
                tempForAddingValuesToTable1=0;
                continue;
            }
            if (tempForAddingValuesToTable1 == 0 && tempForAddingValuesToTable2 == true) {
                log.info("Creating Field Instance");
                Field field = new Field();
                while (cellIterator.hasNext()) {
                    log.info("Adding the field values one by one");
                    Cell cell = cellIterator.next();
                    int columnIndex = cell.getColumnIndex();
                    int tempColumnIndex = columnIndexIndicator;
                    if (tempColumnIndex - columnIndexIndicator > 1) {
                        field.setDefaultValue("null");
                    }
                    switch (columnIndex + 1) {
                        case 1:
                            log.info("Adding name for field");
                            field.setName(cell.getStringCellValue().trim());
                            break;
                        case 2:
                            log.info("Adding Annotation for field");
                            field.setAnnotation(cell.getStringCellValue().trim());
                            break;
                        case 3:
                            log.info("Adding DataType for field");
                            field.setDataType(cell.getStringCellValue());
                            break;

                        case 4:
                            log.info("Adding Size for field");
                            switch (cell.getCellType()) {
                                case Cell.CELL_TYPE_STRING:
                                    field.setSize(cell.getStringCellValue().trim());
                                    break;
                                case Cell.CELL_TYPE_NUMERIC:
                                    Double doubleValue=cell.getNumericCellValue();
                                    DecimalFormat format = new DecimalFormat("0.#");
                                    field.setSize(format.format(doubleValue));
                                    break;
                            }
                            break;

                        case 5:
                            log.info("Adding pKey for field");
                            if (cell.getCellType() == cell.CELL_TYPE_STRING) {
                                String s2 = cell.getStringCellValue();

                                if (s2.equals("Yes"))
                                    tempForAddingBooleanValuesToTable = true;
                                else
                                    tempForAddingBooleanValuesToTable = false;
                                field.setpKey(tempForAddingBooleanValuesToTable);
                            }
                            break;
                        case 6:
                            log.info("Adding Mandatory for field");
                            if (cell.getStringCellValue().equals("Yes"))
                                tempForAddingBooleanValuesToTable = true;
                            else
                                tempForAddingBooleanValuesToTable = false;
                            field.setMandatory(tempForAddingBooleanValuesToTable);
                            break;
                        case 7:
                            log.info("Adding DefaultValue for field");
                            switch (cell.getCellType()) {
                                case Cell.CELL_TYPE_STRING:
                                    field.setDefaultValue(cell.getStringCellValue().trim());

                                    break;
                                case Cell.CELL_TYPE_NUMERIC:
                                    Double doubleValue=cell.getNumericCellValue();
                                    DecimalFormat format = new DecimalFormat("0.####");
                                    field.setDefaultValue(format.format(doubleValue));
                                    break;
                            }
                            break;
                        case 8:
                            log.info("Adding Rec_Identifier for field");
                            if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
                                int i2 = (int) cell.getNumericCellValue();
                                String s = String.valueOf(i2);
                                if (s.equals("Yes"))
                                    tempForAddingBooleanValuesToTable = true;
                                else
                                    tempForAddingBooleanValuesToTable = false;
                                field.setRec_Identifier(tempForAddingBooleanValuesToTable);
                            }
                            break;
                        case 9:
                            log.info("Adding Visible for field");
                            if (cell.getStringCellValue().equals("Yes"))
                                tempForAddingBooleanValuesToTable = true;
                            else
                                tempForAddingBooleanValuesToTable = false;
                            field.setVisible(tempForAddingBooleanValuesToTable);
                            break;

                    }
                    columnIndexIndicator = cell.getColumnIndex();


                }
                if(field.getSize()==null){
                    log.info("Setting the DateFormat");
                    field.setDateFormat("DDMMYYYY");
                }
                log.info("Adding the field to specific table");
                tables.getTableList().get(tempForTableCount).getFieldList().add(field);
            }
        }
        log.info("Setting properties to respective lists");
        recordProperties.setProperties(propertyList1);
        fileProperties.setProperties(propertyList2);
        dataDictionary.setDictionary(dictionary);
        dataDictionary.setFileProperties(fileProperties);
        dataDictionary.setRecordProperties(recordProperties);
        dataDictionary.setTables(tables);
    }
    catch (IOException ie) {
        log.error("Exception Occured: IOException");
        ie.printStackTrace();
    }
    log.info("Calling the createXML to generate the xml file");
    CreateXML.createXML(dataDictionary);
}
public static int addDictionarySpecifications(int j, Iterator<Row> rowIterator,Row row){
   log.info("Getting the CellIterator");
    Iterator<Cell> cellIterator = row.cellIterator();


    if(row.getRowNum()-j>1){
        log.info("Getting Empty Row");
        rowIndicator++;
        tempForSkippingRow=true;
        // return 0;
    }
    if(tempForSkippingRow){
        log.info("Skipping Row");
        tempForSkippingRow=false;
        return 0;

    }
    log.info("Creating instance of Property Object");
    Property property=new Property();
    while (cellIterator.hasNext()) {
        log.info("Getting Cells one by one");
        Cell cell = cellIterator.next();
        log.info("Getting the columnIndex of Current Row");
        int columnIndex = cell.getColumnIndex();

        if(rowIndicator==1) {
            log.info("Adding the Dictionary Nsme and annotation and type");
            switch (columnIndex + 1) {
                case 2:
                    dictionary.setName(cell.getStringCellValue().trim());
                    dictionary.setAnnotation(dictionary.getName());
                    dictionary.setType("DATABASE_TABLES");
                    break;
            }
        }
        if(rowIndicator>1){
            log.info("Adding the Properties for FileProperties and Record Properties");
            switch(columnIndex+1){
                case 1:
                    log.info("GEtting the annotation");
                    String annotation=cell.getStringCellValue().trim();
                    String propertyName;
                    if(annotation.equals("Record Composition")) {
                        log.info("Property is Record Composition");
                        property.setName("COMPOSITION");
                    }
                    else if(annotation.equals("Record Heirarchy")) {
                        log.info("Property is Record Heirarchy");
                        property.setName("HEIRARCHY");
                    }
                    else if(annotation.equals("Set Feed Quality Standards")) {
                        log.info("Property is Set Feed Quality Standards");
                        property.setName("FQD");
                        property.setAnnotation("FQD");
                        break;
                    }
                    else if(annotation.equals("Quality Percentage")) {
                        log.info("Property is Quality Percentage");
                        property.setName("FQD_PERCENT");
                        property.setAnnotation("FQD Percent");
                        break;
                    }
                    else if(annotation.equals("Header Record")) {
                        log.info("Property is Header Record");
                        property.setName("HEADER");
                    }
                    else if(annotation.equals("Trailer Record")) {
                        log.info("Property is Trailer Record");
                        property.setName("TRAILER");
                    }
                    else {
                        log.info("Creating exact name for Property");
                        String[] split = annotation.split("\\s+");
                        String s1=split[0].toUpperCase();
                        String s2=split[1].toUpperCase();
                        propertyName=s1+"_"+s2;
                        log.info("Property is"+propertyName);
                        property.setName(propertyName);
                    }
                    property.setAnnotation(annotation);
                    break;
                case 2:
                    log.info("Setting the valuedescription");
                    String valueDesscription=null;
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            valueDesscription=cell.getStringCellValue().trim();

                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            try {
                                valueDesscription=String.valueOf(cell.getNumericCellValue()).trim();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            break;
                    }

                    if(valueDesscription.equals("Multiple Type of Records"))
                        property.setDefaultValue("HETEROGENOUS");
                    else if(valueDesscription.equals("Single Type of Records"))
                        property.setDefaultValue("HOMOGENOUS");
                    else if(valueDesscription.equals("Record Group Without heirarchy"))
                        property.setDefaultValue("GROUPED");
                    else if(valueDesscription.equals("Record Group With heirarchy"))
                        property.setDefaultValue("HEIRARCHY");
                    else if(valueDesscription.equals("Variable Length"))
                        property.setDefaultValue("VARIABLE_LENGTH");
                    else if(valueDesscription.equals("Carriage return line feed(CRLF)"))
                        property.setDefaultValue("CRLF");
                    else if(valueDesscription.equals("Comma(,)"))
                        property.setDefaultValue(",");
                    else if(valueDesscription.equals("Semicolon(;"))
                        property.setDefaultValue(";");
                    else if(valueDesscription.equals("Tilda(~)"))
                        property.setDefaultValue("~");
                    else if(valueDesscription.equals("Pipe(|)"))
                        property.setDefaultValue("|");
                    else if(valueDesscription.equals("Horizontal Tab(HT)"))
                        property.setDefaultValue("HT");
                    else if(valueDesscription.equals("Hash(#)"))
                        property.setDefaultValue("#");
                    else if(valueDesscription.equals("No"))
                        property.setDefaultValue("false");
                    else if(valueDesscription.equals("FAILED"))
                        property.setDefaultValue("FAILED");
                    else if(valueDesscription.equals("File Content"))
                        property.setDefaultValue("HASH_DUP");
                    else if(valueDesscription.equals("File Name"))
                        property.setDefaultValue("FILE_NAME");
                    else if(valueDesscription.equals("Yes"))
                        property.setDefaultValue("true");
                    else
                        property.setDefaultValue(valueDesscription);
                    property.setValueDescription(valueDesscription);
                    break;
                case 3:
                    log.info("Setting the annotation valueDescription and default Value");
                    property.setAnnotation("Duplicate Duration");
                    property.setName("DUPLICATE_DURATION");
                    property.setValueDescription(cell.getStringCellValue().trim());
                    property.setDefaultValue(property.getAnnotation());
                    break;
            }
        }
    }
    if(rowIndicator>1) {
        log.info("Adding the property to PropertyList");
        if (property.getDefaultValue().equals("HETEROGENOUS") || property.getDefaultValue().equals("HOMOGENOUS") || property.getDefaultValue().equals("GROUPED") || property.getDefaultValue().equals("HEIRARCHY"))
            propertyList1.add(property);
        else
            propertyList2.add(property);
    }
    return 0;
}
DevD
  • 23
  • 1
  • 8
  • 2
    Are you sure that you are using POI 3.17. It looks like both are deprecated in version 3.15 but solves in highest versions https://bz.apache.org/bugzilla/show_bug.cgi?id=60228 – ervidio Nov 02 '17 at 09:29
  • Thanks. But I am using POI 3.17. And It is warning as "Depreciated method". But in the documentation there is no clue of it. – DevD Nov 02 '17 at 09:53
  • 2
    See https://stackoverflow.com/questions/46989000/apache-poi-excel-alternatives-for-getcelltype-and-getcelltypeenumm#comment80926980_46989000. In apache poi version 3.17, the `Cell.getCellTypeEnum` is **not** deprecated. – Axel Richter Nov 02 '17 at 11:23
  • I tried DataFormatter class of Apache POI. But in this case I am not getting the double values as it is entered in the excel. It is by default getting converted to int. `String defaultValueFormatted=formatter.formatCellValue(cell); field.setDefaultValue(defaultValueFormatted);` – DevD Nov 02 '17 at 11:24
  • Thanks Axel. But `Cell.getCellTypeEnum` is also showing depreciated warning. – DevD Nov 02 '17 at 11:41

1 Answers1

2

You should be able to apply the change using the getCellTypeEnumn(). The Cell.CELL_TYPE_* enums are deprecated in Poi 3.17 but you can use Celltype.*, for example:

                if(cell.getCellTypeEnum() == CellType.STRING){
                     //your code
                }else  if(cell.getCellTypeEnum() == CellType.NUMERIC){
                    //your code
                }

Values for Celltype are:

_NONE(-1),
NUMERIC(0),
STRING(1),
FORMULA(2),
BLANK(3),
BOOLEAN(4),
ERROR(5);

Hope this can help you.

Thanks.

ervidio
  • 571
  • 5
  • 12
  • Thanks @ervidio. But `cell.getCellTypeEnum()` is showing as depreciated. The warning is same. – DevD Nov 02 '17 at 11:39
  • 1
    In that case you must be using POI 3.15, or you might be using the current unreleased trunk version of POI. I looked at the tags in the repository, and 3.15 was the last release that `getCellTypeEnum()` was deprecated, and the current release we are switching back to using `getCellType()`, so if you compile from the trunk, `getCellEnum()` is back to deprecated. You can use any version of POI 3.16 or POI 3.17 and you will be fine. – jmarkmurphy Nov 02 '17 at 13:26
  • Thanks @jmarkmurphy. But what do you mean by " compile from the trunk " – DevD Nov 03 '17 at 06:03
  • I think that was a bug only. Thanks @ervidio. Now it is working fine. – DevD Nov 03 '17 at 06:10
  • @user4563 I mean download the project from the trunk of the SVN repository, and build it yourself. This gets you the most recent release, and all the unreleased changes as well. It might be a bit unstable. – jmarkmurphy Nov 03 '17 at 11:04