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

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

Introduction

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

Prototype

short getLastCellNum();

Source Link

Document

Gets the index of the last cell contained in this row PLUS ONE.

Usage

From source file:com.ocs.dynamo.importer.impl.BaseXlsImporter.java

License:Apache License

@Override
protected boolean isWithinRange(Row row, XlsField field) {
    return row.getFirstCellNum() + field.index() < row.getLastCellNum();
}

From source file:com.opendoorlogistics.core.tables.io.PoiIO.java

License:Open Source License

/**
 * See http://thinktibits.blogspot.co.uk/2012/12/Java-POI-XLS-XLSX-Change-Cell-Font-Color-Example.html
 * Currently only for xlsx/*from  www .j a v a 2 s.c om*/
 * @param wb
 * @param sheet
 */
private static void styleHeader(Workbook wb, Sheet sheet) {
    if (XSSFWorkbook.class.isInstance(wb) && XSSFSheet.class.isInstance(sheet)) {
        XSSFWorkbook my_workbook = (XSSFWorkbook) wb;
        XSSFCellStyle my_style = my_workbook.createCellStyle();
        XSSFFont my_font = my_workbook.createFont();
        my_font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        my_style.setFont(my_font);

        Row row = sheet.getRow(0);
        if (row != null && row.getFirstCellNum() >= 0) {
            for (int i = row.getFirstCellNum(); i <= row.getLastCellNum(); i++) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    cell.setCellStyle(my_style);
                }
            }
        }
    }
}

From source file:com.opendoorlogistics.core.tables.io.PoiIO.java

License:Open Source License

private static Dimension getBoundingBox(Sheet sheet) {
    Dimension ret = new Dimension(0, sheet.getLastRowNum() + 1);
    for (int i = 0; i < ret.height; i++) {
        Row row = sheet.getRow(i);
        if (row != null) {
            ret.width = Math.max(ret.width, row.getLastCellNum());
        }//from  w  w w  . j a  v  a  2s  .c  o  m
    }
    return ret;
}

From source file:com.opendoorlogistics.core.tables.io.PoiIO.java

License:Open Source License

/**
 * Schema table can contain multiple tables...
 * @param sheet//from  ww w.  jav a 2 s . co  m
 */
private static SchemaSheetInformation importSchemaTables(Sheet sheet, ExecutionReport report) {
    List<ODLTableReadOnly> tables = new ArrayList<>();

    // schema tables are separated by empty rows
    int lastRow = sheet.getLastRowNum();
    int firstRow = sheet.getFirstRowNum();

    int firstNonEmptyRow = -1;
    int nbCols = 0;
    for (int x = firstRow; x <= lastRow; x++) {

        // check for completely empty row
        Row row = sheet.getRow(x);
        boolean isEmptyRow = true;
        for (int y = 0; row != null && y <= row.getLastCellNum(); y++) {
            if (isEmptyCell(row, y) == false) {
                isEmptyRow = false;
            }
        }

        if (isEmptyRow || x == lastRow) {

            // dump table if row was empty or on last row, but we previously had a non empty row
            if (firstNonEmptyRow != -1) {
                ODLDatastoreAlterable<ODLTableAlterable> tmpDs = ODLDatastoreImpl.alterableFactory.create();
                ODLTableAlterable table = tmpDs.createTable(sheet.getSheetName(), -1);
                importSheetSubset(table, sheet, null, true, firstNonEmptyRow, isEmptyRow ? x - 1 : x, nbCols);
                tables.add(table);
            }
            firstNonEmptyRow = -1;
        } else if (firstNonEmptyRow == -1) {
            // initialise table if we've just found the first non empty row
            firstNonEmptyRow = x;
            nbCols = 0;
            for (int y = 0; y <= row.getLastCellNum(); y++) {
                if (isEmptyCell(row, y)) {
                    break;
                } else {
                    nbCols = y + 1;
                }
            }
        }
    }

    return readSchemaFromODLTables(tables, report);
}

From source file:com.openitech.db.model.ExcelDataSource.java

License:Apache License

