Example usage for org.apache.poi.ss.usermodel Row cellIterator

List of usage examples for org.apache.poi.ss.usermodel Row cellIterator

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Row cellIterator.

Prototype

Iterator<Cell> cellIterator();

Source Link

Usage

From source file:org.wso2.carbon.dataservices.sql.driver.processor.reader.ExcelDataReader.java

License:Open Source License

public void populateData() throws SQLException {
    Workbook workbook = ((TExcelConnection) getConnection()).getWorkbook();
    int noOfSheets = workbook.getNumberOfSheets();
    for (int i = 0; i < noOfSheets; i++) {
        Sheet sheet = workbook.getSheetAt(i);
        String sheetName = sheet.getSheetName();
        ColumnInfo[] headers = this.extractColumnHeaders(sheet);
        DataTable dataTable = new FixedDataTable(sheetName, headers);

        Iterator<Row> rowItr = sheet.rowIterator();
        while (rowItr.hasNext()) {
            Row row = rowItr.next();
            if (row.getRowNum() != 0) {
                DataRow dataRow = new DataRow(row.getRowNum() - 1);
                Iterator<Cell> cellItr = row.cellIterator();
                int cellIndex = 0;
                while (cellItr.hasNext()) {
                    Cell cell = cellItr.next();
                    DataCell dataCell = new DataCell(cellIndex + 1, cell.getCellType(), extractCellValue(cell));
                    dataRow.addCell(dataCell.getColumnId(), dataCell);
                    cellIndex++;/* w  w w.j  a va2s  .  c  o  m*/
                }
                dataTable.addRow(dataRow);
            }
        }
        this.getData().put(dataTable.getTableName(), dataTable);
    }
}

From source file:org.wso2.carbon.dataservices.sql.driver.processor.reader.ExcelDataReader.java

License:Open Source License

/**
 * Extracts out the columns in the given excel sheet
 *
 * @param sheet Sheet instance corresponding to the desired Excel sheet
 * @return Array containing the column header data
 * @throws java.sql.SQLException SQLException
 *///from   w  ww  . j  a v a2  s .c  om
private ColumnInfo[] extractColumnHeaders(Sheet sheet) throws SQLException {
    List<ColumnInfo> headers = new ArrayList<ColumnInfo>();

    /* If hasHeader property is set to false, populate header map with column names following
     * the format 'COLUMN' + 'i' where i corresponds to the column id */
    if (!((TConnection) getConnection()).hasHeader()) {
        int maxColumns = ((TConnection) getConnection()).getMaxColumns();
        for (int i = 0; i < maxColumns; i++) {
            headers.add(new ColumnInfo(i + 1, Constants.COLUMN + (i + 1), sheet.getSheetName(), -1, i + 1));
        }
        return headers.toArray(new ColumnInfo[headers.size()]);
    }
    // Retrieving the first row of the sheet as the header row.
    Row row = sheet.getRow(0);
    if (row != null) {
        Iterator<Cell> itr = row.cellIterator();
        while (itr.hasNext()) {
            Cell cell = itr.next();
            if (cell != null) {
                int cellType = cell.getCellType();
                switch (cellType) {
                case Cell.CELL_TYPE_STRING:
                    headers.add(new ColumnInfo(cell.getColumnIndex() + 1, cell.getStringCellValue(),
                            sheet.getSheetName(), Types.VARCHAR, cell.getColumnIndex() + 1));
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    headers.add(new ColumnInfo(cell.getColumnIndex() + 1,
                            String.valueOf(cell.getNumericCellValue()), sheet.getSheetName(), Types.INTEGER,
                            cell.getColumnIndex() + 1));
                    break;
                default:
                    throw new SQLException("Invalid column type");
                }
            }
        }
    }
    return headers.toArray(new ColumnInfo[headers.size()]);
}

From source file:personality_prediction.Liwcdict.java

