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

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

Introduction

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

Prototype

@Override
public XSSFRow getRow(int rownum) 

Source Link

Document

Returns the logical row ( 0-based).

Usage

From source file:fyp.POI.POIFunction.java

public double poiToGetDbl(String FileAddress, String CellDetails, int SheetNum) throws NullPointerException {
    double result = 0.0;
    try {//from w w  w .j a  v  a2  s  .  c om
        FileInputStream fis = new FileInputStream(new File(FileAddress));
        XSSFWorkbook wb = null;
        try {
            wb = new XSSFWorkbook(fis);
        } catch (IOException e) {
            System.out.print("Error detected: " + e);
        }
        XSSFSheet sheet = wb.getSheetAt(SheetNum);
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
        try {
            CellReference cr = new CellReference(CellDetails);
            Row row = sheet.getRow(cr.getRow());
            Cell cell = row.getCell(cr.getCol());
            //System.out.println("CELL "+ cell);
            checkNullNumeric(cell);
            CellValue cellValue = evaluator.evaluate(cell);

            switch (cellValue.getCellType()) {
            case Cell.CELL_TYPE_BLANK:
                result = 0.0;
                break;

            case Cell.CELL_TYPE_ERROR:
                result = 0.0;
                break;

            case Cell.CELL_TYPE_NUMERIC:
                result = cellValue.getNumberValue();
                break;

            }
        } catch (NullPointerException n) {
            result = 0.0;
        }
        fis.close();
    } catch (IOException e) {
        System.out.print("Error detected: " + e);
    }
    return result;
}

From source file:fyp.POI.POIFunction.java

public double poiToGetInt(String FileAddress, String CellDetails, int SheetNum) throws FileNotFoundException {
    double result = 0.0;

    try {//from ww  w. j a  v a 2  s  . co m
        FileInputStream fis = new FileInputStream(new File(FileAddress));
        XSSFWorkbook wb = null;
        try {
            wb = new XSSFWorkbook(fis);
        } catch (IOException e) {
            System.out.print("Error detected: " + e);
        }
        XSSFSheet sheet = wb.getSheetAt(SheetNum);
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

        try {
            CellReference cr = new CellReference(CellDetails);
            Row row = sheet.getRow(cr.getRow());
            Cell cell = row.getCell(cr.getCol());
            checkNullNumeric(cell);
            CellValue cellValue = evaluator.evaluate(cell);
            switch (cellValue.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                result = cellValue.getNumberValue();
                break;

            case Cell.CELL_TYPE_BLANK:
                result = 0;
                break;

            case Cell.CELL_TYPE_ERROR:
                result = 0;
                break;
            }
        } catch (NullPointerException n) {
            result = 0;
        }

        fis.close();
    } catch (IOException e) {
        System.out.print("Error detected: " + e);
    }
    return (int) result;
}

From source file:fyp.POI.POIFunction.java

public String poiToGetString(String FileAddress, String CellDetails, int SheetNum)
        throws FileNotFoundException {
    String result = "NOTFOUND";
    try {/*from  www  .ja v  a 2  s .  c  om*/
        FileInputStream fis = new FileInputStream(new File(FileAddress));
        XSSFWorkbook wb = null;
        try {
            wb = new XSSFWorkbook(fis);
        } catch (IOException e) {
            System.out.print("Error detected: " + e);
        }
        XSSFSheet sheet = wb.getSheetAt(SheetNum);
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
        try {
            CellReference cr = new CellReference(CellDetails);
            Row row = sheet.getRow(cr.getRow());
            Cell cell = row.getCell(cr.getCol());
            checkNullString(cell);
            CellValue cellValue = evaluator.evaluate(cell);

            switch (cellValue.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                result = cellValue.getStringValue();
                break;

            case Cell.CELL_TYPE_BLANK:
                result = " ";
                break;

            case Cell.CELL_TYPE_ERROR:
                result = " ";
                break;
            }
        } catch (NullPointerException n) {
            result = " ";
        }
        fis.close();
    } catch (IOException e) {
        System.out.print("Error detected: " + e);
    }
    return result;
}

