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

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

Introduction

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

Prototype

short getFirstCellNum();

Source Link

Document

Get the number of the first cell contained in this row.

Usage

From source file:com.miraisolutions.xlconnect.Workbook.java

License:Open Source License

public void appendWorksheet(DataFrame data, int worksheetIndex, boolean header) {
    Sheet sheet = getSheet(worksheetIndex);
    int lastRow = getLastRow(worksheetIndex);
    int firstCol = Integer.MAX_VALUE;
    for (int i = 0; i < lastRow && firstCol > 0; i++) {
        Row row = sheet.getRow(i);
        if (row != null && row.getFirstCellNum() < firstCol)
            firstCol = row.getFirstCellNum();
    }/*  w ww. j a  va  2 s  .  co m*/
    if (firstCol == Integer.MAX_VALUE)
        firstCol = 0;

    writeWorksheet(data, worksheetIndex, getLastRow(worksheetIndex) + 1, firstCol, header);
}

From source file:com.miraisolutions.xlconnect.Workbook.java

License:Open Source License

public int[] getBoundingBox(int sheetIndex, int startRow, int startCol, int endRow, int endCol,
        boolean autofitRow, boolean autofitCol) {
    Sheet sheet = workbook.getSheetAt(sheetIndex);
    final int mark = Integer.MAX_VALUE - 1;

    if (startRow < 0) {
        startRow = sheet.getFirstRowNum();
        if (sheet.getRow(startRow) == null) {
            // There is no row in this sheet
            startRow = -1;/* w  ww  .  j  a  v a2  s  .c o m*/
        }
    }

    if (endRow < 0) {
        endRow = sheet.getLastRowNum();
        if (sheet.getRow(endRow) == null) {
            // There is no row in this sheet
            endRow = -1;
        }
    }

    int minRow = startRow;
    int maxRow = endRow;
    int minCol = startCol;
    int maxCol = endCol < 0 ? mark : endCol;

    startCol = startCol < 0 ? mark : startCol;
    endCol = endCol < 0 ? -1 : endCol;
    Cell topLeft = null, bottomRight = null;
    boolean anyCell = false;
    for (int i = minRow; i > -1 && i <= maxRow; i++) {
        Row r = sheet.getRow(i);
        if (r != null) {
            // Determine column boundaries
            int start = Math.max(minCol, r.getFirstCellNum());
            int end = Math.min(maxCol + 1, r.getLastCellNum()); // NOTE: getLastCellNum is 1-based!
            boolean anyNonBlank = false;
            for (int j = start; j > -1 && j < end; j++) {
                Cell c = r.getCell(j);
                if (c != null && c.getCellType() != Cell.CELL_TYPE_BLANK) {
                    anyCell = true;
                    anyNonBlank = true;
                    if ((autofitCol || minCol < 0) && (topLeft == null || j < startCol)) {
                        startCol = j;
                        topLeft = c;
                    }
                    if ((autofitCol || maxCol == mark) && (bottomRight == null || j > endCol)) {
                        endCol = j;
                        bottomRight = c;
                    }
                }
            }
            if (autofitRow && anyNonBlank) {
                endRow = i;
                if (sheet.getRow(startRow) == null) {
                    startRow = i;
                }
            }
        }
    }

    if ((autofitRow || startRow < 0) && !anyCell) {
        startRow = endRow = -1;
    }
    if ((autofitCol || startCol == mark) && !anyCell) {
        startCol = endCol = -1;
    }

    return new int[] { startRow, startCol, endRow, endCol };
}

From source file:com.murilo.excel.ExcelHandler.java

public String getLine(int sheetIndex, int lineIndex, char separator) {
    Row linha = sheets[sheetIndex].getRow(lineIndex);
    String aux = "";
    for (int i = linha.getFirstCellNum(); i < linha.getLastCellNum(); i++) {
        Cell campo = linha.getCell(i);/*from   w  w w  .jav a2 s .  c om*/
        if ((i + 1) != linha.getLastCellNum()) {
            aux = aux + "\"" + stringrizeCell(campo) + "\"" + separator;
        } else {
            aux = aux + "\"" + stringrizeCell(campo) + "\"\n";
        }
    }

    return aux;
}

