Example usage for org.apache.poi.ss.usermodel Sheet getLastRowNum

List of usage examples for org.apache.poi.ss.usermodel Sheet getLastRowNum

Introduction

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

Prototype

int getLastRowNum();

Source Link

Document

Gets the last row on the sheet Note: rows which had content before and were set to empty later might still be counted as rows by Excel and Apache POI, so the result of this method will include such rows and thus the returned value might be higher than expected!

Usage

From source file:eionet.gdem.conversion.excel.reader.ExcelReader.java

License:Mozilla Public License

@Override
public boolean isEmptySheet(String sheetName) {

    Sheet sheet = getSheet(sheetName);
    int rowCount = sheet.getLastRowNum();
    if (rowCount < 1) {
        return true;
    }//w  ww  .j av  a  2 s  .c o  m

    // check if the first row has any data
    for (int i = 1; i <= rowCount; i++) {
        Row row = sheet.getRow(i);
        if (isEmptyRow(row)) {
            continue;
        } else {
            return false;
        }
    }

    return true;
}

From source file:eionet.gdem.conversion.excel.reader.ExcelReader.java

License:Mozilla Public License

/**
 * Method goes through 4 rows and search the best fit of XML Schema. The deault row is 4.
 *
 * @param schemaSheet Schema sheet name.
 * @return schema URL.//ww w  . ja  va  2 s  .c  o  m
 */
private String findSchemaFromSheet(Sheet schemaSheet) {
    Row schemaRow = null;
    Cell schemaCell = null;

    for (int i = 3; i > -1; i--) {
        if (schemaSheet.getLastRowNum() < i) {
            continue;
        }
        schemaRow = schemaSheet.getRow(i);
        if (schemaRow == null) {
            continue;
        }
        if (schemaRow.getLastCellNum() < 0) {
            continue;
        }
        schemaCell = schemaRow.getCell(0);
        String val = schemaCell.getRichStringCellValue().toString();

        if (val.startsWith("http://") && val.toLowerCase().indexOf("/getschema") > 0 && Utils.isURL(val)) {
            return val;
        }
    }
    return null;
}

From source file:eionet.gdem.conversion.excel.reader.ExcelReader.java

License:Mozilla Public License

/**
 * Method goes through rows after XML Schema and finds schemas for Excel sheets (DataDict tables). cell(0) =sheet name;
 * cell(1)=XML schema/*from   www .  ja v  a 2s.  co m*/
 *
 * @param schemaSheet sheet name
 * @return Map
 */
private Map<String, String> findSheetSchemas(Sheet schemaSheet) {

    Row schemaRow = null;
    Cell schemaCell = null;
    Cell sheetCell = null;

    Map<String, String> result = new LinkedHashMap<String, String>();
    if (schemaSheet.getLastRowNum() < 1) {
        return null;
    }

    for (int i = 0; i <= schemaSheet.getLastRowNum(); i++) {
        schemaRow = schemaSheet.getRow(i);
        if (schemaRow == null) {
            continue;
        }
        if (schemaRow.getLastCellNum() < 1) {
            continue;
        }
        schemaCell = schemaRow.getCell(1);
        if (schemaCell == null) {
            continue;
        }
        String schemaValue = schemaCell.getRichStringCellValue().toString();

        if (schemaValue.startsWith("http://") && schemaValue.toLowerCase().indexOf("/getschema") > 0
                && Utils.isURL(schemaValue)) {

            sheetCell = schemaRow.getCell(0);
            String sheetValue = sheetCell.getRichStringCellValue().toString();
            if (sheetValue == null) {
                continue;
            }
            if (sheetValue != null && sheetValue.length() > 31) {
                sheetValue = sheetValue.substring(0, 31);
            }
            Sheet sheet = getSheet(sheetValue);
            if (sheet != null && !result.containsKey(sheetValue)) {
                result.put(sheetValue, schemaValue);
            }
        }
    }
    return result;
}

From source file:es.us.isa.jdataset.loader.ExcelToCSV.java

License:Apache License

/**
 * Called to convert the contents of the currently opened workbook into
 * a CSV file./*w ww . jav a 2  s.  com*/
 */
