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

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

Introduction

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

Prototype

@Override
public Iterator<Row> iterator() 

Source Link

Document

Alias for #rowIterator() to allow foreach loops

Usage

From source file:org.wandora.application.tools.extractors.excel.ExcelTopicTreeExtractor.java

License:Open Source License

public void processSheet(XSSFSheet sheet, TopicMap tm) {
    Iterator<Row> rowIterator = sheet.iterator();
    hierarchy = new Topic[1000];
    while (rowIterator.hasNext() && !forceStop()) {
        Row row = rowIterator.next();//from  w w  w .java  2  s. co  m
        processRow(row, tm);
    }
}

From source file:pl.exsio.ck.model.reader.XlsxEntryReaderImpl.java

License:Open Source License

@Override
public Collection<Entry> readEntries(File file, String progressName, boolean serialsOnly) {
    ProgressPresenter progress = ProgressHelper.showProgressBar(progressName, false);
    Row currentRow = null;//w  w  w . j  av  a2 s.c  o m
    Cell currentCell = null;
    ArrayList<Entry> entries = new ArrayList<>();
    try {
        XSSFSheet sheet = this.openSheet(file);

        Iterator<Row> rowIterator = sheet.iterator();
        int totalRowCount = sheet.getPhysicalNumberOfRows() - 1;
        int rowCounter = 0;
        while (rowIterator.hasNext()) {
            ProgressHelper.updateProgressBar(progress, (int) (rowCounter * 100 / totalRowCount));
            currentRow = rowIterator.next();
            if (currentRow.getRowNum() > 0) {
                Entry e = new EntryImpl();
                Iterator<Cell> cellIterator = currentRow.cellIterator();
                while (cellIterator.hasNext()) {
                    currentCell = cellIterator.next();
                    if (!this.fillEntryField(currentCell, e, serialsOnly)) {
                        break;
                    }
                }
                if (e.getSerialNo() != null) {
                    entries.add(e);
                }
            }
            rowCounter++;
        }
    } catch (IOException ex) {
        this.log.log("nieudana prba otwarcia pliku " + file.getAbsolutePath());
        this.log.log(ExceptionUtils.getMessage(ex));
    } catch (ParseException ex) {
        this.log.log("nieprawidowy format daty w komrce " + currentRow.getRowNum()
                + CellReference.convertNumToColString(currentCell.getColumnIndex())
                + ". Akceptowalny format to 'yyyy-mm-dd'");
        this.log.log(ExceptionUtils.getMessage(ex));
    }
    System.gc();
    ProgressHelper.hideProgressBar(progress);
    return entries;
}

From source file:py.gov.datos.XlsToCsvConverter.java

License:GNU General Public License

/**
 * Convierte una hoja de una planilla XLSX a un archivo .csv
 * @param sheet hoja a convertir./*from  w w  w .  j av  a2s.c om*/
 * @return un archivo .csv
 */
private StringBuffer convertSheet(XSSFSheet sheet) {
    StringBuffer data = new StringBuffer();
    Iterator<Row> rowIterator = sheet.iterator();

    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        StringBuffer rowBuffer = convertRow(row);
        data.append(rowBuffer);
    }

    return data;
}

From source file:regression.data.GenerateData.java

/**
 * Read data from excel/*from  w  w w  . jav  a2s.  c  o  m*/
 *
 * @param file
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidFormatException
 */
public void readExcelDataSet(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream excelFile = new FileInputStream(file);
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = firstSheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Cell firstCell = row.getCell(row.getFirstCellNum());
        Cell secondCell = row.getCell(row.getFirstCellNum() + 1);

        if (firstCell.getCellType() == Cell.CELL_TYPE_NUMERIC
                && secondCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            try {
                this.points.add(new Point(firstCell.getNumericCellValue(), secondCell.getNumericCellValue()));
            } catch (Exception e) {
                System.err.println("Cannot convert data in row: " + row.getRowNum());
            }
        }

    }
    this.numberOfInstances = points.size();
}