From source file:gov.anl.cue.arcane.engine.Util.java

License:Open Source License

/**
 * Gets the spreadsheet string./*from   w  ww.j av  a2s. c  om*/
 *
 * @param sheet the sheet
 * @param rowIndex the row index
 * @param columnIndex the column index
 * @return the spreadsheet string
 */
public static String getSpreadsheetString(XSSFSheet sheet, int rowIndex, int columnIndex) {

    // Prepare the results storage.
    String cellContents = "";

    // Get the next value.
    Cell cell = sheet.getRow(rowIndex).getCell(columnIndex, Row.RETURN_BLANK_AS_NULL);

    // Check the next value.
    if (cell != null) {

        // Convert the next value, as needed.
        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            cellContents = cell.getStringCellValue();
        } else {
            cellContents = "" + cell.getNumericCellValue();
        }

    }

    // Return the results.
    return cellContents;

}

From source file:gov.anl.cue.arcane.engine.Util.java

License:Open Source License

/**
 * Gets the spreadsheet number.//from   w  ww  .j a  va2s  .  c om
 *
 * @param sheet the sheet
 * @param rowIndex the row index
 * @param columnIndex the column index
 * @return the spreadsheet number
 */
public static double getSpreadsheetNumber(XSSFSheet sheet, int rowIndex, int columnIndex) {

    // Prepare the results storage.
    Double cellContents = Double.NaN;

    // Get the next value.
    Cell cell = sheet.getRow(rowIndex).getCell(columnIndex, Row.RETURN_BLANK_AS_NULL);

    // Check the next value.
    if (cell != null) {

        // Convert the next value, as needed.
        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            cellContents = Double.parseDouble(cell.getStringCellValue());
        } else {
            cellContents = cell.getNumericCellValue();
        }

    }

    // Return the results.
    return cellContents;

}

From source file:icalendarconverter.ExcelConverter.java

