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

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

Introduction

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

Prototype

int getRowNum();

Source Link

Document

Get row number this row represents

Usage

From source file:com.fjn.helper.common.io.file.office.excel.ListExcelSheetEditor.java

License:Apache License

@SuppressWarnings({ "unchecked", "unused", "deprecation" })
public <T> void insertItemToSheet(int rowIndex, T t) {
    if (t == null)
        return;/*from  w w w .j  ava  2 s.  c om*/
    // ?
    Row row = null;
    Cell cell = null;
    Class<T> clazz = (Class<T>) t.getClass();
    Sheet sheet = excelSheet.sheet;
    row = sheet.createRow(rowIndex);
    for (int j = 0; j < excelSheet.getFieldList().size(); j++) {
        try {
            Field field = clazz.getDeclaredField(excelSheet.getFieldList().get(j));
            field.setAccessible(true);
            cell = row.createCell(j);
            Class fieldType = field.getType();
            //   cell.setCellType(Cell.);
            Object value = field.get(t);
            setCellValue(cell, value);

        } catch (Exception ex) {
            if (logger.isEnabledFor(Priority.ERROR)) {
                logger.error("" + row.getRowNum() + "" + j + "", ex);
            }
        }
    }

}

From source file:com.FuntionLibrary.java

public static int findRow(XSSFSheet sheet, String cellContent) {
    try {//from w  ww .j  a v a 2s . c  o m
        for (Row row : sheet) {
            for (Cell cell : row) {
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
                        return row.getRowNum();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, cellContent + " is not a valid Please try again !");
                }
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, cellContent + " is not a valid Please try again !");
    }

    return 0;
}

From source file:com.funtl.framework.smoke.core.commons.excel.ExportExcel.java

License:Apache License

/**
 * ?/*from w w  w  . j a  v a 2s  .  c  o  m*/
 *
 * @param row    
 * @param column ?
 * @param val    
 * @param align  ??1?23??
 * @return ?
 */
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType) {
    Cell cell = row.createCell(column);
    String cellFormatString = "@";
    try {
        if (val == null) {
            cell.setCellValue("");
        } else if (fieldType != Class.class) {
            cell.setCellValue((String) fieldType.getMethod("setValue", Object.class).invoke(null, val));
        } else {
            if (val instanceof String) {
                cell.setCellValue((String) val);
            } else if (val instanceof Integer) {
                cell.setCellValue((Integer) val);
                cellFormatString = "0";
            } else if (val instanceof Long) {
                cell.setCellValue((Long) val);
                cellFormatString = "0";
            } else if (val instanceof Double) {
                cell.setCellValue((Double) val);
                cellFormatString = "0.00";
            } else if (val instanceof Float) {
                cell.setCellValue((Float) val);
                cellFormatString = "0.00";
            } else if (val instanceof Date) {
                cell.setCellValue((Date) val);
                cellFormatString = "yyyy-MM-dd HH:mm";
            } else {
                cell.setCellValue((String) Class
                        .forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
                                "fieldtype." + val.getClass().getSimpleName() + "Type"))
                        .getMethod("setValue", Object.class).invoke(null, val));
            }
        }
        if (val != null) {
            CellStyle style = styles.get("data_column_" + column);
            if (style == null) {
                style = wb.createCellStyle();
                style.cloneStyleFrom(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
                style.setDataFormat(wb.createDataFormat().getFormat(cellFormatString));
                styles.put("data_column_" + column, style);
            }
            cell.setCellStyle(style);
        }
    } catch (Exception ex) {
        log.info("Set cell value [" + row.getRowNum() + "," + column + "] error: " + ex.toString());
        cell.setCellValue(val.toString());
    }
    return cell;
}

From source file:com.github.camaral.sheeco.Sheeco.java

License:Apache License

