Example usage for org.apache.poi.xssf.usermodel XSSFSheet rowIterator

List of usage examples for org.apache.poi.xssf.usermodel XSSFSheet rowIterator

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFSheet rowIterator.

Prototype

@Override
@SuppressWarnings("unchecked")
public Iterator<Row> rowIterator() 

Source Link

Usage

From source file:parser.CloudDSFPlusParser.java

License:Apache License

/**
 * Retrieves influencing relations between decisions.
 * /*from   www  . j a v  a2 s .  co  m*/
 * @return
 */
private void setInfluencingRelations() {
    XSSFSheet sheet = workbook.getSheet("Decision Level");
    // Column B has name of start Decision
    int startDecisionColumn = 1;
    // Row 1 has names of endDecision
    Row endDecisionRow = sheet.getRow(1);
    // Iterate over all rows starting at 3
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        XSSFRow row = (XSSFRow) rows.next();
        // select cell C
        Iterator<Cell> cells = row.cellIterator();
        // Iterate of all cells in row
        while (cells.hasNext()) {
            XSSFCell cell = (XSSFCell) cells.next();
            String relationType = cell.getStringCellValue();
            if (relationType.equals("Influencing") || relationType.equals("Affecting")
                    || relationType.equals("Binding")) {
                // if type of relationship matches predefined values get names of the two participating
                // decisions
                String startDecision = row.getCell(startDecisionColumn).getStringCellValue();
                String endDecision = endDecisionRow.getCell(cell.getColumnIndex()).getStringCellValue();
                // add decision relation to cloudDSFPlus
                cdsf.setDecisionRelation(startDecision, endDecision, relationType, null);
            }
        }
    }
}

From source file:parser.CloudDSFPlusParser.java

License:Apache License

/**
 * Retrieves requiring relations between decisions.
 * //from ww  w. j  a  v a  2 s.  c o  m
 * @return
 */
private void setRequiringRelations() {
    XSSFSheet sheet = workbook.getSheet("Required Level");
    // Column B has name of start Decision
    int startDecisionColumn = 1;
    // Row 1 has names of endDecision
    Row endDecisionRow = sheet.getRow(1);
    // Iterate over all rows starting at 3
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        XSSFRow row = (XSSFRow) rows.next();
        Iterator<Cell> cells = row.cellIterator();
        while (cells.hasNext()) {
            XSSFCell cell = (XSSFCell) cells.next();
            String relationType = cell.getStringCellValue();
            if (relationType.equals("Requiring")) {
                // if requiring relationship is denoted get names of both decisions
                String startDecision = row.getCell(startDecisionColumn).getStringCellValue();
                String endDecision = endDecisionRow.getCell(cell.getColumnIndex()).getStringCellValue();
                // add requiring relation to cloudDSFPlus
                cdsf.setDecisionRelation(startDecision, endDecision, relationType, null);
            }
        }
    }
}

From source file:parser.CloudDSFPlusParser.java

License:Apache License

/**
 * Retrieves relations between outcomes.
 * /*from   w  ww .ja va  2s . co  m*/
 * @return
 */
private void setInfluencingOutcomes() {
    XSSFSheet sheet = workbook.getSheet("Outcome Level");
    // Column B has name of start Decision
    int startOutcomeColumn = 1;
    // Row 1 has names of endDecision
    Row endOutcomeRow = sheet.getRow(0);
    // Iterate over all rows
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        XSSFRow row = (XSSFRow) rows.next();
        Iterator<Cell> cells = row.cellIterator();
        // Iterate over all cells
        while (cells.hasNext()) {
            XSSFCell cell = (XSSFCell) cells.next();
            String relationType = cell.getStringCellValue();
            if (relationType.equals("in") || relationType.equals("ex") || relationType.equals("a")
                    || relationType.equals("eb") || relationType.equals("aff")) {
                // if relationship is denoted get names of both outcomes
                String startOutcome = row.getCell(startOutcomeColumn).getStringCellValue();
                String endOutcome = endOutcomeRow.getCell(cell.getColumnIndex()).getStringCellValue();
                // add new outcome relation to cloudDSFPlus
                cdsf.setOutcomeRelation(startOutcome, endOutcome, relationType, null, null);
            }
        }
    }
}

From source file:preprocessing.Utils.java

