Example usage for org.apache.poi.hssf.usermodel HSSFSheet getRow

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet getRow

Introduction

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

Prototype

@Override
public HSSFRow getRow(int rowIndex) 

Source Link

Document

Returns the logical row (not physical) 0-based.

Usage

From source file:com.dtrules.compiler.excel.util.ImportRuleSets.java

License:Apache License

/**
 * Looks for the value in some column, and returns that index.  This way we can be a bit more 
 * flexible in our format of the EDD./*w  w w  .j a va  2  s .  c  o m*/
 * @param value
 * @param sheet
 * @param row
 * @return  the Index of the value, or -1 if not found.
 */
private int findvalue(String value, HSSFSheet sheet, int row) {
    HSSFRow theRow = sheet.getRow(row);
    if (theRow == null)
        return -1;
    for (int i = 0; i < theRow.getLastCellNum(); i++) {
        String v = getCellValue(sheet, row, i).trim();
        v = v.replaceAll(" ", "");
        if (v.equalsIgnoreCase(value))
            return i;
    }
    return -1;
}

From source file:com.dtrules.compiler.excel.util.ImportRuleSets.java

License:Apache License

/**
 * Clear a number field that shouldn't have any numbers.
 * @param sheet/*  w  w  w .  j av a  2s.com*/
 * @param row
 */
void clearNumber(HSSFSheet sheet, int row) {
    String numberFound = getNumber(sheet, row);
    int field = getColumn("number");
    if (field != -1) {
        if (sheet != null && sheet.getRow(row) != null) {
            sheet.getRow(row).createCell(field).setCellValue("");
        }
    }
}

From source file:com.dtrules.compiler.excel.util.ImportRuleSets.java

License:Apache License

/**
 * Prints the Context/InitialAction/Condition/Action number, does checks, prints
 * errors.  We MIGHT make it fix the errors...
 * @param out//from w  w w . j a v  a  2s  .  c om
 * @param sheet
 * @param row
 * @param label
 * @param count
 * @return Return an error message, or null.
 */
private String printNumber(XMLPrinter out, HSSFSheet sheet, int row, String label, int count) {
    String numberFound = getNumber(sheet, row);
    int v;
    String result = null;
    try {
        v = Integer.parseInt(numberFound);
    } catch (NumberFormatException e) {
        result = " Invalid number " + label + " on the " + count;
        v = count;
        int field = getColumn("number");
        if (field != -1) {
            sheet.getRow(row).createCell(field, HSSFCell.CELL_TYPE_NUMERIC);
            sheet.getRow(row).getCell(field).setCellValue((double) count);
        }
        CountsAreDirty = true;
    }
    if (v != count) {
        result = " Incorrect Count " + label + " on the " + count + ".  Found " + numberFound;
        int field = getColumn("number");
        if (field != -1) {
            sheet.getRow(row).getCell(field).setCellValue((double) count);
        }
        CountsAreDirty = true;
    }
    out.printdata(label, numberFound);
    return result;
}

From source file:com.dtrules.compiler.excel.util.ImportRuleSets.java

License:Apache License

/**
 * Returns the index of the heading of the next block.
 * @param sheet//from w  w  w  .  j a  va  2 s .  c  o m
 * @param row
 * @return
 */
int nextBlock(HSSFSheet sheet, int row) {
    String attrib = getNextAttrib(sheet, row);
    if (sheet.getRow(row) == null) {
        return row;
    }
    Cell c = sheet.getRow(row).getCell(0);
    while (attrib.equals("") && c.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {
        row++;
        attrib = getNextAttrib(sheet, row);
        if (row > sheet.getLastRowNum())
            return row - 1;
        c = sheet.getRow(row).getCell(0);
    }
    return row;
}

From source file:com.dv.util.DVExcelIO.java

License:Open Source License

public static Vector<Vector> readExcelReturnArrayList(String fileName, String sheetName, int rowCount) {

    Vector<Vector> hm = new Vector<Vector>();

    File file = new File(fileName);
    FileInputStream in = null;//w  w  w  .  ja  v a  2s. c  om

    try {

        in = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(in);
        HSSFSheet sheet = workbook.getSheet(sheetName);

        HSSFRow row = null;
        HSSFCell cell = null;

        for (int i = 0; i < rowCount; i = i + 3) {

            Vector cellList = new Vector();

            row = sheet.getRow((short) i);
            if (row != null) {
                cell = row.getCell(0);
                String cellString = cell.toString().replace(".0", " ").trim();
                cellString = cellString.replace("\n", " ").trim();
                cellList.add(0, cellString);
            }

            row = sheet.getRow((short) i + 1);
            if (row != null) {
                cell = row.getCell(0);
                String cellString = cell.toString().replace(".0", " ").trim();
                cellString = cellString.replace("\n", " ").trim();
                cellList.add(1, cellString);
            }

            row = sheet.getRow((short) i + 2);
            if (row != null) {
                cell = row.getCell(0);
                String cellString = cell.toString().replace(".0", " ").trim();
                cellString = cellString.replace("\n", " ").trim();
                cellList.add(2, cellString);
            }

            hm.addElement(cellList);

        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e1) {
            }
        }
    }

    return hm;

}