private <T> List<T> readPayloads(final PayloadContext<T> ctx) {
    final List<T> payloads = new ArrayList<>();

    final int firstRowNum = ctx.getSheet().getFirstRowNum();
    int blankRowCount = 0;

    for (final Row row : ctx.getSheet()) {
        if (row.getRowNum() == firstRowNum) {
            // First row reserved for headers
            continue;
        }//from   w  ww  .j  ava2 s  . c om
        if (!isBlankRow(row)) {
            blankRowCount = 0;

            payloads.add(readPayload(row, ctx));

        } else {
            // if the blank rows limit is reached, throws an exception
            ++blankRowCount;
            if (blankRowCount >= MAX_BLANK_ROWS) {
                throw new SpreasheetUnmarshallingUnrecoverableException(
                        "serializer.spreadsheet.row.too.many.blank");
            }
        }
    }

    return payloads;
}

From source file:com.github.crab2died.handler.SheetTemplateHandler.java

License:Open Source License

/**
 * ???// w ww .ja va  2s  .  co  m
 */
private static void initModuleConfig(SheetTemplate template) {

    for (Row row : template.sheet) {
        for (Cell c : row) {
            if (c.getCellTypeEnum() != CellType.STRING)
                continue;
            String str = c.getStringCellValue().trim().toLowerCase();
            // ??
            if (HandlerConstant.SERIAL_NUMBER.equals(str)) {
                template.serialNumberColumnIndex = c.getColumnIndex();
            }
            // ?
            if (HandlerConstant.DATA_INIT_INDEX.equals(str)) {
                template.initColumnIndex = c.getColumnIndex();
                template.initRowIndex = row.getRowNum();
                template.rowHeight = row.getHeightInPoints();
            }
            // ???
            initStyles(template, c, str);
        }
    }
}

From source file:com.github.drbookings.ical.XlsxBookingFactory.java

License:Open Source License

@Override
public Collection<BookingBeanSer> build() {
    final List<BookingBeanSer> bookings = new ArrayList<>();
    FileInputStream stream = null;
    Workbook workbook = null;/*from   w  ww. j av  a  2  s.  com*/
    try {
        stream = new FileInputStream(file);
        workbook = new HSSFWorkbook(stream);
        final Sheet sheet = workbook.getSheetAt(0);
        if (logger.isInfoEnabled()) {
            logger.info("Processing sheet " + sheet.getSheetName());
        }
        final int indexBookingNumber = FileFormatBookingXLS.getColumnIndexBookingNumber(sheet.getRow(0));
        final int indexClientName = FileFormatBookingXLS.getColumnIndexClientName(sheet.getRow(0));
        final int indexBookingCheckIn = FileFormatBookingXLS.getColumnIndexCheckIn(sheet.getRow(0));
        final int indexBookingCheckOut = FileFormatBookingXLS.getColumnIndexCheckOut(sheet.getRow(0));
        final int indexStatus = FileFormatBookingXLS.getColumnIndexStatus(sheet.getRow(0));
        final List<Integer> bookingNumbers = new ArrayList<>();
        final List<String> guestNames = new ArrayList<>();
        final List<String> stati = new ArrayList<>();
        final List<LocalDate> bookingCheckIn = new ArrayList<>();
        final List<LocalDate> bookingCheckOut = new ArrayList<>();
        for (final Row r : sheet) {
            // skip first row
            if (r.getRowNum() == 0) {
                continue;
            }
            bookingNumbers.add(FileFormatBookingXLS.getBookingNumber(r.getCell(indexBookingNumber)));
            guestNames.add(FileFormatBookingXLS.getString(r.getCell(indexClientName)));
            bookingCheckIn.add(FileFormatBookingXLS.getDate(r.getCell(indexBookingCheckIn)));
            bookingCheckOut.add(FileFormatBookingXLS.getDate(r.getCell(indexBookingCheckOut)));
            stati.add(FileFormatBookingXLS.getString(r.getCell(indexStatus)));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("BookingBean numbers: " + bookingNumbers);
            logger.debug("Guest names: " + guestNames);
            logger.debug("Check-in dates: " + bookingCheckIn);
            logger.debug("Check-out dates: " + bookingCheckOut);
        }
        if (logger.isInfoEnabled()) {
            logger.info("Building bookings.. ");
        }

        for (int i = 0; i < bookingNumbers.size(); i++) {
            final int number = bookingNumbers.get(i);
            final LocalDate checkIn = bookingCheckIn.get(i);
            final LocalDate checkOut = bookingCheckOut.get(i);
            final String names = guestNames.get(i);
            final String status = stati.get(i);
            if (status.equals("ok")) {
                final BookingBeanSer bb = new BookingBeanSer();
                bb.checkInDate = checkIn;
                bb.checkOutDate = checkOut;
                bb.guestName = names;
                bb.externalId = Integer.toString(number);
                bookings.add(bb);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Skipping status " + status);
                }
            }
        }
    } catch (final Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error(e.getLocalizedMessage(), e);
        }
    } finally {
        if (workbook != null) {
            IOUtils.closeQuietly(workbook);
        }
        if (stream != null) {
            IOUtils.closeQuietly(stream);
        }
    }
    return bookings;
}