void extract_features() {
    File f = new File("C:\\Users\\divya\\Desktop\\Personality Mining\\LIWC2007.cat");
    LIWCDictionary dict = new LIWCDictionary(f);
    System.out.println("1.. for viewing dictionary");
    System.out.println("2..generating excel file");
    Scanner sc = new Scanner(System.in);
    int choice = sc.nextInt();
    if (choice == 1) {
        dict.Display_Dictionary_Map();
    } else {//from w w  w  .  j a  v  a  2s.  c o m
        Liwcdict obj = new Liwcdict();
        Map<String, Double> counts;
        // String filename="C:\\Users\\somya\\Desktop\\Personality Mining\\WEKA_Dataset\\feature_values.xls" ;
        HSSFWorkbook hwb = new HSSFWorkbook();
        HSSFSheet sheet = hwb.createSheet("new sheet");
        try {
            FileInputStream file = new FileInputStream(new File(
                    "C:\\Users\\divya\\Desktop\\Personality Mining\\mypersonality_final\\mypersonality_final.xls"));
            //Create Workbook instance holding reference to .xlsx file
            HSSFWorkbook workbook = new HSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            HSSFSheet sheet_1 = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet_1.iterator();
            int row_count = 0;
            while (rowIterator.hasNext()) {
                //for(int i=0;i<10;i++){
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();
                Cell cell = cellIterator.next();
                cell = cellIterator.next();
                String tweet = cell.getStringCellValue();

                counts = dict.getJustCounts(tweet, true);
                //counts=dict.getCounts(tweet, true);
                obj.export2excel(counts, row_count, tweet, sheet, hwb);
                System.out.println(tweet);
                //Check the cell type and format accordingly
                //System.out.print(cell.getStringCellValue() + "t");
                row_count++;
            }
            file.close();
        }

        catch (Exception e) {
            e.printStackTrace();
        }

        // LIWCDictionary.tokenize("g hjgjh hgjh");
    }
}

From source file:personality_prediction.Liwcdict.java

void fetch_tweet() {
    try {/*from www . ja  v  a2 s  .  c om*/
        FileInputStream file = new FileInputStream(new File(
                "C:\\Users\\somya\\Desktop\\Personality Mining\\mypersonality_final\\mypersonality_final.xls"));
        //Create Workbook instance holding reference to .xlsx file
        HSSFWorkbook workbook = new HSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        int row_count = 0;
        //while (rowIterator.hasNext())
        for (int i = 0; i < 10; i++) {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            Cell cell = cellIterator.next();
            cell = cellIterator.next();
            String tweet = cell.getStringCellValue();
            System.out.println(tweet);
            //Check the cell type and format accordingly
            //System.out.print(cell.getStringCellValue() + "t");

            row_count++;

            file.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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;
    Cell currentCell = null;//w  w  w.  j a  v  a  2s  .  co m
    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:pl.ulit.xsl.handler.ListyProcedur.java

@Override
public void read() {
    String newKodListy;/*from   ww w.j  a  v  a 2 s.  c  o m*/
    String oldKodListy = "A02";
    ICD9 procedura;
    String kodICD9;
    int ranga;
    String newTyp;
    String oldTyp = "H";
    HashMap<ICD9, Integer> procedury = new HashMap<ICD9, Integer>();
    Cell cell;

    Iterator<Row> rowIterator = sheet.iterator();
    skipRows(rowIterator, 4);

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();

        newKodListy = getKodListy(cellIterator);
        newTyp = getTypListy(cellIterator);
        if (isNewListaProcedur(oldKodListy, newKodListy)) {
            ListaProcedur lp = ListaProcedur.newInstance(oldKodListy, oldTyp, procedury);
            listyProcedur.add(lp);
            oldKodListy = newKodListy;
            oldTyp = newTyp;
            procedury = new HashMap<>();
        }
        cell = cellIterator.next();
        if (isBlankCell(cell)) {
            break;
        }
        kodICD9 = getKodICD9(cell);
        ranga = getRanga(cellIterator);

        String nazwa = getNazwaICD9(cellIterator);
        procedura = ICD9.newInstance(kodICD9, nazwa);
        procedury.put(procedura, ranga);
    }

}

From source file:pl.ulit.xsl.handler.ListyRozpoznan.java

@Override
public void read() {
    String newKodListy;//from   w  w w.j a  v  a 2  s  .c  o  m
    String oldKodListy = "A01";
    ICD10 rozpoznanie;
    String kodICD10;
    int ranga;
    String oldTyp = "H";
    String newTyp;

    ArrayList<ICD10> rozpoznania = new ArrayList<>();
    Cell cell;

    Iterator<Row> rowIterator = sheet.iterator();
    skipRows(rowIterator, 4);

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();

        newKodListy = getKodListy(cellIterator);
        newTyp = getTypListy(cellIterator);
        if (isNewListaProcedur(oldKodListy, newKodListy)) {
            ListaRozpoznan lr = ListaRozpoznan.newInstance(oldKodListy, oldTyp, rozpoznania);
            listyRozpoznan.add(lr);
            oldKodListy = newKodListy;
            oldTyp = newTyp;
            rozpoznania = new ArrayList<>();
        }
        cell = cellIterator.next();
        if (isBlankCell(cell)) {
            break;
        }
        kodICD10 = getKodICD10(cell);

        String nazwa = getNazwaICD10(cellIterator);
        rozpoznanie = ICD10.newInstance(kodICD10, nazwa);
        rozpoznania.add(rozpoznanie);
    }

}

From source file:pl.ulit.xsl.handler.MechanizmOsobodni.java

@Override
public void read() {
    Cell cell;//from  w  ww. j  av  a  2s  .c  om
    String kodJGP;
    String kodProduktu;
    String nazwaJGP;
    int dniPobytuFinansGrupa;
    int hospitalizacja1dnia;
    int ryczalt;
    Iterator<Row> rowIterator = sheet.iterator();
    skipRows(rowIterator, 3);

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        kodJGP = getKodJGP(cellIterator);
        kodProduktu = getKodProduktu(cellIterator);
        nazwaJGP = getNazwaJGP(cellIterator);
        dniPobytuFinansGrupa = getDniPobytuFinansGrupa(cellIterator);
        hospitalizacja1dnia = getHospitalizacja1Dnia(cellIterator);
        ryczalt = getRyczalt(cellIterator);
        setMechanizmOsobodni(kodJGP, kodProduktu, nazwaJGP, dniPobytuFinansGrupa, hospitalizacja1dnia, ryczalt);
    }
}