private void convertToCSV() {
    Sheet sheet = null;
    Row row = null;
    int lastRowNum = 0;
    this.csvData = new ArrayList<ArrayList>();

    System.out.println("Converting files contents to CSV format.");

    // Discover how many sheets there are in the workbook....
    int numSheets = this.getWorkbook().getNumberOfSheets();

    // and then iterate through them.
    int i = 0;

    // Get a reference to a sheet and check to see if it contains
    // any rows.
    sheet = this.getWorkbook().getSheetAt(i);
    if (sheet.getPhysicalNumberOfRows() > 0) {

        // Note down the index number of the bottom-most row and
        // then iterate through all of the rows on the sheet starting
        // from the very first row - number 1 - even if it is missing.
        // Recover a reference to the row and then call another method
        // which will strip the data from the cells and build lines
        // for inclusion in the resylting CSV file.
        lastRowNum = sheet.getLastRowNum();
        for (int j = 0; j <= lastRowNum; j++) {
            row = sheet.getRow(j);
            this.rowToCSV(row);
        }
    }

}

From source file:eu.esdihumboldt.hale.app.bgis.ade.common.AbstractAnalyseTable.java

License:Open Source License

/**
 * Analyse the table content./*from w w w  . j  ava2 s .c  o  m*/
 * 
 * @param sheet the table sheet
 */
private void analyseContent(Sheet sheet) {
    // for each row starting from the second
    for (int i = 1; i < sheet.getLastRowNum(); i++) {
        Row row = sheet.getRow(i);
        analyseRow(i, row);
    }
}

From source file:eu.esdihumboldt.hale.io.xls.AbstractAnalyseTable.java

License:Open Source License

/**
 * Analyse the table content.//  w ww  .ja  v a  2 s  . c o m
 * 
 * @param sheet the table sheet
 */
private void analyseContent(Sheet sheet) {
    // for each row starting from the second
    for (int i = 1; i <= sheet.getLastRowNum(); i++) {
        Row row = sheet.getRow(i);
        analyseRow(i, row);
    }
}

From source file:fi.semantum.strategia.widget.Database.java

License:Open Source License

public static Database load(Main main, String databaseId) {

    Database result = null;/*from  www  .ja v a 2s  .co m*/

    synchronized (Database.class) {

        try {

            Map<String, EnumerationDatatype> enumerations = new HashMap<String, EnumerationDatatype>();

            try {
                File file = new File(Main.baseDirectory(), "database.xlsx");
                FileInputStream fis = new FileInputStream(file);
                Workbook book = WorkbookFactory.create(fis);
                fis.close();
                Sheet sheet = book.getSheetAt(0);
                for (int rowN = sheet.getFirstRowNum(); rowN <= sheet.getLastRowNum(); rowN++) {
                    Row row = sheet.getRow(rowN);
                    Cell cell = row.getCell(0, Row.RETURN_BLANK_AS_NULL);
                    if (cell != null) {
                        if ("Monivalinta".equals(cell.toString())) {
                            Cell id = row.getCell(1, Row.RETURN_BLANK_AS_NULL);
                            if (id == null)
                                continue;
                            Cell traffic = row.getCell(2, Row.RETURN_BLANK_AS_NULL);
                            if (traffic == null)
                                continue;
                            int count = row.getLastCellNum() - 3;
                            if (traffic.toString().length() != count)
                                continue;

                            List<String> values = new ArrayList<String>();
                            for (int i = 0; i < count; i++) {
                                Cell val = row.getCell(3 + i, Row.RETURN_BLANK_AS_NULL);
                                if (val != null)
                                    values.add(val.toString());
                            }
                            enumerations.put(id.toString(),
                                    new EnumerationDatatype(result, id.toString(), values, traffic.toString()));

                        }
                    }
                }

            } catch (Exception e) {
            }

            File f = new File(Main.baseDirectory(), databaseId);
            FileInputStream fileIn = new FileInputStream(f);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            result = (Database) in.readObject();
            in.close();
            fileIn.close();

            result.databaseId = databaseId;

            main.setDatabase(result);

            migrate(main, enumerations);
            validate(main);

            result.lastModified = new Date(f.lastModified());

        } catch (IOException i) {

            i.printStackTrace();
            result = create(main, databaseId);

        } catch (ClassNotFoundException c) {

            System.out.println("Database class not found");
            c.printStackTrace();
            result = create(main, databaseId);

        }

        result.touchBackup();

        result.updateTags();

        try {

            if (!Lucene.indexExists(databaseId)) {

                Lucene.startWrite(databaseId);
                for (Base b : result.enumerate()) {
                    Lucene.set(databaseId, b.uuid, b.searchText(result));
                }
                Lucene.endWrite();

            }

        } catch (Throwable t) {

            t.printStackTrace();

        }

    }

    return result;

}

