Example usage for org.apache.poi.hssf.usermodel HSSFRow getHeightInPoints

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow getHeightInPoints

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFRow getHeightInPoints.

Prototype


@Override
public float getHeightInPoints() 

Source Link

Document

get the row's height or ff (-1) for undefined/default-height in points (20*getHeight())

Usage

From source file:org.jxstar.report.util.ReportXlsUtil.java

/**
 * //from ww w .j av a  2s . co  m
 * @param mainSheet -- ?
 * @param subSheet -- ?
 * @param tempRow -- ?????
 * @return
 */
public static HSSFSheet appendSheet(HSSFSheet mainSheet, HSSFSheet subSheet, int tempRow) {
    if (mainSheet == null || subSheet == null)
        return null;
    //??
    if (!isAllowOut(mainSheet))
        return mainSheet;
    //?
    int endRowNum = mainSheet.getPhysicalNumberOfRows();

    HSSFRow sourow = null, descrow = null;
    HSSFCell sourcell = null, descell = null, orgcell = null;
    int i = 0, offsetcnt = 0;

    //?
    copySheetImage(mainSheet.getWorkbook(), subSheet.getWorkbook());

    //??
    CellRangeAddress range = null;
    int mergedNum = subSheet.getNumMergedRegions();
    for (i = 0; i < mergedNum; i++) {
        range = subSheet.getMergedRegion(i);
        range.setFirstRow(range.getFirstRow() + endRowNum);
        range.setLastRow(range.getLastRow() + endRowNum);
        mainSheet.addMergedRegion(range);
    }
    range = null;
    //int k = 0;

    //?
    mainSheet.setAlternativeExpression(subSheet.getAlternateExpression());
    mainSheet.setAlternativeFormula(subSheet.getAlternateFormula());
    mainSheet.setAutobreaks(subSheet.getAutobreaks());
    mainSheet.setDialog(subSheet.getDialog());
    mainSheet.setDisplayGuts(subSheet.getDisplayGuts());
    mainSheet.setFitToPage(subSheet.getFitToPage());

    for (java.util.Iterator<Row> iterow = subSheet.rowIterator(); iterow.hasNext();) {
        sourow = (HSSFRow) iterow.next();
        offsetcnt = sourow.getRowNum() + endRowNum;
        descrow = mainSheet.createRow(offsetcnt);
        descrow.setHeight(sourow.getHeight());
        descrow.setHeightInPoints(sourow.getHeightInPoints());

        java.util.Iterator<Cell> iter = sourow.cellIterator();
        while (iter.hasNext()) {
            sourcell = (HSSFCell) iter.next();
            int column = sourcell.getColumnIndex();
            descell = descrow.createCell(column);

            /**
             * ??????orgcell = mainSheet.getRow(row).getCell(column);
             * ??
             * ??orgcell.getCellStyle()????sheet??
             * This Style does not belong to the supplied Workbook.
             * ?descell.getCellStyle().cloneStyleFrom(sourcell.getCellStyle());???excel
             * HSSFCellStyle cs = mainSheet.getWorkbook().createCellStyle();
             * cs.cloneStyleFrom(sourcell.getCellStyle());
             * descell.setCellStyle(cs);//excel?
             * tempRow????
             */

            //????????
            int row = sourcell.getRowIndex();
            if (tempRow > 0 && row > tempRow) {
                row = tempRow;
            }
            orgcell = mainSheet.getRow(row).getCell(column);
            if (orgcell != null) {
                //orgcell.getCellType()???0
                descell.setCellType(HSSFCell.CELL_TYPE_STRING);
                //???
                descell.setCellStyle(orgcell.getCellStyle());
            } else {
                _log.showWarn("module xls [{0}, {1}] cell is null!", row, column);
            }

            if (sourcell.getCellType() == HSSFCell.CELL_TYPE_STRING)
                descell.setCellValue(sourcell.getStringCellValue());
            else if (sourcell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
                descell.setCellValue(sourcell.getNumericCellValue());
        }
        sourow = null;
        sourcell = null;
        descrow = null;
        orgcell = null;
    }

    return mainSheet;
}

From source file:org.jxstar.report.util.ReportXlsUtil.java

/**
 * ?PageSize?/* w w  w. ja  v a2s  .com*/
 * @param sheet -- ?
 * @param startRow -- ???PageSize
 * @param rows -- ?
 * @return
 */
public static HSSFSheet insertSheetRow(HSSFSheet sheet, int startRow, int rows) {
    if (sheet == null)
        return null;
    sheet.shiftRows(startRow, sheet.getLastRowNum(), rows, true, false);

    HSSFCell sourcell = null, descell = null;
    HSSFRow sourow = sheet.getRow(startRow - 1);

    for (int i = 0; i < rows; i++) {
        HSSFRow descrow = sheet.createRow(startRow + i);

        descrow.setHeight(sourow.getHeight());
        descrow.setHeightInPoints(sourow.getHeightInPoints());

        java.util.Iterator<Cell> iter = sourow.cellIterator();
        while (iter.hasNext()) {
            sourcell = (HSSFCell) iter.next();
            int column = sourcell.getColumnIndex();
            descell = descrow.createCell(column);

            descell.setCellType(sourcell.getCellType());
            descell.setCellStyle(sourcell.getCellStyle());
        }
    }
    //??
    insertSheetRegions(sheet, startRow, rows);

    return sheet;
}

From source file:org.jxstar.report.util.XlsToHtml.java

public String parserXls(String fileName) throws ReportException {
    ////  w w  w  .j a  va  2s .  c  o  m
    HSSFWorkbook hssfWB = ReportXlsUtil.readWorkBook(fileName);
    if (hssfWB == null) {//"?{0}??"
        throw new ReportException(JsMessage.getValue("xlstohtml.hint01"), fileName);
    }

    HSSFSheet sheet = hssfWB.getSheetAt(0);
    //???0
    int lastRowNum = sheet.getLastRowNum();
    if (lastRowNum == 0) {
        _log.showDebug("xls file row num is 0!!");
        return "";
    }

    //?xls?tablehtml
    StringBuilder sbTable = new StringBuilder();
    sbTable.append("<table id='" + PARSERTABLEID + "' class='xls_table' >\n");

    //?
    List<Integer> lsemp = FactoryUtil.newList();

    //?1
    int hasnum = 0, tableColNum = 0;

    //??1IE1?
    //table-layout:fixed;??????1??
    List<Integer> lswidth = FactoryUtil.newList();

    //??
    for (int i = 0; i <= lastRowNum; i++) {
        HSSFRow row = sheet.getRow(i);
        if (row == null) {//??
            lsemp.add(i);
            sbTable.append("<tr height='22px' >\n{EMPTY_LINE}</tr>\n");
            continue;
        }

        //???
        int lastCellNum = row.getLastCellNum();

        //cells-1?
        if (lastCellNum <= 0) {
            lsemp.add(i);
            sbTable.append("<tr height='22px' >\n{EMPTY_LINE}</tr>\n");
            continue;
        } else {
            //?
            if (hasnum == 0)
                tableColNum = lastCellNum + EMPTY_COLNUM;
            hasnum++;
        }

        //tr
        sbTable.append("<tr height='" + getHeightPixel(row.getHeightInPoints()) + "px' >\n");
        //_log.showDebug("row=" + i + "; nums=" + cells);
        for (int j = 0; j < tableColNum; j++) {
            HSSFCell cell = null;
            if (j < lastCellNum)
                cell = row.getCell(j);
            //?ID
            String tdid = i + "," + j;

            //
            if (cell == null) {
                String ls = "";
                if (hasnum == 1) {
                    //?30px
                    int width = (j < lastCellNum) ? 10 : 30;
                    ls = " style='width:" + width + "px;'";
                    lswidth.add(width);
                }
                sbTable.append("\t<td id='" + tdid + "' class='xls_emp'" + ls + " >&nbsp;</td>\n");

                continue;
            }

            //td?
            String style = getCellStyle(cell);

            //???cssName
            String cssName = getTdCss(style);

            //?td
            String value = getCellValue(cell);
            if (value == null || value.length() == 0) {
                value = "&nbsp;";
            } else {
                value = value.replaceAll("\\s", "&nbsp;");
            }

            //1?????
            if (hasnum == 1) {
                //
                int colw = getWidthPixel(sheet.getColumnWidth(j));
                lswidth.add(colw);
                //td
                sbTable.append("\t<td id='" + tdid + "' class='" + cssName + "' style='width:" + colw + "px;' >"
                        + value + "</td>\n");
            } else {
                sbTable.append("\t<td id='" + tdid + "' class='" + cssName + "' >" + value + "</td>\n");
            }
        }
        sbTable.append("</tr>\n");
    }
    //_log.showDebug(sbTable.toString());
    //??
    for (int i = 1; i <= EMPTY_ROWNUM; i++) {
        lsemp.add(lastRowNum + i);
        //tr
        sbTable.append("<tr height='22px' >\n");
        sbTable.append("{EMPTY_LINE}");
        sbTable.append("</tr>\n");
    }

    //cells-1?
    if (tableColNum > 0 && lsemp.size() > 0) {
        sbTable = fillEmptyLine(sbTable, tableColNum, lsemp, lswidth);
        lsemp.clear();
    }

    sbTable.append("</table>\n");

    //?html
    StringBuilder sbHtml = new StringBuilder();
    sbHtml.append("<html>\n<body style='margin:1px;'>\n");
    sbHtml.append(getCssStyle());
    sbHtml.append(sbTable);

    //???
    sbHtml.append(mergedRegion(sheet));

    //html
    sbHtml.append("</body>\n</html>\n");
    //_log.showDebug(sbHtml.toString());

    return sbHtml.toString();
}

From source file:org.unitime.timetable.export.XLSPrinter.java

License:Apache License

private ClientAnchorDetail fitImageToRows(HSSFSheet sheet, int rowNumber, double reqImageHeightMM,
        int resizeBehaviour) {
    double rowCoordinatesPerMM;
    int pictureHeightCoordinates;
    ClientAnchorDetail rowClientAnchorDetail = null;

    HSSFRow row = sheet.getRow(rowNumber);
    if (row == null) {
        row = sheet.createRow(rowNumber);
    }//w w  w .  j a va2s . co  m

    double rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;

    if (rowHeightMM < reqImageHeightMM) {
        if (resizeBehaviour == EXPAND_ROW || resizeBehaviour == EXPAND_ROW_AND_COLUMN) {
            row.setHeightInPoints((float) (reqImageHeightMM * ConvertImageUnits.POINTS_PER_MILLIMETRE));
            rowHeightMM = reqImageHeightMM;
            rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
            pictureHeightCoordinates = (int) (reqImageHeightMM * rowCoordinatesPerMM);
            rowClientAnchorDetail = new ClientAnchorDetail(rowNumber, rowNumber, pictureHeightCoordinates);
        } else if (resizeBehaviour == OVERLAY_ROW_AND_COLUMN || resizeBehaviour == EXPAND_COLUMN) {
            rowClientAnchorDetail = calculateRowLocation(sheet, rowNumber, reqImageHeightMM);
        }
    } else {
        rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
        pictureHeightCoordinates = (int) (reqImageHeightMM * rowCoordinatesPerMM);
        rowClientAnchorDetail = new ClientAnchorDetail(rowNumber, rowNumber, pictureHeightCoordinates);
    }

    return rowClientAnchorDetail;
}

From source file:org.unitime.timetable.export.XLSPrinter.java

License:Apache License

private ClientAnchorDetail calculateRowLocation(HSSFSheet sheet, int startingRow, double reqImageHeightMM) {
    ClientAnchorDetail clientAnchorDetail;
    HSSFRow row;
    double rowHeightMM = 0.0D;
    double totalRowHeightMM = 0.0D;
    double overlapMM;
    double rowCoordinatesPerMM;
    int toRow = startingRow;
    int inset;//from w w  w . j  ava2s.  co  m

    while (totalRowHeightMM < reqImageHeightMM) {
        row = sheet.getRow(toRow);
        if (row == null) {
            row = sheet.createRow(toRow);
        }
        rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;
        totalRowHeightMM += rowHeightMM;
        toRow++;
    }
    toRow--;

    if ((int) totalRowHeightMM == (int) reqImageHeightMM) {
        clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow,
                ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS);
    } else {
        overlapMM = reqImageHeightMM - (totalRowHeightMM - rowHeightMM);
        if (overlapMM < 0) {
            overlapMM = 0.0D;
        }
        rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
        inset = (int) (overlapMM * rowCoordinatesPerMM);
        clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow, inset);
    }

    return clientAnchorDetail;
}