From source file:regression.data.GenerateData.java

public void createWekaFile(File f) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream excelFile = new FileInputStream(f);
    File wekaFile = new File("weka.arff");
    XSSFWorkbook workbook = new XSSFWorkbook(f);
    XSSFSheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = firstSheet.iterator();
    PrintWriter writer = new PrintWriter(wekaFile);
    writer.println("@RELATION logistic");
    writer.println("@ATTRIBUTE x NUMERIC");
    writer.println("@ATTRIBUTE class {1,0}");
    writer.println("@DATA");
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();//from  w  w  w  .  ja va2 s .  com
        Cell firstCell = row.getCell(row.getFirstCellNum());
        Cell secondCell = row.getCell(row.getFirstCellNum() + 1);

        if (firstCell.getCellType() == Cell.CELL_TYPE_NUMERIC
                && secondCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            try {
                writer.println(firstCell.getNumericCellValue() + "," + (int) secondCell.getNumericCellValue());
            } catch (Exception e) {
                System.err.println("Cannot convert data in row: " + row.getRowNum());
            }
        }

    }
    writer.close();
    this.numberOfInstances = points.size();

}

From source file:regression.data.GenerateLinearData.java

/**
 * Read data from excel/*w w w  .j a v  a  2s  . c  o  m*/
 *
 * @param file
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidFormatException
 */
public void readExcelDataSet(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream excelFile = new FileInputStream(file);
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = firstSheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Cell firstCell = row.getCell(row.getFirstCellNum());
        Cell secondCell = row.getCell(row.getFirstCellNum() + 1);
        System.out.println(firstCell.getCellType() + " " + secondCell.getCellType());
        if (firstCell.getCellType() == Cell.CELL_TYPE_NUMERIC
                && secondCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            try {
                this.points.add(new Point(firstCell.getNumericCellValue(), secondCell.getNumericCellValue()));
            } catch (Exception e) {
                System.err.println("Cannot convert data in row: " + row.getRowNum());
            }
        }

    }

}

From source file:regression.data.GenerateLinearData.java

public Function testDataFromFile(File file, JTextArea output)
        throws FileNotFoundException, IOException, InvalidFormatException {

    FileInputStream excelFile = new FileInputStream(file);
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = firstSheet.iterator();
    List<Point> points = new ArrayList<>();
    Function function = getLastSavedFunction();
    output.append("Dla Funkcji:y=x" + function.getFactor().get(0) + "+" + function.getFreeFactor()
            + " Wartoci testowe przyjmuj" + "\n");
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();/*from   w  ww . j ava 2 s .  co  m*/
        Cell firstCell = row.getCell(row.getFirstCellNum());
        if (firstCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            try {
                Double score = countFunction(function, firstCell.getNumericCellValue());
                this.points.add(new Point(firstCell.getNumericCellValue(), score));
                output.append("Dla x=" + firstCell.getNumericCellValue() + " y=" + score + "\n");
            } catch (Exception e) {
                System.err.println("Cannot convert data in row: " + row.getRowNum());
            }
        }

    }

    return function;
}

From source file:regression.data.GenerateLinearData.java

/**
 * Read data from excel/*from w ww . j  a  v  a2  s.  c  om*/
 *
 * @param file
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidFormatException
 */
public List<Point> getExcelDataSet(File file)
        throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream excelFile = new FileInputStream(file);
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = firstSheet.iterator();
    List<Point> points = new ArrayList<>();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Cell firstCell = row.getCell(row.getFirstCellNum());
        Cell secondCell = row.getCell(row.getFirstCellNum() + 1);
        //    if (secondCell != null) {
        if ((firstCell.getCellType() == Cell.CELL_TYPE_NUMERIC
                && secondCell.getCellType() == Cell.CELL_TYPE_NUMERIC)) {
            try {
                points.add(new Point(firstCell.getNumericCellValue(), secondCell.getNumericCellValue()));
            } catch (Exception e) {
                System.err.println("Cannot convert data in row: " + row.getRowNum());
            }
        }
    } //else {
      // if (firstCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
      //      try {
      //          points.add(new Point(firstCell.getNumericCellValue(), 0.0));
      //       } catch (Exception e) {
      //            System.err.println("Cannot convert data in row: " + row.getRowNum());
      //         }
      //      }
      //   }
      //   }
    return points;

}