From source file:pl.ulit.xsl.handler.OgraniczeniePobytuSheet.java

@Override
public void read() {
    int kodOgraniczeniaPobytu;
    int ponizejGornaGranica;
    int pomiedzyDolnaGranica;
    int pomiedzyGornaGranica;
    int powyzejDolnaGranica;
    Iterator<Row> rowIterator = sheet.iterator();
    skipRows(rowIterator, 4);//  ww w  . j a v  a2  s . co m

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        kodOgraniczeniaPobytu = getKodOgraniczeniaWieku(cellIterator);
        ponizejGornaGranica = getPonizejGornaGranica(cellIterator);
        pomiedzyDolnaGranica = getPomiedzyDolnaGranica(cellIterator);
        pomiedzyGornaGranica = getPomiedzyGornaGranica(cellIterator);
        powyzejDolnaGranica = getPomiedzyDolnaGranica(cellIterator);
        listaOgraniczeniaPobytu.add(OgraniczeniePobytuV5.newInstance(kodOgraniczeniaPobytu, ponizejGornaGranica,
                pomiedzyGornaGranica, pomiedzyDolnaGranica, powyzejDolnaGranica));
    }
    /*  for(OgraniczeniePobytu ogr: listaOgraniczeniaPobytu){
          logger.info(ogr.toString());
      }*/
}

From source file:pl.ulit.xsl.handler.OgraniczeniePobytuSheetv6.java

@Override
public void read() {
    int kodOgraniczeniaWieku;
    int ponizejGornaGranica;
    int powyzejDolnaGranica;
    String jednostka;/*from   w  w w . j a va 2s.  c o  m*/
    Iterator<Row> rowIterator = sheet.iterator();
    skipRows(rowIterator, 4);

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        kodOgraniczeniaWieku = getKodOgraniczeniaWieku(cellIterator);
        ponizejGornaGranica = getPonizejGornaGranica(cellIterator);
        powyzejDolnaGranica = getPowyzejDolnaGranica(cellIterator);
        jednostka = getJednostka(cellIterator);

        listaOgraniczeniaWieku.add(OgraniczenieWiekuV6.newInstance(kodOgraniczeniaWieku, jednostka,
                ponizejGornaGranica, powyzejDolnaGranica));
    }
    /* for(OgraniczenieWiekuV6 ograniczenie:listaOgraniczeniaWieku){
         System.out.println(ograniczenie);
     }*/
}