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

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

Introduction

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

Prototype

CellStyle getRowStyle();

Source Link

Document

Returns the whole-row cell styles.

Usage

From source file:com.runwaysdk.dataaccess.io.excel.ErrorSheet.java

License:Open Source License

public void addRow(Row _row) {
    Row row = this.errorSheet.createRow(count++);
    row.setZeroHeight(_row.getZeroHeight());
    row.setHeight(_row.getHeight());// w  ww . j a va  2  s.co  m

    CellStyle style = _row.getRowStyle();

    if (style != null) {
        Workbook workbook = row.getSheet().getWorkbook();

        CellStyle clone = workbook.createCellStyle();
        clone.cloneStyleFrom(style);

        row.setRowStyle(clone);
    }

    Iterator<Cell> cellIterator = _row.cellIterator();
    while (cellIterator.hasNext()) {
        Cell oldCell = cellIterator.next();
        Cell newCell = row.createCell(oldCell.getColumnIndex());

        int cellType = oldCell.getCellType();

        if (cellType == Cell.CELL_TYPE_FORMULA) {
            cellType = oldCell.getCachedFormulaResultType();
        }

        switch (cellType) {
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java

License:Open Source License

public void reloadActiveSheetColumnRowStyles() {
    final Workbook workbook = spreadsheet.getWorkbook();

    if (spreadsheet.getState().rowIndexToStyleIndex == null) {
        spreadsheet.getState().rowIndexToStyleIndex = new HashMap<Integer, Integer>(
                workbook.getNumCellStyles());
    } else {/*from  w  w  w  . j a  v  a  2  s  . c o m*/
        spreadsheet.getState().rowIndexToStyleIndex.clear();
    }
    if (spreadsheet.getState().columnIndexToStyleIndex == null) {
        spreadsheet.getState().columnIndexToStyleIndex = new HashMap<Integer, Integer>(
                workbook.getNumCellStyles());
    } else {
        spreadsheet.getState().columnIndexToStyleIndex.clear();
    }
    if (spreadsheet.getState().lockedColumnIndexes == null) {
        spreadsheet.getState().lockedColumnIndexes = new HashSet<Integer>();
    } else {
        spreadsheet.getState().lockedColumnIndexes.clear();
    }
    if (spreadsheet.getState().lockedRowIndexes == null) {
        spreadsheet.getState().lockedRowIndexes = new HashSet<Integer>();
    } else {
        spreadsheet.getState().lockedRowIndexes.clear();
    }

    Sheet activeSheet = spreadsheet.getActiveSheet();
    for (int i = 0; i < spreadsheet.getRows(); i++) {
        Row row = activeSheet.getRow(i);
        if (row != null && row.getRowStyle() != null) {
            int styleIndex = row.getRowStyle().getIndex();
            spreadsheet.getState().rowIndexToStyleIndex.put(i + 1, styleIndex);
            if (row.getRowStyle().getLocked()) {
                spreadsheet.getState().lockedRowIndexes.add(i + 1);
            }
        } else {
            if (spreadsheet.isActiveSheetProtected()) {
                spreadsheet.getState().lockedRowIndexes.add(i + 1);
            }
        }
    }

    for (int i = 0; i < spreadsheet.getColumns(); i++) {
        if (activeSheet.getColumnStyle(i) != null) {
            int styleIndex = activeSheet.getColumnStyle(i).getIndex();
            spreadsheet.getState().columnIndexToStyleIndex.put(i + 1, styleIndex);
            if (activeSheet.getColumnStyle(i).getLocked()) {
                spreadsheet.getState().lockedColumnIndexes.add(i + 1);
            }
        }
    }
}

From source file:mpqq.MPQQ.java

private static XSSFWorkbook procTab1(XSSFWorkbook referenceWB, XSSFWorkbook mpqqWB, int referenceFirstRow) {

    XSSFSheet trackerTab = referenceWB.getSheetAt(USE_FOR_TAB1);
    XSSFSheet tab1 = mpqqWB.getSheetAt(1);
    //Iterator<Row> rowIterator = trackerTab.iterator();
    DataFormatter df = new DataFormatter();

    //MPQQ first row    
    int rowIdx = 11;

    for (int refCurRow = referenceFirstRow; refCurRow <= trackerTab.getLastRowNum(); refCurRow++) {
        Row row = trackerTab.getRow(refCurRow);

        //Check if row is visible
        if (!row.getZeroHeight() || (row.isFormatted() && row.getRowStyle().getHidden())) {

            int colIdx = 1;
            //Iterate trough the Columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                switch (cell.getColumnIndex()) {
                case 3:
                    Cell currentCell = checkRowCellExists(tab1, rowIdx, colIdx);
                    currentCell.setCellValue(df.formatCellValue(row.getCell(T2PEPSICO_STOCK_CODE)));

                    //Go to next Column
                    colIdx++;/*from   ww  w .  j  a v a  2  s .c  o m*/
                    break;
                case 4:

                    break;
                default:
                }
            }
            //Jump Next Row
            rowIdx++;
        }
    }
    return mpqqWB;
}

From source file:org.hellojavaer.poi.excel.utils.ExcelUtils.java

License:Apache License

private static InnerRow getTemplateRow(Map<Integer, InnerRow> cache, Sheet sheet,
        ExcelWriteSheetProcessor<?> sheetProcessor, int rowIndex) {
    InnerRow cachedRow = cache.get(rowIndex);
    if (cachedRow != null || cache.containsKey(rowIndex)) {
        return cachedRow;
    }// ww  w. j a  v  a  2s.  co m
    InnerRow templateRow = null;
    if (sheetProcessor.getTemplateStartRowIndex() != null && sheetProcessor.getTemplateEndRowIndex() != null) {
        if (rowIndex <= sheetProcessor.getTemplateEndRowIndex()) {
            return null;
        }
        int tempRowIndex = (rowIndex - sheetProcessor.getTemplateEndRowIndex() - 1)
                % (sheetProcessor.getTemplateEndRowIndex() - sheetProcessor.getTemplateStartRowIndex() + 1)
                + sheetProcessor.getTemplateStartRowIndex();
        Row tempRow = sheet.getRow(tempRowIndex);
        if (tempRow != null) {
            templateRow = new InnerRow();
            templateRow.setHeight(tempRow.getHeight());
            templateRow.setHeightInPoints(tempRow.getHeightInPoints());
            templateRow.setRowStyle(tempRow.getRowStyle());
            templateRow.setZeroHeight(tempRow.getZeroHeight());
            for (int i = tempRow.getFirstCellNum(); i <= tempRow.getLastCellNum(); i++) {
                Cell cell = tempRow.getCell(i);
                if (cell != null) {
                    InnerCell innerCell = new InnerCell();
                    innerCell.setCellStyle(cell.getCellStyle());
                    innerCell.setCellType(cell.getCellType());
                    templateRow.setCell(i, innerCell);
                }
            }
        }
    }
    cache.put(rowIndex, templateRow);
    return templateRow;
}

From source file:org.openmrs.module.mksreports.renderer.PatientHistoryExcelTemplateRenderer.java

License:Open Source License

/**
 * Adds in a Row to the given Sheet//  w ww  .j av  a  2 s .c om
 */
public Row addRow(Workbook wb, SheetToAdd sheetToAdd, RowToAdd rowToAdd, int rowIndex, ReportData reportData,
        ReportDesign design, Map<String, String> repeatSections) {

    // Create a new row and copy over style attributes from the row to add
    Row newRow = sheetToAdd.getSheet().createRow(rowIndex);
    Row rowToClone = rowToAdd.getRowToClone();
    try {
        CellStyle rowStyle = rowToClone.getRowStyle();
        if (rowStyle != null) {
            newRow.setRowStyle(rowStyle);
        }
    } catch (Exception e) {
        // No idea why this is necessary, but this has thrown IndexOutOfBounds errors getting the rowStyle.  Mysteries of POI
    }
    newRow.setHeight(rowToClone.getHeight());

    // Iterate across all of the cells in the row, and configure all those that need to be added/cloned
    List<CellToAdd> cellsToAdd = new ArrayList<CellToAdd>();

    int totalCells = rowToClone.getPhysicalNumberOfCells();
    int cellsFound = 0;
    for (int cellNum = 0; cellsFound < totalCells; cellNum++) {
        Cell currentCell = rowToClone.getCell(cellNum);
        log.debug("Handling cell: " + currentCell);
        if (currentCell != null) {
            cellsFound++;
        }
        // If we find that the cell that we are on is a repeating cell, then add the appropriate number of cells to clone
        String repeatingColumnProperty = getRepeatingColumnProperty(sheetToAdd.getOriginalSheetNum(), cellNum,
                repeatSections);
        if (repeatingColumnProperty != null) {
            String[] dataSetSpanSplit = repeatingColumnProperty.split(",");
            String dataSetName = dataSetSpanSplit[0];
            DataSet dataSet = getDataSet(reportData, dataSetName, rowToAdd.getReplacementData());
            int numCellsToRepeat = 1;
            if (dataSetSpanSplit.length == 2) {
                numCellsToRepeat = Integer.parseInt(dataSetSpanSplit[1]);
            }
            log.debug("Repeating this cell with dataset: " + dataSet + " and repeat of " + numCellsToRepeat);
            int repeatNum = 0;
            for (DataSetRow dataSetRow : dataSet) {
                repeatNum++;
                for (int i = 0; i < numCellsToRepeat; i++) {
                    Cell cell = (i == 0 ? currentCell : rowToClone.getCell(cellNum + i));
                    if (repeatNum == 1 && cell != null && cell != currentCell) {
                        cellsFound++;
                    }
                    Map<String, Object> newReplacements = getReplacementData(rowToAdd.getReplacementData(),
                            reportData, design, dataSetName, dataSetRow, repeatNum);
                    cellsToAdd.add(new CellToAdd(cell, newReplacements));
                    log.debug("Adding " + cell + " with dataSetRow: " + dataSetRow);
                }
            }
            cellNum += numCellsToRepeat;
        } else {
            cellsToAdd.add(new CellToAdd(currentCell, rowToAdd.getReplacementData()));
            log.debug("Adding " + currentCell);
        }
    }

    // Now, go through all of the collected cells, and add them back in

    String prefix = getExpressionPrefix(design);
    String suffix = getExpressionSuffix(design);

    List<CellRangeAddress> newMergedRegions = new ArrayList<CellRangeAddress>();

    for (int i = 0; i < cellsToAdd.size(); i++) {
        CellToAdd cellToAdd = cellsToAdd.get(i);
        Cell newCell = newRow.createCell(i);
        Cell cellToClone = cellToAdd.getCellToClone();
        if (cellToClone != null) {
            Object contents = ExcelUtil.getCellContents(cellToClone);
            newCell.setCellStyle(cellToClone.getCellStyle());

            int numFormattings = sheetToAdd.getSheet().getSheetConditionalFormatting()
                    .getNumConditionalFormattings();
            for (int n = 0; n < numFormattings; n++) {
                ConditionalFormatting f = sheetToAdd.getSheet().getSheetConditionalFormatting()
                        .getConditionalFormattingAt(n);
                for (CellRangeAddress add : f.getFormattingRanges()) {

                    if (add.getFirstRow() == rowToAdd.getRowToClone().getRowNum()
                            && add.getLastRow() == rowToClone.getRowNum()) {
                        if (add.getFirstColumn() == cellToClone.getColumnIndex()
                                && add.getLastColumn() == cellToClone.getColumnIndex()) {
                            ConditionalFormattingRule[] rules = new ConditionalFormattingRule[f
                                    .getNumberOfRules()];
                            for (int j = 0; j < f.getNumberOfRules(); j++) {
                                rules[j] = f.getRule(j);
                            }
                            CellRangeAddress[] cellRange = new CellRangeAddress[1];
                            cellRange[0] = new CellRangeAddress(rowIndex, rowIndex, i, i);
                            sheetToAdd.getSheet().getSheetConditionalFormatting()
                                    .addConditionalFormatting(cellRange, rules);
                        }
                    }
                }
            }

            int numMergedRegions = sheetToAdd.getSheet().getNumMergedRegions();
            for (int n = 0; n < numMergedRegions; n++) {
                CellRangeAddress add = sheetToAdd.getSheet().getMergedRegion(n);
                int rowNum = rowToClone.getRowNum();
                if (add.getFirstRow() == rowNum && add.getLastRow() == rowNum) {
                    if (add.getFirstColumn() == cellToClone.getColumnIndex()) {
                        newMergedRegions
                                .add(new CellRangeAddress(rowNum, rowNum, i, i + add.getNumberOfCells() - 1));
                    }
                }
            }

            if (ObjectUtil.notNull(contents)) {
                if (contents instanceof String) {
                    contents = EvaluationUtil.evaluateExpression(contents.toString(),
                            cellToAdd.getReplacementData(), prefix, suffix);
                }
                ExcelUtil.setCellContents(newCell, contents);
            }

            ExcelUtil.copyFormula(cellToClone, newCell);
        }
    }

    for (CellRangeAddress mergedRegion : newMergedRegions) {
        sheetToAdd.getSheet().addMergedRegion(mergedRegion);
    }

    return newRow;
}

From source file:org.opentestsystem.delivery.testreg.upload.RowMetadataImpl.java

License:Open Source License

public RowMetadataImpl(final Row row) {
    // Excel Rows are 0 based so add 1
    this(row.getRowNum() + 1, row.getRowStyle() != null ? row.getRowStyle().toString() : null);
}