Example usage for org.apache.poi.hssf.usermodel HSSFRow getRowNum

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow getRowNum

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFRow getRowNum.

Prototype

@Override
public int getRowNum() 

Source Link

Document

get row number this row represents

Usage

From source file:gatebass.utils.exel.POIExcelReader.java

@SuppressWarnings("unchecked")
public void displayFromExcel2(String xlsPath) {
    InputStream inputStream = null;

    try {/*from  w  w  w  . j a v  a  2 s .  c  om*/
        inputStream = new FileInputStream(xlsPath);
    } catch (FileNotFoundException e) {
        System.out.println("File not found in the specified path.");
        e.printStackTrace();
    }

    POIFSFileSystem fileSystem = null;
    int dd = 0;

    try {
        fileSystem = new POIFSFileSystem(inputStream);

        HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator rows = sheet.rowIterator();
        List<Cars> carses = new ArrayList<>();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            //                if (row.getRowNum() >= end_row) {
            //                    break;
            //                }

            if (row.getRowNum() <= start_row) {
                continue;
            }

            dd = row.getRowNum();
            //                if (row.getRowNum() == 0
            //                        || row.getRowNum() < 195 || row.getRowNum() > 250
            //                        ) {
            //                    continue;
            //                }
            Cars cars = null;

            // display row number in the console.
            //                System.out.println("Row No.: " + row.getRowNum());
            // once get a row its time to iterate through cells.
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {
                HSSFCell cell = (HSSFCell) cells.next();

                //                    System.out.println("Cell No.: " + cell.getCellNum());

                /*
                 * Now we will get the cell type and display the values
                 * accordingly.
                 */
                switch (cell.getCellNum()) {
                case 0:
                    cars = new Cars();
                    //                            individuals = new Individuals(Integer.parseInt(cell.getRichStringCellValue().getString()));
                    break;
                case 1:
                    try {
                        cars.setCard_id(((long) cell.getNumericCellValue()) + "");
                    } catch (Exception e) {
                    }
                    try {
                        cars.setCard_id(cell.getRichStringCellValue().getString());
                    } catch (Exception e) {
                    }
                    cars.setShasi_number(cars.getCard_id());
                    break;
                //                        case 2:
                //                            if (!cell.getRichStringCellValue().getString().isEmpty()) {
                //                            }
                //                            break;
                case 3:
                    if (!cell.getRichStringCellValue().getString().isEmpty()) {
                        cars.setCar_name(cell.getRichStringCellValue().getString());
                    }
                    break;
                //                        case 4:
                //                            if (!cell.getRichStringCellValue().getString().isEmpty()) {
                //                                cars.setLast_name(cell.getRichStringCellValue().getString());
                //                            }
                //                            break;
                //                        case 5:
                //                            if (!cell.getRichStringCellValue().getString().isEmpty()) {
                //                                cars.set(cell.getRichStringCellValue().getString());
                //                            }
                //                            break;
                case 13:
                    if (!cell.getRichStringCellValue().getString().isEmpty()) {
                        cars.setComments(cell.getRichStringCellValue().getString());
                    }
                    break;
                }

                switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC: {
                    // cell type numeric.
                    //                            System.out.println("Numeric value: " + cell.getNumericCellValue());
                    break;
                }

                case HSSFCell.CELL_TYPE_STRING: {
                    // cell type string.
                    HSSFRichTextString richTextString = cell.getRichStringCellValue();
                    //                            System.out.println("String value: " + richTextString.getString());
                    break;
                }

                default: {
                    // types other than String and Numeric.
                    //                            System.out.println("Type not supported.");
                    break;
                }
                }
            }
            String split = FileSystems.getDefault().getSeparator();

            cars.setFilesPatch(
                    "data" + split + "1394" + split + dd / 50 + split + cars.getShasi_number() + "_c" + split);
            carses.add(cars);
            //                databaseHelper.individualsDao.createOrUpdate(individuals, dd);
        }
        databaseHelper.carDao.insertList(carses);

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

From source file:gatebass.utils.exel.POIExcelReader.java