public List<ScheduleClass> readExcel() throws FileNotFoundException, IOException {
    ArrayList<ScheduleClass> scheduleList = new ArrayList<>();

    FileInputStream fis = new FileInputStream(pathFile);

    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sheet = wb.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();

    CellRangeAddress add;//from   ww w  .j a v  a 2s.c  om
    int colNoIdx = 0;
    ArrayList<String> dosen = new ArrayList<>();
    ArrayList<Integer> idxDosen = new ArrayList<>();
    ArrayList<Integer> colDosen = new ArrayList<>();
    ArrayList<String> location = new ArrayList<>();
    int idxNumber = 0;
    ArrayList<Integer> locationIdx = new ArrayList<>();
    outerloop: for (int j = 0; j < sheet.getLastRowNum(); j++) {
        row = sheet.getRow(j);
        for (int f = 0; f < row.getLastCellNum(); f++) {
            Cell cell = row.getCell(f);
            if (cell.getCellType() == Cell.CELL_TYPE_STRING
                    && cell.getStringCellValue().equalsIgnoreCase("No.")) {
                rowNoIdx = j;
                colNoIdx = cell.getColumnIndex();
            } else if (cell.getCellType() == Cell.CELL_TYPE_STRING
                    && cell.getStringCellValue().equalsIgnoreCase("Nama Mata Kuliah")) {
                colMatkulIdx = cell.getColumnIndex();
                break outerloop;
            }

        }
    }
    //System.out.println("col matkul = "+colMatkulIdx);
    System.out.println("sheet = " + sheet.getLastRowNum());
    outerloop2: for (int i = 0; i < sheet.getLastRowNum(); i++) {
        outerloop: for (int j = 0; j < row.getLastCellNum(); j++) {
            row = sheet.getRow(i);
            if (row == null) {
                i = sheet.getLastRowNum();
                break outerloop2;
            }
            Cell cell = row.getCell(j);
            FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
            if (cell.getColumnIndex() == colNoIdx && i > rowNoIdx + 3
                    && cell.getCellType() != Cell.CELL_TYPE_BLANK
                    && evaluator.evaluate(cell).getCellType() != Cell.CELL_TYPE_NUMERIC) {
                i = sheet.getLastRowNum();
                break outerloop2;
            }
            if (cell.getColumnIndex() == colNoIdx && i > rowNoIdx + 3
                    && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                i = i + 1;
                break outerloop;
            }

            if (cell.getRowIndex() > rowNoIdx + 1 && cell.getColumnIndex() == (colNoIdx + 1)) {
                if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                    i = i + 1;
                    break outerloop;
                }
                String delims = "[,. ]";
                String[] sumary = cell.getStringCellValue().split(delims);
                for (int l = 0; l < sumary.length; l++) {
                    if (sumary[l].equalsIgnoreCase("Mrt")) {
                        sumary[l] = "3";
                    }
                    if (sumary[l].equalsIgnoreCase("Okt")) {
                        sumary[l] = "10";
                    }
                    if (sumary[l].equalsIgnoreCase("`16")) {
                        sumary[l] = "2016";
                    }
                }

                lc = LocalDate.of(Integer.parseInt(sumary[5]), Integer.parseInt(sumary[3]),
                        Integer.parseInt(sumary[2]));
                // System.out.println("LC = "+lc);

                //                        sp = new SimpleDateFormat("EEEE, MMMM d, yyyy");
                //                        String b = sumary[3] + "/" + sumary[2] + "/" + sumary[5];
                //                        date = new Date(b);
                //System.out.println(sp.format(date));
            }
            if (cell.getRowIndex() > rowNoIdx + 1 && cell.getColumnIndex() == (colNoIdx + 2)) {
                if (cell.getStringCellValue().equalsIgnoreCase("LIBUR")) {
                    i = i + 1;
                    break outerloop;
                } else {
                    if (cell.getStringCellValue().equalsIgnoreCase("Shift 1")
                            || cell.getStringCellValue().equalsIgnoreCase("Shift 2")) {
                        CellReference cr = new CellReference(cell.getRowIndex() + 1, cell.getColumnIndex());
                        Row row2 = sheet.getRow(cr.getRow());
                        Cell c = row2.getCell(cr.getCol());
                        String delimsJam = "[-]";
                        String[] arrJam = c.getStringCellValue().split(delimsJam);
                        for (int k = 0; k < arrJam.length; k++) {
                            arrJam[k] = arrJam[k].replace('.', ':');
                        }
                        //                                indoFormatter = DateTimeFormatter
                        //                                        .ofLocalizedTime(FormatStyle.SHORT)
                        //                                        .withLocale(Locale.getDefault());
                        //System.out.println("I3 = " + i);
                        lt = LocalTime.parse(arrJam[0]);
                        //System.out.println(lt+"-"+lt.plusHours(2)); 

                    } else {
                        String delimsJam = "[-]";
                        String[] arrJam = cell.getStringCellValue().split(delimsJam);
                        for (int k = 0; k < arrJam.length; k++) {
                            arrJam[k] = arrJam[k].replace('.', ':');
                        }
                        //                                indoFormatter = DateTimeFormatter
                        //                                        .ofLocalizedTime(FormatStyle.SHORT)
                        //                                        .withLocale(Locale.getDefault());
                        //System.out.println("I3 = " + i);
                        lt = LocalTime.parse(arrJam[0]);
                        //System.out.println(lt+"-"+lt.plusHours(2)); 
                    }

                }

            }
            if (cell.getRowIndex() > rowNoIdx + 1 && cell.getColumnIndex() == colMatkulIdx) {
                subject = cell.getStringCellValue();
                //System.out.println("Subject = "+subject);
            }

            if (cell.getRowIndex() > rowNoIdx && cell.getColumnIndex() >= colMatkulIdx + 1
                    && cell.getColumnIndex() < row.getLastCellNum()) {
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    //                        location.add(String.valueOf((int)cell.getNumericCellValue()));
                    //                        locationIdx.add(cell.getColumnIndex());
                }
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    if (cell.getStringCellValue().contains(":")) {
                        String[] splt = cell.getStringCellValue().split(":");
                        String[] splt2 = splt[1].split(",");
                        for (int l = 0; l < splt2.length; l++) {
                            dosen.add(splt2[l].trim());
                            location.add("Lab");
                            //System.out.println(splt2[l] + "= lab");
                        }
                    } else {
                        CellReference cr = new CellReference(1, cell.getColumnIndex());
                        Row row2 = sheet.getRow(cr.getRow());
                        Cell c = row2.getCell(cr.getCol());
                        if (!cell.getStringCellValue().isEmpty()) {
                            dosen.add(cell.getStringCellValue().trim());
                            location.add(String.valueOf((int) c.getNumericCellValue()).trim());
                        }

                        //System.out.print(cell.getStringCellValue() + " Ruang =" + (int) c.getNumericCellValue() + " ");
                    }

                }
                if (cell.getCellType() == Cell.CELL_TYPE_BLANK && cell.getRowIndex() > 2) {
                    CellReference cr = new CellReference(cell.getRowIndex() - 1, cell.getColumnIndex());
                    Row row2 = sheet.getRow(cr.getRow());
                    Cell c = row2.getCell(cr.getCol());
                    CellReference cr2 = new CellReference(1, cell.getColumnIndex());
                    Row row3 = sheet.getRow(cr2.getRow());
                    Cell c2 = row3.getCell(cr2.getCol());
                    if (c.getStringCellValue().contains(":")) {
                        String[] splt = c.getStringCellValue().split(":");
                        String[] splt2 = splt[1].split(",");
                        for (int l = 0; l < splt2.length; l++) {
                            dosen.add("".trim());
                            location.add("");
                            //System.out.println(splt2[l] + "= lab");
                        }
                    } else {
                        if (!c.getStringCellValue().isEmpty()) {
                            dosen.add("");
                            location.add("");
                        }

                        //System.out.print(c.getStringCellValue() + " Ruang = " + (int) c2.getNumericCellValue() + " ");
                    }
                }
                //                   scheduleList.add(new ScheduleClass(lc, lt, lt, subject, dosen.get(j), location.get(j)));
            }
            //                System.out.println("lc = "+lc+",lt = "+lt+",subject = "+subject+",dosen = "+dosen.get(i)+",location = "+location.get(i));
            //                scheduleList.add(new ScheduleClass(lc, lt, lt, subject, dosen.get(j), location.get(j)));

        }

        for (int j = 0; j < dosen.size(); j++) {
            //System.out.println("lc = "+lc+",lt = "+lt+",subject = "+subject+",dosen = "+dosen.get(j)+",location = "+location.get(j));
            scheduleList
                    .add(new ScheduleClass(lc, lt, lt.plusHours(2), subject, dosen.get(j), location.get(j)));
        }
        dosen.clear();
        location.clear();

    }

    return mergeringList(scheduleList);
}