From source file:com.ncc.excel.test.ExcelUtil.java

License:Apache License

/** 
 * Excel? /* w  w  w .  j  a  va2s.c  o  m*/
 *  
 * @Title: WriteExcel 
 * @Date : 2014-9-11 ?01:33:59 
 * @param wb 
 * @param rowList 
 * @param xlsPath 
 */
private void writeExcel(Workbook wb, List<Row> rowList, String xlsPath) {

    if (wb == null) {
        out("???");
        return;
    }

    Sheet sheet = wb.getSheetAt(0);// sheet  

    // ???????  
    int lastRowNum = isOverWrite ? startReadPos : sheet.getLastRowNum() + 1;
    int t = 0;//  
    out("???" + rowList.size());
    for (Row row : rowList) {
        if (row == null)
            continue;
        // ???  
        int pos = findInExcel(sheet, row);

        Row r = null;// ??????  
        if (pos >= 0) {
            sheet.removeRow(sheet.getRow(pos));
            r = sheet.createRow(pos);
        } else {
            r = sheet.createRow(lastRowNum + t++);
        }

        //??  
        CellStyle newstyle = wb.createCellStyle();

        //?  
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            Cell cell = r.createCell(i);// ??  
            cell.setCellValue(getCellValue(row.getCell(i)));// ???  
            // cell.setCellStyle(row.getCell(i).getCellStyle());//  
            if (row.getCell(i) == null)
                continue;
            copyCellStyle(row.getCell(i).getCellStyle(), newstyle); // ????  
            cell.setCellStyle(newstyle);// ?  
            // sheet.autoSizeColumn(i);//  
        }
    }
    out("???:" + (rowList.size() - t) + " ?" + t);

    // ??  
    setMergedRegion(sheet);

    try {
        // ??Excel  
        FileOutputStream outputStream = new FileOutputStream(xlsPath);
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        out("Excel?? ");
        e.printStackTrace();
    }
}

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

License:Apache License

/**
 * Checks if any cell in the row contains a certain (String) value
 * /*from w w  w . jav  a  2 s .c om*/
 * @param row
 * @param value
 * @return
 */
protected boolean containsStringValue(Row row, String value) {
    if (row == null || !row.iterator().hasNext()) {
        return false;
    }

    boolean found = false;
    for (int i = row.getFirstCellNum(); !found && i < row.getLastCellNum(); i++) {
        if (row.getCell(i) != null) {
            try {
                found = value.equalsIgnoreCase(row.getCell(i).getStringCellValue());
            } catch (Exception ex) {
                // do nothing
            }
        }
    }
    return found;
}

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

License:Apache License

@Override
protected Cell getUnit(Row row, XlsField field) {
    return row.getCell(row.getFirstCellNum() + field.index());
}

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

License:Apache License

/**
 * Check if the specified row is completely empty
 * //from  ww w. j a  va2s. c  om
 * @param row
 * @return
 */
public boolean isRowEmpty(Row row) {
    if (row == null || row.getFirstCellNum() < 0) {
        return true;
    }

    Iterator<Cell> iterator = row.iterator();
    while (iterator.hasNext()) {
        Cell next = iterator.next();
        String value = next.getStringCellValue();
        if (!StringUtils.isEmpty(value)) {
            return false;
        }
    }

    return true;
}

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.opengamma.integration.copier.sheet.reader.SimpleXlsSheetReader.java

License:Open Source License

@Override
public Map<String, String> loadNextRow() {

    // Get a reference to the next Excel row
    Row rawRow = _sheet.getRow(_currentRowNumber++);

    // If the row is empty return null (assume end of table)
    if (rawRow == null || rawRow.getFirstCellNum() == -1) {
        return null; // new HashMap<String, String>();
    }//from   w w  w.  ja  v  a  2  s  . c om

    // Map read-in row onto expected columns
    Map<String, String> result = new HashMap<String, String>();
    for (int i = 0; i < getColumns().length; i++) {
        String cell = getCell(rawRow, rawRow.getFirstCellNum() + i).trim();
        if (cell != null && cell.length() > 0) {
            result.put(getColumns()[i], cell);
        }
    }

    return result;
}