@SuppressWarnings("unchecked")
public void worksFromExcel2(String xlsPath) {
    InputStream inputStream = null;

    try {/*from w  ww.  j av a 2s .co  m*/
        inputStream = new FileInputStream(xlsPath);
    } catch (FileNotFoundException e) {
        System.out.println("File not found in the specified path.");
        e.printStackTrace();
    }

    POIFSFileSystem fileSystem = null;

    List<CarHistory> historys = new ArrayList<>();
    try {
        fileSystem = new POIFSFileSystem(inputStream);

        HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator rows = sheet.rowIterator();
        boolean check = false;
        while (rows.hasNext()) {
            check = false;
            HSSFRow row = (HSSFRow) rows.next();
            //                if (row.getRowNum() >= end_row) {
            //                    break;
            //                }
            if (row.getRowNum() <= start_row) {
                continue;
            }

            CarHistory carHistory = new CarHistory();

            //                System.out.println("Row No.: " + row.getRowNum());
            String row_value = row.getCell(2).getRichStringCellValue().getString();
            if (!row_value.isEmpty()) {
                Companies companiesTEMP = databaseHelper.companiesDao.getFirst("company_fa", row_value);
                carHistory.setCompanies(companiesTEMP);
            }

            row_value = row.getCell(5).getRichStringCellValue().getString();
            if (!row_value.isEmpty()) {
                carHistory.setPellak(row_value);
            }

            try {
                row_value = row.getCell(6).getRichStringCellValue().getString();
                if (!row_value.isEmpty()) {
                    row_value = row_value.substring(2);
                    History HistoryTEMP = databaseHelper.historyDao.getFirst("date", row_value);
                    carHistory.setCardExpirationDateId(HistoryTEMP);
                }
            } catch (Exception e) {
            }

            try {
                row_value = row.getCell(7).getRichStringCellValue().getString();
                if (!row_value.isEmpty()) {
                    row_value = row_value.substring(2);
                    History HistoryTEMP = databaseHelper.historyDao.getFirst("date", row_value);
                    carHistory.setCardIssuedDateId(HistoryTEMP);
                }
            } catch (Exception e) {
            }

            try {
                row_value = row.getCell(8).getRichStringCellValue().getString();
                if (!row_value.isEmpty()) {
                    row_value = row_value.substring(2);
                    History HistoryTEMP = databaseHelper.historyDao.getFirst("date", row_value);
                    carHistory.setCardIssuedDateId(HistoryTEMP);
                }
            } catch (Exception e) {
            }

            try {
                row_value = row.getCell(9).getRichStringCellValue().getString();
                if (!row_value.isEmpty()) {
                    row_value = row_value.substring(2);
                    History HistoryTEMP = databaseHelper.historyDao.getFirst("date", row_value);
                    carHistory.setCardIssuedDateId(HistoryTEMP);
                }
            } catch (Exception e) {
            }

            try {
                row_value = row.getCell(10).getRichStringCellValue().getString();
                if (!row_value.isEmpty()) {
                    row_value = row_value.substring(2);
                    History HistoryTEMP = databaseHelper.historyDao.getFirst("date", row_value);
                    carHistory.setCardIssuedDateId(HistoryTEMP);
                }
            } catch (Exception e) {
            }

            try {
                row_value = row.getCell(11).getRichStringCellValue().getString();
                if (!row_value.isEmpty()) {
                    row_value = row_value.substring(2);
                    History HistoryTEMP = databaseHelper.historyDao.getFirst("date", row_value);
                    carHistory.setCardIssuedDateId(HistoryTEMP);
                }
            } catch (Exception e) {
            }

            try {
                row_value = row.getCell(12).getRichStringCellValue().getString();
                if (!row_value.isEmpty()) {
                    row_value = row_value.substring(2);
                    History HistoryTEMP = databaseHelper.historyDao.getFirst("date", row_value);
                    carHistory.setCardDeliveryDateId(HistoryTEMP);
                }
            } catch (Exception e) {
            }

            try {
                row_value = ((long) row.getCell(1).getNumericCellValue()) + "";
                if (!row_value.isEmpty()) {
                    Cars cr1 = databaseHelper.carDao.getFirst("card_id", row_value);
                    carHistory.setCar_id(cr1);
                }
            } catch (Exception e) {
            }
            try {
                row_value = row.getCell(1).getRichStringCellValue().getString();
                if (!row_value.isEmpty()) {
                    Cars cr1 = databaseHelper.carDao.getFirst("card_id", row_value);
                    carHistory.setCar_id(cr1);
                }
            } catch (Exception e) {
            }

            historys.add(carHistory);

            //                    databaseHelper.manageDao.createOrUpdate(mm);
        }
        databaseHelper.carsHistoryDao.insertList(historys);
    } catch (Exception e) {
        Logger.getLogger(POIExcelReader.class.getName()).log(Level.SEVERE, e.getMessage(), e);
    }

}

From source file:gatebass.utils.exel.POIExcelReader.java