From source file:poi.hssf.usermodel.examples.AddDimensionedImage.java

License:Apache License

/**
 * Determines whether the sheet's row should be re-sized to accomodate
 * the image, adjusts the rows height if necessary and creates then
 * returns a ClientAnchorDetail object that facilitates construction of
 * an HSSFClientAnchor that will fix the image on the sheet and establish
 * it's size.//from  ww  w.  j  av  a  2 s . c o m
 *
 * @param sheet A reference to the sheet that will 'contain' the image.
 * @param rowNumber A primtive int that contains the index number of a
 *                  row on the sheet.
 * @param reqImageHeightMM A primtive double that contains the required
 *                         height of the image in millimetres
 * @param resizeBehaviour A primitve int whose value will indicate how the
 *                        height of the row should be adjusted if the
 *                        required height of the image is greater than the
 *                        height of the row.
 * @return An instance of the ClientAnchorDetail class that will contain
 *         the index number of the row containing the cell whose top
 *         left hand corner also defines the top left hand corner of the
 *         image, the index number of the row containing the cell whose
 *         top left hand corner also defines the bottom right hand
 *         corner of the image and an inset that determines how far the
 *         bottom edge of the image can protrude into the next (lower)
 *         row - expressed as a specific number of co-ordinate positions.
 */