From source file:Import.ImportCapital.java

@Override
public int getSheetRowCount(XSSFSheet sheet) {
    int lastRowNum = sheet.getLastRowNum();
    lastRowNum++;//  ww  w.ja  va 2s .co  m
    for (int i = START_ROW_CAPITAL; i < lastRowNum; i++) {
        XSSFRow row = sheet.getRow(i);
        XSSFCell cell1 = row.getCell(1);
        XSSFCell cell2 = row.getCell(2);
        XSSFCell cell3 = row.getCell(3);
        XSSFCell cell4 = row.getCell(4);

        if ((cell1 == null || getStringCellValueNoSetError(cell1) == null)
                && (cell2 == null || getStringCellValueNoSetError(cell2) == null)
                && (cell3 == null || getStringCellValueNoSetError(cell3) == null)
                && (cell4 == null || getStringCellValueNoSetError(cell4) == null)) {
            return i - START_ROW_CAPITAL;
        }
    }
    return lastRowNum - START_ROW_CAPITAL;
}

From source file:Import.ImportCapital.java

@Override
protected void threadProcess(XSSFSheet sheet, int index, int lastRowNum) {
    validationError = new StringBuffer();

    XSSFRow row = sheet.getRow(index);

    int rowCount = index + 1;
    // A//from  ww w.j ava 2 s  .  c o m
    //XSSFCell cell0 = row.getCell(0);

    // B                         
    XSSFCell cell1 = row.getCell(1);
    String depaertmentCode = this.getStringCellValueSetError(cell1, 1);

    Integer departmentId;
    Integer businessUnitId;
    if (!isBlankOrNull(depaertmentCode)) {
        if (TestGUI.IsEnableLog()) {
            logln("? " + rowCount);
        }
        departmentId = cacheDao.findDepartIdmentByFullCodeCache(depaertmentCode);
        businessUnitId = departmentId;
    } else {
        departmentId = 0;
        businessUnitId = 0;
        //            validationError.append("  ").append(mapStringValidation.get(1)).append(" [ ").append(depaertmentCode).append(" ] ");
        //            if(index == START_ROW_CAPITAL){
        //                validationErrorList.add(new StringBuffer(""));
        //                logln("");
        //            }
        //            logln("? " + (index-5) + " ?");
        //            return;
    }

    //? C
    XSSFCell cell2 = row.getCell(2);
    String fundCode = this.getStringCellValueSetError(cell2, 2);
    FiFund fundDao = null;
    if (!isBlankOrNull(fundCode)) {
        fundDao = cacheDao.findFundByFundCode(fundCode, departmentId);
        if (fundDao == null || isNullOrZero(fundDao.getFundId())) {
            validationError.append(" [ ").append(mapStringValidation.get(2)).append(fundCode)
                    .append("  ").append(depaertmentCode).append(" ] ");
        }
    }

    // D
    XSSFCell cell3 = row.getCell(3);
    String capitalCode = this.getStringCellValueSetError(cell3, 3);

    // E
    XSSFCell cell4 = row.getCell(4);
    String capitalName = this.getStringCellValueSetError(cell4, 4);

    //? F
    XSSFCell cell5 = row.getCell(5);
    String interestType = this.getStringCellValueNoSetError(cell5);
    interestType = (!isBlankOrNull(interestType) && INTERREST_STRING.equals(interestType)) ? STRING_C
            : STRING_I;

    // G
    XSSFCell cell6 = row.getCell(6);
    String capitalDesc = this.getStringCellValueNoSetError(cell6);

    // H
    XSSFCell cell7 = row.getCell(7);
    Date setupDate = this.getDateCellValueNoSetError(cell7);

    // I
    XSSFCell cell8 = row.getCell(8);
    String note = this.getStringCellValueNoSetError(cell8);

    //? J            
    XSSFCell cell9 = row.getCell(9);
    String accountCode = this.getStringCellValueSetError(cell9, 9);
    Integer accountId = null;
    if (!isBlankOrNull(accountCode)) {
        accountId = cacheDao.getAccountIdByFullCodeCache(accountCode);
        if (isNullOrZero(accountId)) {
            validationError.append(" [ ").append(mapStringValidation.get(9)).append(accountCode)
                    .append(" ?").append(" ] ");
        }
    }

    //? K
    XSSFCell cell10 = row.getCell(10);
    String sourceCode = this.getStringCellValueSetError(cell10, 10);
    Integer sourceId = null;
    if (!isBlankOrNull(sourceCode)) {
        sourceId = cacheDao.getSourceIdByFullCodeCache(sourceCode);
        if (isNullOrZero(sourceId)) {
            validationError.append(" [ ").append(mapStringValidation.get(10)).append(sourceCode)
                    .append(" ?").append(" ] ");
        }
    }

    //? L
    XSSFCell cell11 = row.getCell(11);
    String planCode = this.getStringCellValueSetError(cell11, 11);
    Integer planId = null;
    if (!isBlankOrNull(planCode)) {
        planId = cacheDao.findPlanIdmentByFullCodeCache(planCode);
        if (isNullOrZero(planId)) {
            validationError.append(" [ ").append(mapStringValidation.get(11)).append(planCode)
                    .append(" ?").append(" ] ");
        }
    }

    // M
    XSSFCell cell12 = row.getCell(12);
    BigDecimal balance = this.getBigDecimalCellValueNoSetError(cell12);

    //? N
    XSSFCell cell13 = row.getCell(13);
    String firstName = this.getStringCellValueNoSetError(cell13);

    //? O
    XSSFCell cell14 = row.getCell(14);
    String address = this.getStringCellValueNoSetError(cell14);

    if (validationError.length() > 0) {
        // error ? row ? commit
        this.commit = false;
        if (TestGUI.IsEnableLogError()) {
            if (!TestGUI.IsEnableLog()) {
                logln("? " + rowCount);
            }
            log.append("   Error : ");
            log.append(validationError.toString());
            log.append("\n");
        }

        validationError.insert(0, " ").insert(0, rowCount).insert(0, "ROW ");
        validationErrorList.add(validationError);

    } else {
        //?
        if (this.commit) {

            FiCapital capital = createFiCapital(departmentId, fundDao, capitalCode, capitalName, interestType,
                    capitalDesc, setupDate, note, accountId, sourceId, planId, balance);

            // spec  @N  person ? capitalFounder
            if (!isBlankOrNull(firstName)) {
                ShPerson person = createShPerson(firstName, address, businessUnitId);

                createFiCapitalFounder(capital.getCapitalId(), person.getPersonId());
            }
        }
        if (TestGUI.IsEnableLog()) {
            log.append("      ");
            log.append("\n");
        }

    }
}