public static int cumpleFormatoExcelRNV(String ruta) {
    InputStream ExcelFileToRead = null;
    try {/*w  w  w.  j a v a2 s.  c o m*/
        //regresa -1 si NO cumpel formato
        // de lo contrario regresa la cantidad de registros que tiene
        ExcelFileToRead = new FileInputStream(ruta);
        XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row;
        XSSFCell cell;
        Iterator rows = sheet.rowIterator();
        row = (XSSFRow) rows.next();
        Iterator cells = row.cellIterator();
        cell = (XSSFCell) cells.next();
        if (cell.getCellType() != XSSFCell.CELL_TYPE_STRING)
            return -1;
        String dh = cell.getStringCellValue();
        dh = dh.toUpperCase();
        if (dh.compareTo("NOMBRE") != 0)
            return -1;
        cell = (XSSFCell) cells.next();
        if (cell.getCellType() != XSSFCell.CELL_TYPE_STRING)
            return -1;
        dh = cell.getStringCellValue();
        dh = dh.toUpperCase();
        if (dh.compareTo("APELLIDOS") != 0)
            return -1;
        cell = (XSSFCell) cells.next();
        if (cell.getCellType() != XSSFCell.CELL_TYPE_STRING)
            return -1;
        dh = cell.getStringCellValue();
        dh = dh.toUpperCase();
        if (dh.compareTo("DNI") != 0)
            return -1;
        cell = (XSSFCell) cells.next();
        if (cell.getCellType() != XSSFCell.CELL_TYPE_STRING)
            return -1;
        dh = cell.getStringCellValue();
        dh = dh.toUpperCase();
        if (dh.compareTo("UBIGEO") != 0)
            return -1;
        cell = (XSSFCell) cells.next();
        if (cell.getCellType() != XSSFCell.CELL_TYPE_STRING)
            return -1;
        dh = cell.getStringCellValue();
        dh = dh.toUpperCase();
        if (!dh.matches("HUELLA.*"))
            return -1;
        cell = (XSSFCell) cells.next();
        if (cell.getCellType() != XSSFCell.CELL_TYPE_STRING)
            return -1;
        dh = cell.getStringCellValue();
        dh = dh.toUpperCase();
        if (!dh.matches("FIRMA.*"))
            return -1;
        int cant = 0;
        while (rows.hasNext()) {
            row = (XSSFRow) rows.next();
            cells = row.cellIterator();
            cell = (XSSFCell) cells.next();
            if (cell.getCellType() != XSSFCell.CELL_TYPE_STRING)
                break;
            cant++;
        }
        return cant;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            ExcelFileToRead.close();
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return -1;
}

From source file:ru.letoapp.tests.RegistrationQuickDDTest.java

public String[][] getTestDataFromXlsxFile() throws Exception {
    String path = "src/main/resources/TestData.xlsx";
    String[][] dataList = new String[2][2];
    FileInputStream fis = null;/*from   w w w . ja va2 s .  c  om*/
    try {
        fis = new FileInputStream(new File(path));
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheet("TestData");
        Iterator<Row> rows = sheet.rowIterator();
        if (rows.hasNext()) {
            rows.next();
        }
        for (int i = 0; rows.hasNext(); ++i) {
            XSSFRow row = ((XSSFRow) rows.next());
            Iterator<Cell> cells = row.cellIterator();
            for (int j = 0; cells.hasNext(); ++j) {
                XSSFCell cell = (XSSFCell) cells.next();
                String value = cell.getStringCellValue();
                if (!value.equals(null)) {
                    dataList[i][j] = value;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dataList;
}

From source file:si_piket_smkn3.frm_tambah_guru.java

public static Vector read(String fileName) {
    Vector cellVectorHolder = new Vector();
    try {/*from ww w  .ja  va  2s . c  o  m*/
        FileInputStream myInput = new FileInputStream(fileName);
        //POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
        XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator rowIter = mySheet.rowIterator();
        while (rowIter.hasNext()) {
            XSSFRow myRow = (XSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();
            //Vector cellStoreVector=new Vector();
            List list = new ArrayList();
            while (cellIter.hasNext()) {
                XSSFCell myCell = (XSSFCell) cellIter.next();
                list.add(myCell);
            }
            cellVectorHolder.addElement(list);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cellVectorHolder;
}

From source file:utils.ReadLLDSheet.java

@SuppressWarnings("unchecked")
public static List ReadLLDSheet(String FileName, int SheetNo) throws IOException {
    ////from w  ww . ja  va  2s.c  o m
    //
    // Create an ArrayList to store the data read from excel sheet.
    //
    List sheetData = new ArrayList();

    FileInputStream fis = null;
    try {
        //
        // Create a FileInputStream that will be use to read the excel file.
        //
        fis = new FileInputStream(FileName);

        //
        // Create an excel workbook from the file system.
        //
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        //
        // Get the first sheet on the workbook.
        //
        XSSFSheet sheet = workbook.getSheetAt(SheetNo);

        //
        // When we have a sheet object in hand we can iterator on each
        // sheet's rows and on each row's cells. We store the data read
        // on an ArrayList so that we can printed the content of the excel
        // to the console.
        //
        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();
            Iterator cells = row.cellIterator();

            List data = new ArrayList();
            while (cells.hasNext()) {
                XSSFCell cell = (XSSFCell) cells.next();
                data.add(cell);
            }

            sheetData.add(data);
        }
    } catch (IOException e) {
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
    return sheetData;

}

From source file:utils.ReadWriteExcelFile.java

public static void readXLSXFile(String aFile, int SheetNo) throws IOException {

    InputStream ExcelFileToRead = new FileInputStream(aFile);
    XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);

    XSSFWorkbook test = new XSSFWorkbook();

    XSSFSheet sheet = wb.getSheetAt(SheetNo);
    XSSFRow row;//from  w ww  . ja v a2s. c o  m
    XSSFCell cell;
    CTSheetDimension dimension = sheet.getCTWorksheet().getDimension();
    String sheetDimensions = dimension.getRef();
    System.out.println(sheetDimensions);
    List<String> dimensions = StringUtils.split(sheetDimensions, ":", true);
    String[] Dimensions = dimensions.get(1).toString().split("(?<=\\D)(?=\\d)");
    int Colums = CharToInt(Dimensions[0]);
    int Rows = Integer.parseInt(Dimensions[1]);
    System.out.println();
    Iterator rows = sheet.rowIterator();
    ArrayList[][] TableName = new ArrayList[Rows][Colums];
    System.out.println(TableName.length);
    int currentRow = 0;
    while (rows.hasNext()) {

        row = (XSSFRow) rows.next();
        Iterator cells = row.cellIterator();
        int currentCell = 0;
        //         System.out.println("currentRow="+currentRow+" And Current Colum is ="+currentCell);
        while (cells.hasNext()) {
            cell = (XSSFCell) cells.next();

            if (cell.getStringCellValue().isEmpty()) {
                TableName[cell.getRowIndex()][cell.getColumnIndex()].add(" - ");
                //               System.out.print(cell.getStringCellValue()+" ");
                //                                        TableName[currentRow][currentCell].add(" ");
                //            System.out.println("Cell Value is : "+cell.toString());
                //                                System.out.println("Empty cell currentRow="+currentRow+" And Current Colum is ="+currentCell);

            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                System.out.println("current Row=" + cell.getRowIndex() + " And Current Colum is ="
                        + cell.getColumnIndex());
                System.out.println(cell.getRichStringCellValue());
                TableName[cell.getRowIndex()][cell.getColumnIndex()]
                        .add(cell.getRichStringCellValue().toString());
                //                                    System.out.println("Cell Type is :"+cell.getCellType());
                //                                    System.out.println("Cell Value is : "+cell.getRichStringCellValue());
            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                TableName[cell.getRowIndex()][cell.getColumnIndex()].add(cell.getNumericCellValue());
                //                                    System.out.println("Cell Type is :"+cell.getCellType());
                //                                    System.out.println("current Row="+cell.getRowIndex()+" And Current Colum is ="+cell.getColumnIndex());
                //                                    int numericValue = (int) cell.getNumericCellValue();
                //                                    System.out.println("Cell Value is : "+numericValue);

            }
            //            else
            //            {
            //               //U Can Handel Boolean, Formula, Errors
            //            }
            currentCell++;
        }
        System.out.println();
        currentRow++;
    }
    for (int i = 0; TableName.length > i; i++) {
        for (int j = 0; TableName[i].length > j; j++) {
            System.out.println(TableName[i][j].toString());
        }

    }
}

From source file:vn.vnpttech.ssdc.nms.webapp.action.MatAction.java

License:Apache License

private List<Materials> processExcelFile(File file) throws IOException {
    List<Materials> list = new ArrayList<Materials>();

    //try {//w w w . ja v a  2s  .c o  m
    // Creating Input Stream
    FileInputStream myInput = new FileInputStream(file);

    // Create a workbook using the File System 
    XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);

    // Get the first sheet from workbook 
    XSSFSheet mySheet = myWorkBook.getSheetAt(0);

    Materials item = new Materials();

    /**
     * We now need something to iterate through the cells.*
     */
    Iterator<Row> rowIter = mySheet.rowIterator();
    while (rowIter.hasNext()) {
        XSSFRow myRow = (XSSFRow) rowIter.next();
        if (myRow.getRowNum() != 0) {// loi b? header

            Iterator<Cell> cellIter = myRow.cellIterator();
            while (cellIter.hasNext()) {
                XSSFCell myCell = (XSSFCell) cellIter.next();

                if (myCell.getCellType() != HSSFCell.CELL_TYPE_STRING) {
                    myCell.setCellType(Cell.CELL_TYPE_STRING);
                }
                String value = myCell.getStringCellValue().trim();

                switch (myCell.getColumnIndex()) {
                case 0: //stt ko lam gi
                    //                        item.setProvince(value);
                    break;

                case 1: //name
                    item.setName(value);
                    break;

                //                        case 2: //unit
                //                            if (StringUtils.isNotBlank(value)) {
                //                                item.setUnit(Double.parseDouble(value));
                //                            } else {
                //                                item.setUnit(0);
                //                            }
                //                            break;
                case 2: //unit price
                    if (StringUtils.isNotBlank(value)) {
                        item.setUnitPrice(Double.parseDouble(value));
                    } else {
                        item.setUnitPrice(0.0);
                    }
                    break;

                case 3: //weight
                    if (StringUtils.isNotBlank(value)) {
                        item.setWeight(Double.parseDouble(value));
                    } else {
                        item.setWeight(0.0);
                    }
                    break;

                default:
                    break;
                }

            }
            list.add(item);

            if (item.getName() != null) {
                item = new Materials(); //for next row
            }

        }
    }
    return list;
}