@Override
public boolean loadData(boolean reload, int oldRow) {
    boolean result = false;

    if (isDataLoaded && !reload) {
        return false;
    }/*from  w  w w . j a  v a2 s  . co  m*/
    if (sourceFile != null) {
        try {
            Workbook workBook = WorkbookFactory.create(new FileInputStream(sourceFile));
            //        HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(sourceFile));
            Sheet sheet = workBook.getSheetAt(0);
            DataFormatter dataFormatter = new DataFormatter(Locale.GERMANY);
            FormulaEvaluator formulaEvaluator = workBook.getCreationHelper().createFormulaEvaluator();

            int lastRowNum = sheet.getLastRowNum();

            boolean isFirstLineHeader = true;

            //count = sheet. - (isFirstLineHeader ? 1 : 0);
            int tempCount = 0;
            for (int j = 0; j <= lastRowNum; j++) {
                //zane se z 0
                Row row = row = sheet.getRow(j);
                if (row == null) {
                    continue;
                }

                // display row number in the console.
                System.out.println("Row No.: " + row.getRowNum());
                if (isFirstLineHeader && row.getRowNum() == 0) {
                    populateHeaders(row);
                    continue;
                }
                tempCount++;

                Map<String, DataColumn> values;
                if (rowValues.containsKey(row.getRowNum())) {
                    values = rowValues.get(row.getRowNum());
                } else {
                    values = new HashMap<String, DataColumn>();
                    rowValues.put(row.getRowNum(), values);
                }

                // once get a row its time to iterate through cells.
                int lastCellNum = row.getLastCellNum();
                for (int i = 0; i <= lastCellNum; i++) {
                    DataColumn dataColumn = new DataColumn();
                    Cell cell = row.getCell(i);
                    if (cell == null) {
                        continue;
                    }
                    System.out.println("Cell No.: " + cell.getColumnIndex());
                    System.out.println("Value: " + dataFormatter.formatCellValue(cell));
                    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                        dataColumn = new DataColumn(dataFormatter.formatCellValue(cell, formulaEvaluator));
                    } else {
                        dataColumn = new DataColumn(dataFormatter.formatCellValue(cell));
                    }

                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC: {
                        // cell type numeric.
                        System.out.println("Numeric value: " + cell.getNumericCellValue());
                        dataColumn = new DataColumn(dataFormatter.formatCellValue(cell));
                        break;
                    }
                    case Cell.CELL_TYPE_STRING:
                        // cell type string.
                        System.out.println("String value: " + cell.getStringCellValue());
                        dataColumn = new DataColumn(dataFormatter.formatCellValue(cell));
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        // cell type string.
                        System.out.println("String value: " + cell.getBooleanCellValue());
                        dataColumn.setValue(cell.getBooleanCellValue(), Boolean.class);
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        // cell type string.
                        System.out.println(
                                "Formula value: " + dataFormatter.formatCellValue(cell, formulaEvaluator));
                        dataColumn = new DataColumn(dataFormatter.formatCellValue(cell, formulaEvaluator));
                        break;
                    default:
                        dataColumn.setValue(cell.getStringCellValue(), String.class);
                        break;
                    }

                    values.put(getColumnName(cell.getColumnIndex()).toUpperCase(), dataColumn);

                }
            }

            count = tempCount;

            isDataLoaded = true;
            //se postavim na staro vrstico ali 1
            if (oldRow > 0) {
                absolute(oldRow);
            } else {
                first();
            }

            result = true;
        } catch (Exception ex) {
            Logger.getLogger(ExcelDataSource.class.getName()).log(Level.SEVERE, null, ex);
            result = false;
        }
    }

    return result;
}

From source file:com.openitech.db.model.ExcelDataSource.java

License:Apache License

private void populateHeaders(Row row) {
    columnCount = 0;/*from  w  w  w . jav  a  2  s . co m*/
    int lastCellNum = row.getLastCellNum();
    for (int i = 0; i <= lastCellNum; i++) {
        Cell cell = row.getCell(i);
        if (cell == null) {
            continue;
        }

        System.out.println("String value: " + cell.getStringCellValue());

        String header = cell.getStringCellValue();
        columnMapping.put(header, cell.getColumnIndex());
        columnMappingIndex.put(cell.getColumnIndex(), header);
        columnCount++;
    }
}

From source file:com.ostrichemulators.semtool.poi.main.POIReader.java

License:Open Source License

public static ImportData readNonloadingSheet(Workbook workbook) {
    ImportData id = new ImportData();

    int sheets = workbook.getNumberOfSheets();
    for (int sheetnum = 0; sheetnum < sheets; sheetnum++) {
        Sheet sheet = workbook.getSheetAt(sheetnum);
        String sheetname = workbook.getSheetName(sheetnum);

        // we need to shoehorn the arbitrary data from a spreadsheet into our
        // ImportData class, which has restrictions on the data...we're going
        // to do it by figuring out the row with the most columns, and then
        // naming all the columns with A, B, C...AA, AB...
        // then load everything as if it was plain data
        // first, figure out our max number of columns
        int rows = sheet.getLastRowNum();
        int maxcols = Integer.MIN_VALUE;
        for (int r = 0; r <= rows; r++) {
            Row row = sheet.getRow(r);
            if (null != row) {
                int cols = (int) row.getLastCellNum();
                if (cols > maxcols) {
                    maxcols = cols;/*from www . j a v  a 2 s .  co m*/
                }
            }
        }

        // second, make "properties" for each column
        LoadingSheetData nlsd = new LoadingSheetData(sheetname, "A");
        for (int c = 1; c < maxcols; c++) {
            nlsd.addProperty(Integer.toString(c));
        }

        // lastly, fill the sheets
        for (int r = 0; r <= rows; r++) {
            Row row = sheet.getRow(r);
            if (null != row) {
                Map<String, Value> propmap = new HashMap<>();

                int lastpropcol = row.getLastCellNum();
                for (int c = 1; c <= lastpropcol; c++) {
                    String val = getString(row.getCell(c));
                    if (!val.isEmpty()) {
                        propmap.put(Integer.toString(c), VF.createLiteral(val));
                    }
                }

                nlsd.add(getString(row.getCell(0)), propmap);
            }
        }

        if (!nlsd.isEmpty()) {
            id.add(nlsd);
        }
    }

    return id;
}