@SuppressWarnings("unchecked")
public void displayFromExcel3(String xlsPath) {
    InputStream inputStream = null;

    try {//ww w.j  a  v  a 2s  . c  o  m
        inputStream = new FileInputStream(xlsPath);
    } catch (FileNotFoundException e) {
        System.out.println("File not found in the specified path.");
        e.printStackTrace();
    }

    POIFSFileSystem fileSystem = null;
    int dd = 0;

    try {
        fileSystem = new POIFSFileSystem(inputStream);

        HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator rows = sheet.rowIterator();
        List<Individuals> individualses = new ArrayList<>();

        //            Manage manage = databaseHelper.manageDao.getFirst("key", "card_id_count");
        //            long card_sequential = Long.parseLong(manage.getValue());
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            //                if (row.getRowNum() >= end_row) {
            //                    break;
            //                }

            if (row.getRowNum() <= start_row) {
                continue;
            }

            dd = row.getRowNum();
            //                if (row.getRowNum() == 0
            //                        || row.getRowNum() < 195 || row.getRowNum() > 250
            //                        ) {
            //                    continue;
            //                }
            Individuals individuals = null;

            // display row number in the console.
            //                System.out.println("Row No.: " + row.getRowNum());
            // once get a row its time to iterate through cells.
            String meli = "";

            {
                try {
                    meli = (((long) row.getCell(6).getNumericCellValue()) + "");
                } catch (Exception e) {
                }
                try {
                    meli = (row.getCell(6).getRichStringCellValue().getString());
                } catch (Exception e) {
                }
                //                    System.out.println("meli = " + meli);
                individuals = databaseHelper.individualsDao.getFirst("national_id", meli);
                if (individuals == null) {

                    individuals = new Individuals();
                    individuals.setNational_id(meli);

                    //                        try {
                    //                            individuals.setCard_id(((long) row.getCell(0).getNumericCellValue()) + "");
                    //                        } catch (Exception e) {
                    //                        }
                    //                        try {
                    //                            individuals.setCard_id(row.getCell(0).getRichStringCellValue().getString());
                    //                        } catch (Exception e) {
                    //                        }
                    if (!row.getCell(1).getRichStringCellValue().getString().isEmpty()) {
                        individuals.setFirst_name(row.getCell(1).getRichStringCellValue().getString());
                    }
                    try {
                        if (!row.getCell(2).getRichStringCellValue().getString().isEmpty()) {
                            individuals
                                    .setFather_first_name(row.getCell(2).getRichStringCellValue().getString());
                        }
                    } catch (Exception e) {
                    }
                    try {
                        individuals.setId_number(((long) row.getCell(3).getNumericCellValue()) + "");
                    } catch (Exception e) {
                    }
                    try {
                        individuals.setId_number(row.getCell(3).getRichStringCellValue().getString());
                    } catch (Exception e) {
                    }
                    try {
                        if (!row.getCell(4).getRichStringCellValue().getString().isEmpty()) {
                            individuals.setBirth_day(databaseHelper.historyDao.getFirst("date",
                                    row.getCell(4).getRichStringCellValue().getString().substring(2)));
                        }
                    } catch (Exception e) {
                    }
                    try {
                        if (!row.getCell(5).getRichStringCellValue().getString().isEmpty()) {
                            individuals.setBirth_state(row.getCell(5).getRichStringCellValue().getString());
                        }
                    } catch (Exception e) {
                    }
                    try {
                        individuals.setComments("   : "
                                + ((long) row.getCell(0).getNumericCellValue())
                                + (individuals.getComments() != null ? "\n" + individuals.getComments() : ""));
                    } catch (Exception e) {
                    }
                    try {
                        individuals.setComments("   : "
                                + row.getCell(0).getRichStringCellValue().getString()
                                + (individuals.getComments() != null ? "\n" + individuals.getComments() : ""));
                    } catch (Exception e) {
                    }

                    String split = FileSystems.getDefault().getSeparator();
                    individuals.setFilesPatch("data" + split + "1394" + split + dd / 50 + split
                            + individuals.getNational_id() + split);
                    //                        individuals.setCard_id(card_sequential + "");
                    //                        card_sequential++;

                    individualses.add(individuals);
                }

            }
            //                databaseHelper.individualsDao.createOrUpdate(individuals, dd);
        }

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

From source file:gatebass.utils.exel.POIExcelReader.java

@SuppressWarnings("unchecked")
public void displayFromExcel35(String xlsPath) {
    InputStream inputStream = null;

    try {/*from   w  ww  . j av  a 2 s .c  om*/
        inputStream = new FileInputStream(xlsPath);
    } catch (FileNotFoundException e) {
        System.out.println("File not found in the specified path.");
        e.printStackTrace();
    }

    POIFSFileSystem fileSystem = null;
    int dd = 0;

    try {
        fileSystem = new POIFSFileSystem(inputStream);

        HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator rows = sheet.rowIterator();
        List<WorkHistory> whs = new ArrayList<>();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            //                if (row.getRowNum() >= end_row) {
            //                    break;
            //                }

            if (row.getRowNum() <= start_row) {
                continue;
            }

            dd = row.getRowNum();
            //                if (row.getRowNum() == 0
            //                        || row.getRowNum() < 195 || row.getRowNum() > 250
            //                        ) {
            //                    continue;
            //                }
            Individuals individuals = null;

            // display row number in the console.
            //                System.out.println("Row No.: " + row.getRowNum());
            // once get a row its time to iterate through cells.
            String meli = "";

            {
                try {
                    meli = (((long) row.getCell(6).getNumericCellValue()) + "");
                } catch (Exception e) {
                }
                try {
                    meli = (row.getCell(6).getRichStringCellValue().getString());
                } catch (Exception e) {
                }

                //                    System.out.println("meli = " + meli);
                individuals = databaseHelper.individualsDao.getFirst("national_id", meli);
            }
            whs.add(works_add(row, individuals));
        }
        databaseHelper.workHistoryDao.insertList(whs);

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

From source file:gatebass.utils.exel.POIExcelReader.java

@SuppressWarnings("unchecked")
public void replica_FromExcel(String xlsPath) {
    InputStream inputStream = null;

    try {/*from w ww  . j  a va  2  s  . c om*/
        inputStream = new FileInputStream(xlsPath);
    } catch (FileNotFoundException e) {
        System.out.println("File not found in the specified path.");
        e.printStackTrace();
    }

    POIFSFileSystem fileSystem = null;

    try {
        fileSystem = new POIFSFileSystem(inputStream);

        HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator rows = sheet.rowIterator();
        List<IndividualReplica> individualReplicas = new ArrayList<>();

        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            //                if (row.getRowNum() >= end_row) {
            //                    break;
            //                }

            if (row.getRowNum() <= start_row) {
                continue;
            }

            //                if (row.getRowNum() == 0
            //                        || row.getRowNum() < 195 || row.getRowNum() > 250
            //                        ) {
            //                    continue;
            //                }
            IndividualReplica individualReplica = new IndividualReplica();

            // display row number in the console.
            //                System.out.println("Row No.: " + row.getRowNum());
            // once get a row its time to iterate through cells.
            String meli = "";
            try {
                meli = (((long) row.getCell(3).getNumericCellValue()) + "");
            } catch (Exception e) {
            }
            try {
                meli = (row.getCell(3).getRichStringCellValue().getString());
            } catch (Exception e) {
            }
            Individuals individuals = databaseHelper.individualsDao.getFirst("card_id", meli);
            if (individuals != null) {
                //                    individualReplica.setIndividual_id(individuals);

                try {
                    if (!row.getCell(1).getRichStringCellValue().getString().isEmpty()) {
                        individualReplica.setHistory_id(databaseHelper.historyDao.getFirst("date",
                                row.getCell(1).getRichStringCellValue().getString().substring(2)));
                    }
                } catch (Exception e) {
                }

                try {
                    individualReplica.setMablagh(((long) row.getCell(5).getNumericCellValue()) + "");
                } catch (Exception e) {
                }
                try {
                    individualReplica.setMablagh(row.getCell(5).getRichStringCellValue().getString());
                } catch (Exception e) {
                }

                try {
                    individualReplica.setDescription(((long) row.getCell(6).getNumericCellValue()) + "");
                } catch (Exception e) {
                }
                try {
                    individualReplica.setDescription(row.getCell(6).getRichStringCellValue().getString());
                } catch (Exception e) {
                }
                individualReplicas.add(individualReplica);
            } else {
                System.out.println("replica ID : " + meli);
            }
        }
        databaseHelper.individualReplicaDao.insertList(individualReplicas);

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

From source file:guineu.data.parser.impl.GCGCParserXLS.java

License:Open Source License

/**
 *
 * @param sheet/* w w  w . java  2  s . co m*/
 * @return number of row which it starts to read the excel file.
 */
public int getRowInit(HSSFSheet sheet) {

    Iterator rowIt = sheet.rowIterator();
    int num = -1;

    while (rowIt.hasNext()) {
        HSSFRow row = (HSSFRow) rowIt.next();
        HSSFCell cell = row.getCell(0);

        if (cell != null) {
            for (GCGCColumnName field : GCGCColumnName.values()) {
                if (cell.toString().matches(field.getRegularExpression())) {
                    num = row.getRowNum();
                    break;
                }
            }
        }
    }

    return num;
}

From source file:guineu.data.parser.impl.LCMSParserXLS.java

License:Open Source License

/**
 *
 * @param sheet/*  w  ww  .j  av  a2  s  . com*/
 * @return number of row which it starts to read the excel file.
 */
public int getRowInit(HSSFSheet sheet) {

    Iterator rowIt = sheet.rowIterator();
    int num = -1;

    while (rowIt.hasNext()) {
        HSSFRow row = (HSSFRow) rowIt.next();
        HSSFCell cell = row.getCell((short) 0);

        if (cell != null) {
            for (LCMSColumnName field : LCMSColumnName.values()) {
                if (cell.toString().matches(field.getRegularExpression())) {
                    num = row.getRowNum();
                    break;
                }
            }
        }
    }

    return num;
}

From source file:guineu.data.parser.impl.ParserXLS.java

License:Open Source License

public int getNumberRows(int init, HSSFSheet sheet) {
    Iterator rowIt = sheet.rowIterator();
    int num = 0;/*from w w  w  .ja va 2  s. com*/

    while (rowIt.hasNext()) {
        HSSFRow row = (HSSFRow) rowIt.next();
        HSSFCell cell;
        cell = row.getCell(0);
        if ((cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) && row.getRowNum() > init) {
            break;
        }
        num = row.getRowNum();
    }
    return num - init;
}

From source file:guineu.data.parser.impl.ParserXLS.java

License:Open Source License

public int v_type(HSSFWorkbook wb, HSSFRow row, HSSFCell cell) {
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_BLANK:
        System.out.println(" Error - Row: " + row.getRowNum() + " Cell: " + cell.getColumnIndex()
                + "  - Cell type Blank  - " + cell.toString());
        return 0;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        System.out.println(" Error - Row: " + row.getRowNum() + " Cell: " + cell.getColumnIndex()
                + "  - Cell type boolean  - " + cell.toString());
        return 0;
    case HSSFCell.CELL_TYPE_FORMULA:
        System.out.println(" Error - Row: " + row.getRowNum() + " Cell: " + cell.getColumnIndex()
                + "  - Cell type formula  - " + cell.toString());
        return 1;
    case HSSFCell.CELL_TYPE_NUMERIC:
        HSSFCellStyle style = cell.getCellStyle();
        HSSFFont font = wb.getFontAt(style.getFontIndex());
        if (font.getColor() == (new HSSFColor.RED().getIndex())) {
            return 0;
        }/* w w w. j  a v a 2s. c o  m*/
        return 1;
    case HSSFCell.CELL_TYPE_STRING:
        style = cell.getCellStyle();
        font = wb.getFontAt(style.getFontIndex());
        if (font.getColor() == (new HSSFColor.RED().getIndex())) {
            return 0;
        }
        return 2;
    default:
        return 0;
    }
}

From source file:Import.Utils.XSSFConvert.java

/**
 * @param srcSheet the sheet to copy.//from www  . j a v a 2s.  c  o  m
 * @param destSheet the sheet to create.
 * @param srcRow the row to copy.
 * @param destRow the row to create.
 * @param styleMap -
 */
public static void copyRow(HSSFSheet srcSheet, XSSFSheet destSheet, HSSFRow srcRow, XSSFRow destRow,
        Map<Integer, HSSFCellStyle> styleMap) {
    // manage a list of merged zone in order to not insert two times a
    // merged zone
    Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>();
    destRow.setHeight(srcRow.getHeight());
    // pour chaque row
    for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
        HSSFCell oldCell = srcRow.getCell(j); // ancienne cell
        XSSFCell newCell = destRow.getCell(j); // new cell
        if (oldCell != null) {
            if (newCell == null) {
                newCell = destRow.createCell(j);
            }
            // copy chaque cell
            copyCell(oldCell, newCell, styleMap);
            // copy les informations de fusion entre les cellules
            // System.out.println("row num: " + srcRow.getRowNum() +
            // " , col: " + (short)oldCell.getColumnIndex());
            CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(),
                    (short) oldCell.getColumnIndex());

            if (mergedRegion != null) {
                // System.out.println("Selected merged region: " +
                // mergedRegion.toString());
                CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(),
                        mergedRegion.getLastRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastColumn());
                // System.out.println("New merged region: " +
                // newMergedRegion.toString());
                CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion);
                if (isNewMergedRegion(wrapper, mergedRegions)) {
                    mergedRegions.add(wrapper);
                    destSheet.addMergedRegion(wrapper.range);
                }
            }
        }
    }

}