private ClientAnchorDetail fitImageToRows(HSSFSheet sheet, int rowNumber, double reqImageHeightMM,
        int resizeBehaviour) {
    HSSFRow row = null;
    double rowHeightMM = 0.0D;
    double rowCoordinatesPerMM = 0.0D;
    int pictureHeightCoordinates = 0;
    ClientAnchorDetail rowClientAnchorDetail = null;

    // Get the row and it's height
    row = sheet.getRow(rowNumber);
    if (row == null) {
        // Create row if it does not exist.
        row = sheet.createRow(rowNumber);
    }

    // Get the row's height in millimetres
    rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;

    // Check that the row's height will accomodate the image at the required
    // dimensions. If the height of the row is LESS than the required height
    // of the image, decide how the application should respond - resize the
    // row or overlay the image across a series of rows.
    if (rowHeightMM < reqImageHeightMM) {
        if ((resizeBehaviour == AddDimensionedImage.EXPAND_ROW)
                || (resizeBehaviour == AddDimensionedImage.EXPAND_ROW_AND_COLUMN)) {
            row.setHeightInPoints((float) (reqImageHeightMM * ConvertImageUnits.POINTS_PER_MILLIMETRE));
            rowHeightMM = reqImageHeightMM;
            rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
            pictureHeightCoordinates = (int) (reqImageHeightMM * rowCoordinatesPerMM);
            rowClientAnchorDetail = new ClientAnchorDetail(rowNumber, rowNumber, pictureHeightCoordinates);
        }
        // If the user has chosen to overlay both rows and columns or just
        // to expand ONLY the size of the columns, then calculate how to lay
        // the image out ver one or more rows.
        else if ((resizeBehaviour == AddDimensionedImage.OVERLAY_ROW_AND_COLUMN)
                || (resizeBehaviour == AddDimensionedImage.EXPAND_COLUMN)) {
            rowClientAnchorDetail = this.calculateRowLocation(sheet, rowNumber, reqImageHeightMM);
        }
    }
    // Else, if the image is smaller than the space available
    else {
        rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
        pictureHeightCoordinates = (int) (reqImageHeightMM * rowCoordinatesPerMM);
        rowClientAnchorDetail = new ClientAnchorDetail(rowNumber, rowNumber, pictureHeightCoordinates);
    }
    return (rowClientAnchorDetail);
}

