anshumanonly4u / Anshuman

Desc
0 stars 0 forks source link

Excel Reader #3

Open anshumanonly4u opened 5 years ago

anshumanonly4u commented 5 years ago

package com.xml.comparator;

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List;

import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {

public List<PathInfo> getPathList(String path) {
    List<PathInfo> bomAccordNodePathInfoList = new ArrayList<PathInfo>();
    System.out.println(path);
     try {

            FileInputStream excelFile = new FileInputStream(new File(path));
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet datatypeSheet = workbook.getSheetAt(2);
            Iterator<Row> iterator = datatypeSheet.iterator();

            int rowCount = 0;
            while (iterator.hasNext()) {
                rowCount ++;    

                if(rowCount>100)
                break;

                Row currentRow = iterator.next();
                if(rowCount < 1)
                    continue;

                //Iterator<Cell> cellIterator = currentRow.iterator();
                //Cell dctSlNoCell = currentRow.getCell(0);
                Cell dctPageNameCell = currentRow.getCell(2);
                Cell dctFieldNameCell = currentRow.getCell(5);
                Cell bomCell = currentRow.getCell(21);
                Cell accordCell = currentRow.getCell(30);
                Cell sourceCell = currentRow.getCell(33);

                Cell dctSlNoCell = currentRow.getCell(1);
                String slno = "";
                if (dctSlNoCell.getCellTypeEnum() == CellType.STRING) {
                    slno = dctSlNoCell.getStringCellValue();
                     } else if (dctSlNoCell.getCellTypeEnum() == CellType.NUMERIC) {
                     slno = (int)dctSlNoCell.getNumericCellValue() + "";
                     }

                String dctPageNameCellText = dctPageNameCell != null ? dctPageNameCell.getStringCellValue().trim() : "";
                String dctFieldNameCellText = dctFieldNameCell != null ? dctFieldNameCell.getStringCellValue().trim() : "";
                String bomCellText = bomCell != null ? bomCell.getStringCellValue().trim() : "";
                String accordCellText = accordCell != null?accordCell.getStringCellValue().trim():"";
                String sourceCellText = sourceCell != null ? sourceCell.getStringCellValue().trim(): "";

                if( ("PDM".equalsIgnoreCase(sourceCellText) && !"".equals(bomCellText) && !"".equals(accordCellText))

                        || ("Phoenix".equalsIgnoreCase(sourceCellText) && !"".equals(accordCellText))) {

                    PathInfo pathInfo = new PathInfo(bomCellText, accordCellText, sourceCellText, dctFieldNameCellText, slno, dctPageNameCellText);
                    //pathInfo.setDctFieldName(dctFieldNameCellText);
                    bomAccordNodePathInfoList.add(pathInfo);
                }   

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    return bomAccordNodePathInfoList;
}

}