替换为poi 3.17中的getcelltype而不是getCellTypeEnum()

时间:2017-11-02 06:09:21

标签: java excel apache-poi

我正在阅读一个excel文件,并使用apache poi 3.17将其转换为XML文件。我使用getCelltype获取当前Cell的CELLTYPE。但我得到“getCellType()折旧”。我搜索了其他问题,但我发现我应该使用“CellType getCellTypeEnum()”。但这也显示出同样的警告。我搜索了Apache POI文档,但写了"getCellTypeEnum()"已弃用,使用getCellType而不是“。我无法理解我应该使用什么来删除警告。如果有人可以帮助getCellType()的其他替代方案,可以在这种情况下使用。

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;
}

1 个答案:

答案 0 :(得分:1)

您应该可以使用getCellTypeEnumn()应用更改。在Poi 3.17中不推荐使用Cell.CELL_TYPE_*枚举,但您可以使用Celltype.*,例如:

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

Celltype的值为:

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

希望这可以帮到你。

感谢。