From source file:fr.unice.i3s.rockflows.experiments.automatictest.ExcelUtils.java

/**
* Given a sheet, this method deletes a column from a sheet and moves
* all the columns to the right of it to the left one cell.
*
* Note, this method will not update any formula references.
*
* @param sheet//from   w w w.  j  a  v a  2  s.  c om
* @param column
*/
private static void deleteColumn(Sheet sheet, int columnToDelete) {
    int maxColumn = 0;
    for (int iii = 0; iii < sheet.getLastRowNum() + 1; iii++) {
        Row row = sheet.getRow(iii);
        // if no row exists here; then nothing to do; next!
        if (row == null) {
            continue;
        }

        // if the row doesn't have this many columns then we are good; next!
        int lastColumn = row.getLastCellNum();
        if (lastColumn > maxColumn) {
            maxColumn = lastColumn;
        }

        if (lastColumn < columnToDelete) {
            continue;
        }

        for (int x = columnToDelete + 1; x < lastColumn + 1; x++) {
            Cell oldCell = row.getCell(x - 1);
            if (oldCell != null) {
                row.removeCell(oldCell);
            }
            Cell nextCell = row.getCell(x);
            if (nextCell != null) {
                Cell newCell = row.createCell(x - 1, nextCell.getCellType());
                cloneCell(newCell, nextCell);
            }
        }
    }

    // Adjust the column widths
    for (int ccc = 0; ccc < maxColumn; ccc++) {
        sheet.setColumnWidth(ccc, sheet.getColumnWidth(ccc + 1));
    }
}

From source file:fsart.diffTools.converter.ToCSV.java

License:Apache License

/**
 * Called to convert the contents of the currently opened workbook into
 * a CSV file.//  www . jav  a  2  s  . co  m
 */
public void convertToCSV() {
    Sheet sheet = null;
    Row row = null;
    int lastRowNum = 0;
    this.csvData = new ArrayList<ArrayList<String>>();

    System.out.println("Converting files contents to CSV format.");

    // Discover how many sheets there are by the workbook....
    int numSheets = this.workbook.getNumberOfSheets();

    // and then iterate through them.
    for (int i = 0; i < numSheets; i++) {

        // Get a reference to a sheet and check to see if it contains
        // any rows.
        sheet = this.workbook.getSheetAt(i);
        if (sheet.getPhysicalNumberOfRows() > 0) {

            // Note down the index number of the bottom-most row and
            // then iterate through all of the rows on the sheet starting
            // from the very first row - number 1 - even if it is missing.
            // Recover a reference to the row and then call another method
            // which will strip the data from the cells and build lines
            // for inclusion by the resylting CSV file.
            lastRowNum = sheet.getLastRowNum();
            for (int j = 0; j <= lastRowNum; j++) {
                row = sheet.getRow(j);
                this.rowToCSV(row);
            }
        }
    }
}

From source file:fsart.diffTools.converter.ToCSV.java

License:Apache License

public void convertToCSV(String sheetName) {
    Sheet sheet = null;
    Row row = null;//from  www . j  a  v a 2s .  c  o m
    int lastRowNum = 0;
    this.csvData = new ArrayList<ArrayList<String>>();

    int numSheetsToConvert = this.workbook.getSheetIndex(sheetName);

    // Get a reference to a sheet and check to see if it contains
    // any rows.
    if (sheetName != null && !sheetName.equals("")) {
        sheet = this.workbook.getSheet(sheetName);
    }
    if (sheet == null) {
        sheet = this.workbook.getSheetAt(0);
    }
    if (sheet != null && sheet.getPhysicalNumberOfRows() > 0) {

        // Note down the index number of the bottom-most row and
        // then iterate through all of the rows on the sheet starting
        // from the very first row - number 1 - even if it is missing.
        // Recover a reference to the row and then call another method
        // which will strip the data from the cells and build lines
        // for inclusion by the resylting CSV file.
        lastRowNum = sheet.getLastRowNum();
        for (int j = 0; j <= lastRowNum; j++) {
            row = sheet.getRow(j);
            this.rowToCSV(row);
        }
    }
}