From source file:poi.hssf.usermodel.examples.AddDimensionedImage.java

License:Apache License

/**
 * If the image is to overlie more than one rows, calculations need to be
 * performed to determine how many rows and whether the image will
 * overlie just a part of one row in order to be presented at the
 * required size.//from   www.  j  av a  2s .c o  m
 *
 * @param sheet The sheet that will 'contain' the image.
 * @param startingRow A primitive int whose value is the index of the row
 *                    that contains the cell whose top left hand corner
 *                    should be aligned with the top left hand corner of
 *                    the image.
 * @param reqImageHeightMM A primitive double whose value will indicate the
 *                         required height of the image in millimetres.
 * @return An instance of the ClientAnchorDetail class that will contain
 *         the index number of the row containing the cell whose top
 *         left hand corner also defines the top left hand corner of the
 *         image, the index number of the row containing the cell whose top
 *         left hand corner also defines the bottom right hand corner of
 *         the image and an inset that determines how far the bottom edge
 *         can protrude into the next (lower) row - expressed as a specific
 *         number of co-ordinate positions.
 */
private ClientAnchorDetail calculateRowLocation(HSSFSheet sheet, int startingRow, double reqImageHeightMM) {
    ClientAnchorDetail clientAnchorDetail = null;
    HSSFRow row = null;
    double rowHeightMM = 0.0D;
    double totalRowHeightMM = 0.0D;
    double overlapMM = 0.0D;
    double rowCoordinatesPerMM = 0.0D;
    int toRow = startingRow;
    int inset = 0;

    // Step through the rows in the sheet and accumulate a total of their
    // heights.
    while (totalRowHeightMM < reqImageHeightMM) {
        row = sheet.getRow(toRow);
        // Note, if the row does not already exist on the sheet then create
        // it here.
        if (row == null) {
            row = sheet.createRow(toRow);
        }
        // Get the row's height in millimetres and add to the running total.
        rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;
        totalRowHeightMM += rowHeightMM;
        toRow++;
    }
    // Owing to the way the loop above works, the rowNumber will have been
    // incremented one row too far. Undo that here.
    toRow--;
    // Check to see whether the image should occupy an exact number of
    // rows. If so, build the ClientAnchorDetail record to point
    // to those rows and with an inset of the total number of co-ordinate
    // position in the row.
    //
    // To overcome problems that can occur with comparing double values for
    // equality, cast both to int(s) to truncate the value; VERY crude and
    // I do not really like it!!
    if ((int) totalRowHeightMM == (int) reqImageHeightMM) {
        clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow,
                ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS);
    } else {
        // Calculate how far the image will project into the next row. Note
        // that the height of the last row assessed is subtracted from the
        // total height of all rows assessed so far.
        overlapMM = reqImageHeightMM - (totalRowHeightMM - rowHeightMM);

        // To prevent an exception being thrown when the required width of
        // the image is very close indeed to the column size.
        if (overlapMM < 0) {
            overlapMM = 0.0D;
        }

        rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
        inset = (int) (overlapMM * rowCoordinatesPerMM);
        clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow, inset);
    }
    return (clientAnchorDetail);
}

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