From source file:com.github.svrtm.xlreport.Cells.java

License:Apache License

/**
 * Finalization of the implementation <code>Cells</code>.
 *
 * @return an instance of the implementation <code>Row</code>
 * @see com.github.svrtm.xlreport.Row#addCells(int)
 * @see com.github.svrtm.xlreport.Row#addCells(int...)
 *///from  w ww . j  a va  2  s  .c o  m
@SuppressWarnings("unchecked")
public TR configureCells() {
    if (columnWidth == -1 && incrementValue == -1 && cellStyle == null)
        return (TR) row;

    int increment = incrementValue;
    for (final int i : indexesCells) {
        if (row.cells.get(i) == null)
            row.prepareNewCell(i).createCell();

        final org.apache.poi.ss.usermodel.Row poiRow = row.poiRow;
        final Cell poiCell = poiRow.getCell(i);
        if (poiCell == null)
            throw new ReportBuilderException(
                    format("A cell of number %d [row:%d] can't be found. Please, create a cell before using it",
                            i, poiRow.getRowNum()));

        if (columnWidth != -1)
            poiCell.getSheet().setColumnWidth(i, columnWidth);
        if (incrementValue != -1)
            poiCell.setCellValue(increment++);
        if (enableAutoSize)
            setAutoSizeColumn(poiCell);

        if (cellStyle != null)
            if (row.cells.get(i).cellStyle == null) {
                // Apply a style to the cell
                final CellStyle poiStyle = cellStyle.getStyle();
                poiCell.setCellStyle(poiStyle);

                final List<Cell> mergedCells = findMergedCells(poiCell);
                if (mergedCells != null)
                    for (final Cell mergedCell : mergedCells)
                        mergedCell.setCellStyle(poiStyle);
            }
    }

    return (TR) row;
}

From source file:com.github.ukase.toolkit.xlsx.CellMerge.java

License:Open Source License

boolean isApplicable(Row row) {
    return isApplicableRow(row.getRowNum()) && isApplicableCell(row.getPhysicalNumberOfCells());
}

From source file:com.github.ukase.toolkit.xlsx.RenderingTable.java

License:Open Source License

private void mergeCells(Row row, Element td, int cellNumber, CellStyle style) {
    int columns = intValue(td.getAttribute(ATTR_COL_SPAN), 1);
    int rows = intValue(td.getAttribute(ATTR_ROW_SPAN), 1);
    if (columns == 1 && rows == 1) {
        return;/*from   w  w w . j a  va  2 s . c  om*/
    }

    CellMerge merge = new CellMerge(cellNumber, columns, row.getRowNum(), rows, style);
    merge.apply(sheet);
    merge.fillRow(row);
    mergedCells.add(merge);
}

From source file:com.googlecode.testcase.annotation.handle.toexcel.strategy.ToHtmlWithExcel.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads(sheet);//  ww w  .j  a  v  a  2s .c o m

    out.format("<tbody>%n");
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();
        out.format("  <tr>%n");
        out.format("    <td class=%s>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
        for (int i = firstColumn; i < endColumn; i++) {
            String content = "&nbsp;";
            String attrs = "";
            CellStyle style = null;
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    style = cell.getCellStyle();
                    attrs = tagStyle(cell, style);
                    //Set the value that is rendered for the cell
                    //also applies the format
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;
                    if (content.equals(""))
                        content = "&nbsp;";
                }
            }
            out.format("    <td class=%s %s>%s</td>%n", styleName(style), attrs, content);
        }
        out.format("  </tr>%n");
    }
    out.format("</tbody>%n");
}