From source file:com.dv.util.DVExcelIO.java

License:Open Source License

public static Vector setExcelBHTIMFormat(String fullExcelFileName, String sheetName, int rowNumbers) {

    File file = new File(fullExcelFileName);
    FileInputStream in = null;/* ww w. j a v  a2  s.c om*/
    Vector cols = new Vector();

    try {

        in = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(in);
        HSSFSheet sheet = workbook.getSheet(sheetName);

        HSSFRow row = null;
        HSSFCell cell = null;

        for (int i = 2; i < rowNumbers; i++) {

            row = sheet.getRow(i);

            cell = row.getCell(4);//9 for cty

            String ppp = cell.toString().trim();

            cell = row.getCell(9);

            String fff = cell.toString().trim();

            if (!ppp.equals("")) {

                String contents = "Verify from FMS side for " + ppp + "(" + fff + ")";

                //                    cols.addElement(contents);

                row.getCell(16).setCellValue(contents);

            } else {
                return null;
            }

        }

        FileOutputStream fOut = new FileOutputStream(file);
        workbook.write(fOut);
        fOut.flush();
        fOut.close();

    } catch (Exception eee) {

    }

    return cols;
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

private void importDataToWorkbook(HSSFWorkbook xlsBook, Workbook workbook) {
    if (workbook.getObservations() != null) {
        HSSFSheet observationSheet = xlsBook.getSheetAt(1);
        int xlsRowIndex = 1; //row 0 is the header row
        for (MeasurementRow wRow : workbook.getObservations()) {
            HSSFRow xlsRow = observationSheet.getRow(xlsRowIndex);
            for (MeasurementData wData : wRow.getDataList()) {
                String label = wData.getLabel();
                int xlsColIndex = findColumn(observationSheet, label);
                Cell cell = xlsRow.getCell(xlsColIndex);
                String xlsValue = "";

                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        Double doubleVal = Double.valueOf(cell.getNumericCellValue());
                        Integer intVal = Integer.valueOf(doubleVal.intValue());
                        if (Double.parseDouble(intVal.toString()) == doubleVal.doubleValue()) {
                            xlsValue = intVal.toString();
                        } else {
                            xlsValue = doubleVal.toString();
                        }//w ww .j a  va  2 s .c o  m

                    } else
                        xlsValue = cell.getStringCellValue();
                }
                wData.setValue(xlsValue);
            }
            xlsRowIndex++;
        }
    }
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

private void validateDescriptionSheetFirstCell(HSSFSheet descriptionSheet) throws WorkbookParserException {
    if (!TEMPLATE_DESCRIPTION_SHEET_FIRST_VALUE
            .equalsIgnoreCase(descriptionSheet.getRow(0).getCell(0).getStringCellValue())) {
        throw new WorkbookParserException("error.workbook.import.invalidFormatDescriptionSheet");
    }/*from   w ww . j av a2 s .  c o  m*/
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

private void validateRowIdentifiers(HSSFSheet observationSheet, Workbook workbook)
        throws WorkbookParserException {
    if (workbook.getObservations() != null) {
        String gidLabel = getColumnLabel(workbook, TermId.GID.getId());
        String desigLabel = getColumnLabel(workbook, TermId.DESIG.getId());
        String entryLabel = getColumnLabel(workbook, TermId.ENTRY_NO.getId());
        int gidCol = findColumn(observationSheet, gidLabel);
        int desigCol = findColumn(observationSheet, desigLabel);
        int entryCol = findColumn(observationSheet, entryLabel);
        int rowIndex = 1;
        for (MeasurementRow wRow : workbook.getObservations()) {
            HSSFRow row = observationSheet.getRow(rowIndex++);

            Integer gid = getMeasurementDataValueInt(wRow, gidLabel);
            String desig = getMeasurementDataValue(wRow, desigLabel);
            Integer entry = getMeasurementDataValueInt(wRow, entryLabel);
            Integer xlsGid = getExcelValueInt(row, gidCol);
            String xlsDesig = row.getCell(desigCol).getStringCellValue();
            Integer xlsEntry = getExcelValueInt(row, entryCol);

            if (gid == null || desig == null || entry == null || xlsDesig == null || !gid.equals(xlsGid)
                    || !desig.trim().equalsIgnoreCase(xlsDesig.trim()) || !entry.equals(xlsEntry)) {

                throw new WorkbookParserException("error.workbook.import.observationRowMismatch");
            }/*from   w w  w  .  j  ava  2 s  . c o m*/
        }
    }
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

private List<String> getAllVariates(HSSFSheet descriptionSheet) {
    List<String> variates = new ArrayList<String>();

    int startRowIndex = findRow(descriptionSheet, TEMPLATE_SECTION_VARIATE) + 1;
    int endRowIndex = descriptionSheet.getLastRowNum();

    if (startRowIndex <= endRowIndex) {
        for (int rowIndex = startRowIndex; rowIndex <= endRowIndex; rowIndex++) {
            variates.add(descriptionSheet.getRow(rowIndex).getCell(0).getStringCellValue().toUpperCase());
        }/*from   w w  w . j a  v a2  s .co  m*/
    }

    return variates;
}