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

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

Introduction

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

Prototype

short getHeight();

Source Link

Document

Get the row's height measured in twips (1/20th of a point).

Usage

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

License:Open Source License

/**
 * Adds in a Row to the given Sheet//from  www  .  j  a v  a2s . c o  m
 */
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.tiefaces.components.websheet.service.WebSheetLoader.java

License:MIT License

/**
 * Setup row info.//from ww w .  j  av  a2 s  .c  o m
 *
 * @param facesRow
 *            the faces row
 * @param sheet1
 *            the sheet 1
 * @param row
 *            the row
 * @param rowIndex
 *            the row index
 * @param allowAdd
 *            the allow add
 */
private void setupRowInfo(final FacesRow facesRow, final Sheet sheet1, final Row row, final int rowIndex,
        final boolean allowAdd) {

    facesRow.setAllowAdd(allowAdd);
    if (row != null) {
        facesRow.setRendered(!row.getZeroHeight());
        facesRow.setRowheight(row.getHeight());
        int rowNum = ConfigurationUtility.getOriginalRowNumInHiddenColumn(row);
        facesRow.setOriginRowIndex(rowNum);
    } else {
        facesRow.setRendered(true);
        facesRow.setRowheight(sheet1.getDefaultRowHeight());
        facesRow.setOriginRowIndex(rowIndex);
    }

}

From source file:org.tiefaces.components.websheet.utility.CellUtility.java

License:MIT License

/**
 * Copy single row.//from   w w w.j  a  v  a  2s  . c o  m
 *
 * @param srcSheet
 *            the src sheet
 * @param destSheet
 *            the dest sheet
 * @param sourceRowNum
 *            the source row num
 * @param destinationRowNum
 *            the destination row num
 * @param checkLock
 *            the check lock
 * @param setHiddenColumn
 *            the set hidden column
 */
private static void copySingleRow(final Sheet srcSheet, final Sheet destSheet, final int sourceRowNum,
        final int destinationRowNum, final boolean checkLock, final boolean setHiddenColumn) {
    // Get the source / new row
    Row newRow = destSheet.getRow(destinationRowNum);
    Row sourceRow = srcSheet.getRow(sourceRowNum);

    if (newRow == null) {
        newRow = destSheet.createRow(destinationRowNum);
    }
    newRow.setHeight(sourceRow.getHeight());
    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        copyCell(destSheet, sourceRow, newRow, i, checkLock);
    }
    if (setHiddenColumn) {
        ConfigurationUtility.setOriginalRowNumInHiddenColumn(newRow, sourceRow.getRowNum());
    }
    return;

}

From source file:poi.hssf.view.SVSheetTable.java

License:Apache License

public SVSheetTable(HSSFSheet sheet) {
    super(new SVTableModel(sheet));
    this.sheet = sheet;

    setIntercellSpacing(new Dimension(0, 0));
    setAutoResizeMode(AUTO_RESIZE_OFF);/*from w  w w  . j a  v a2 s .  c  o  m*/
    JTableHeader header = getTableHeader();
    header.setDefaultRenderer(new HeaderCellRenderer());
    pendingPaintings = new PendingPaintings(this);

    //Set the columns the correct size
    TableColumnModel columns = getColumnModel();
    for (int i = 0; i < columns.getColumnCount(); i++) {
        TableColumn column = columns.getColumn(i);
        int width = sheet.getColumnWidth(i);
        //256 is because the width is in 256ths of a character
        column.setPreferredWidth(width / 256 * magicCharFactor);
    }

    Toolkit t = getToolkit();
    int res = t.getScreenResolution();
    TableModel model = getModel();
    for (int i = 0; i < model.getRowCount(); i++) {
        Row row = sheet.getRow(i - sheet.getFirstRowNum());
        if (row != null) {
            short h = row.getHeight();
            int height = Math.round(Math.max(1, h / (res / 70 * 20) + 3));
            System.out.printf("%d: %d (%d @ %d)%n", i, height, h, res);
            setRowHeight(i, height);
        }
    }

    addHierarchyListener(new HierarchyListener() {
        public void hierarchyChanged(HierarchyEvent e) {
            if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) != 0) {
                Container changedParent = e.getChangedParent();
                if (changedParent instanceof JViewport) {
                    Container grandparent = changedParent.getParent();
                    if (grandparent instanceof JScrollPane) {
                        JScrollPane jScrollPane = (JScrollPane) grandparent;
                        setupScroll(jScrollPane);
                    }
                }
            }
        }
    });
}