From source file:Import.ImportDonation.java

@Override
protected void threadProcess(XSSFSheet sheet, int index, int lastRowNum) {
    validationError = new StringBuffer();
    validationWarn = new StringBuffer();
    //        List<Object> itemList = new ArrayList<>();
    XSSFRow row = sheet.getRow(index);

    this.rowCount = index + 1;

    XSSFCell cell0 = row.getCell(0);// w ww .  jav a 2s  .co m
    logln("? " + rowCount);
    try {
        ReceiveType receiveType = ReceiveType.valueOf(getStringCellValueNoSetError(cell0));
        mode = receiveType.getType();
        if (isBlankOrNull(mode)) {
            throw new NullPointerException();
        }
    } catch (NullPointerException iEx) {
        validationError.append(
                "  [] ()");
        setError();
        return;
    } catch (IllegalArgumentException iEx) {
        validationError.append(
                " []  ()");
        setError();
        return;
    }

    XSSFCell cell86 = row.getCell(86);
    DEPARTMENT_CODE = getStringFormatNoSetError(cell86, "##");
    try {
        if (DEPARTMENT_CODE.length() > 2) {
            validationError.append(
                    " ? 2 ? ");
            throw new NullPointerException();
        }
    } catch (NullPointerException nullEx) {
        setError(86);
        //            setError();
        //            return;
    }

    ShPerson person = addPerson(row);

    addPersonDoc(row, person);
    addAddress(row, person);
    switch (mode) {
    case STRING_D: {
        AddDoanteMember(row, person);
        break;
    }
    case STRING_C:
        ;
    case STRING_B: {
        AcDonateMember donateMember = AddDoanteMember(row, person);
        AddDoanteMemberAccount(row, person, donateMember);
        break;
    }
    case STRING_M: {
        ShMember member = AddMember(row, person);
        AddMemberCard(row, person, member);
        break;
    }
    default:
        ;
    }
    this.setError();
}

From source file:Import.ImportDonation.java

@Override
protected int getSheetRowCount(XSSFSheet sheet) {
    int lastRowNum = sheet.getLastRowNum();
    lastRowNum++;/*from ww w  .j a  va  2  s . co  m*/
    int count = 0;
    for (int i = START_ROW_DONAION; i < lastRowNum; i++) {
        XSSFRow row = sheet.getRow(i);
        XSSFCell cell0 = row.getCell(0);
        XSSFCell cell3 = row.getCell(3);
        if ((cell0 == null || getStringCellValueNoSetError(cell0) == null)
                && (cell3 == null || getStringCellValueNoSetError(cell3) == null)) {
            return count;
        }
        count++;
    }
    return lastRowNum - START_ROW_DONAION;
}