From source file:regression.gui.MainWindow.java

void createWekaFile(String pathToNewWekaFile, File fileExcel) {

    try {/*from  ww w .j a v  a 2  s  .c  o  m*/

        File wekaFile = new File(pathToNewWekaFile);
        XSSFWorkbook workbook = new XSSFWorkbook(fileExcel);
        XSSFSheet firstSheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = firstSheet.iterator();
        PrintWriter writer = new PrintWriter(wekaFile);
        writer.println("@RELATION logistic");
        writer.println("@ATTRIBUTE x NUMERIC");
        writer.println("@ATTRIBUTE class {1,0}");
        writer.println("@DATA");
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Cell firstCell = row.getCell(row.getFirstCellNum());
            Cell secondCell = row.getCell(row.getFirstCellNum() + 1);

            if (firstCell.getCellType() == Cell.CELL_TYPE_NUMERIC
                    && secondCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                try {
                    writer.println(
                            firstCell.getNumericCellValue() + "," + (int) secondCell.getNumericCellValue());
                } catch (Exception e) {
                    System.err.println("Cannot convert data in row: " + row.getRowNum());
                }
            }

        }
        writer.close();

    } catch (Exception ex) {
        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:scoretracker.beans.EJB.UploadService.java

public Boolean upload(Part file) throws IOException {
    Boolean upload = true;/*ww w  .  j a  va2 s. com*/
    InputStream inputStream = file.getInputStream();
    Query q;
    em = emf.createEntityManager();
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
    XSSFSheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> iterator = firstSheet.iterator();
    List<Teststudent> studenten = new ArrayList<Teststudent>();
    Test test = new Test();

    //Klas ophalen uit Excel
    Row nextRow = iterator.next();
    q = em.createNamedQuery("Klas.findByName");
    List<Klas> klassen = new ArrayList<>();
    q.setParameter("name", nextRow.getCell(1).getStringCellValue());
    klassen = q.getResultList();

    if (klassen.size() != 1) {
        //vak ophalen uit Excel
        nextRow = iterator.next();
        q = em.createNamedQuery("Course.findByName");
        List<Course> courses = new ArrayList<>();
        q.setParameter("name", nextRow.getCell(1).getStringCellValue());
        courses = q.getResultList();

        if (courses.size() != 1) {
            //Test object aanmaken

            nextRow = iterator.next();
            test.setClassId(klassen.get(0));
            test.setCourseId(courses.get(0));
            test.setName(nextRow.getCell(1).getStringCellValue());
            nextRow = iterator.next();
            Double maxScore = nextRow.getCell(1).getNumericCellValue();
            test.setMaxScore(maxScore.intValue());

            nextRow = firstSheet.getRow(5);
            while (iterator.hasNext()) {
                nextRow = iterator.next();
                List<Student> students = new ArrayList<>();
                q = em.createNamedQuery("Student.findByRNr");
                q.setParameter("rNr", nextRow.getCell(0).getStringCellValue());
                students = q.getResultList();

                if (students.size() != 1) {
                    Student student = students.get(0);
                    Teststudent testStudent = new Teststudent();

                    testStudent.setTestId(test);
                    testStudent.setStudentId(student);
                    Double score = nextRow.getCell(2).getNumericCellValue();
                    testStudent.setScore(score.intValue());

                    studenten.add(testStudent);
                } else {
                    upload = false;
                }
            }
        } else {
            upload = false;
        }
    } else {
        upload = false;
    }

    workbook.close();
    inputStream.close();
    //fouthandeling en toevoegen
    if (upload) {

        em.persist(test);

        for (Teststudent t : studenten) {
            em.persist(t);

        }

    } else {
        upload = false;
    }

    return upload;

}