License:Apache License

protected JComponent makeSheetView(HSSFSheet sheet) {
    JTable sheetView = new JTable(new SVTableModel(sheet));
    sheetView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    sheetView.setDefaultRenderer(HSSFCell.class, cellRenderer);
    if (allowEdits)
        sheetView.setDefaultEditor(HSSFCell.class, cellEditor);
    JTableHeader header = sheetView.getTableHeader();
    //Dont allow column reordering
    header.setReorderingAllowed(false);/*  w  ww .j a v  a2 s.c  o  m*/
    //Only allow column resizing if editing is allowed
    header.setResizingAllowed(allowEdits);

    //Set the columns the correct size
    TableColumnModel columns = sheetView.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);
    }

    //Set the rows to the correct size
    int rows = sheet.getPhysicalNumberOfRows();
    Insets insets = cellRenderer.getInsets();
    //Need to include the insets in the calculation of the row height to use.
    int extraHeight = insets.bottom + insets.top;
    for (int i = 0; i < rows; i++) {
        HSSFRow row = sheet.getRow(i);
        if (row == null) {
            sheetView.setRowHeight(i, (int) sheet.getDefaultRowHeightInPoints() + extraHeight);
        } else {
            sheetView.setRowHeight(i, (int) row.getHeightInPoints() + extraHeight);
        }
    }

    //Add the row header to the sheet
    SVRowHeader rowHeader = new SVRowHeader(sheet, sheetView, extraHeight);
    JScrollPane scroll = new JScrollPane(sheetView);
    scroll.setRowHeaderView(rowHeader);
    return scroll;
}