From source file:com.plant.bussines.NboComputation.java

public void doComputation(String inputFile) {
    try {//from  w ww  .jav  a 2 s  . c  o m
        //IF(ABS(D2)>0.08,D2,0)
        String currentElement;
        String atom;
        String resultIntoFile = "";
        ArrayList<String> resultArrayList;
        double valNumber;
        String finalResult = "";
        String temp;

        double currentVal;
        BufferedWriter bufferedWriter = new BufferedWriter(
                new FileWriter("/windows/Study_Data/JAVA/MyApplications/output.txt"));
        ExcelAdapter excelAdapter = new ExcelAdapter(inputFile);
        excelAdapter.setSheet(0);
        HashMap<Integer, ArrayList<String>> myHeaderMap = excelAdapter.getHeader();
        while (excelAdapter.hasRow()) {
            Row currentRow = excelAdapter.getCurrentRow();
            atom = currentRow.getCell(1).getStringCellValue();
            currentElement = currentRow.getCell(2).getStringCellValue();

            for (int i = 3; i < currentRow.getLastCellNum(); i++) {
                valNumber = currentRow.getCell(i).getNumericCellValue();
                System.out.println(valNumber);

                currentVal = (Math.abs(valNumber) > 0.08) ? valNumber : 0.0;
                if (currentVal > 0) {

                    finalResult = "+" + currentVal + "(" + currentElement + " " + atom + ")";
                } else {
                    finalResult = currentVal + "(" + currentElement + " " + atom + ")";
                }
                myHeaderMap.get(i).add(finalResult);

            }

        }
        excelAdapter.closeFile();
        Iterator it = myHeaderMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            //   System.out.println(pair.getKey() + " = " + pair.getValue());
            resultArrayList = (ArrayList<String>) pair.getValue();
            for (int i = 0; i < resultArrayList.size(); i++) {
                resultIntoFile = resultIntoFile + resultArrayList.get(i);
            }
            bufferedWriter.write(resultIntoFile + "\n");
            resultIntoFile = "";
        }
        bufferedWriter.close();
    } catch (Exception ex) {
        System.out.println("error " + ex.getMessage());
        ex.printStackTrace();
    }

}

From source file:com.plant.controll.ExcelAdapter.java

public HashMap<Integer, ArrayList<String>> getHeader() {
    HashMap<Integer, ArrayList<String>> headerMap = new HashMap();
    Row currentRow = rowIterator.next();
    for (int i = 3; i < currentRow.getLastCellNum(); i++) {
        headerMap.put(i, new ArrayList<String>());
        headerMap.get(i).add("#" + currentRow.getCell(i).getNumericCellValue() + "= ");
    }/*w w w .  jav a2  s .co  m*/
    return headerMap;

}

From source file:com.plugin.excel.util.ExcelUtil.java

License:Apache License

/**
 * @param newSheet//  w w  w. java2s  . co  m
 *            the sheet to create from the copy.
 * @param sheet
 *            the sheet to copy.
 * @param copyStyle
 *            true copy the style.
 */
public static void copySheets(SXSSFSheet newSheet, SXSSFSheet sheet, boolean copyStyle) {
    int maxColumnNum = 0;
    Map<Integer, CellStyle> styleMap = (copyStyle) ? new HashMap<Integer, CellStyle>() : null;
    for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
        Row srcRow = sheet.getRow(i);
        Row destRow = newSheet.createRow(i);
        if (srcRow != null) {
            ExcelUtil.copyRow(sheet, newSheet, srcRow, destRow, styleMap);
            if (srcRow.getLastCellNum() > maxColumnNum) {
                maxColumnNum = srcRow.getLastCellNum();
            }
        }
    }
    for (int i = 0; i <= maxColumnNum; i++) {
        newSheet.setColumnWidth(i, sheet.getColumnWidth(i));
    }
}