Example usage for org.apache.poi.hssf.usermodel HSSFCellStyle getFont

List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle getFont

Introduction

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

Prototype

public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) 

Source Link

Document

gets the font for this style

Usage

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

/**
 * (Each Excel sheet cell becomes an HTML table cell) Generates an HTML
 * table cell which has the same font styles, alignments, colours and
 * borders as the Excel cell.//  w ww. j  a  v  a 2  s.c o m
 *
 * @param cell
 *            The Excel cell.
 */

private void td(final HSSFCell cell) {
    int colspan = 1;
    if (colIndex == mergeStart) {
        // First cell in the merging region - set colspan.
        colspan = mergeEnd - mergeStart + 1;
    } else if (colIndex == mergeEnd) {
        // Last cell in the merging region - no more skipped cells.
        mergeStart = -1;
        mergeEnd = -1;
        return;
    } else if (mergeStart != -1 && mergeEnd != -1 && colIndex > mergeStart && colIndex < mergeEnd) {
        // Within the merging region - skip the cell.
        return;
    }
    //KLO 05022018
    //out.append("<td ");

    out.append("<td height=\"15\" ");
    if (colspan > 1) {
        out.append("colspan='").append(colspan).append("' ");
    }
    if (cell == null) {
        out.append("/>\n");
        return;
    }

    out.append("style='");
    final HSSFCellStyle style = cell.getCellStyle();
    // Text alignment
    switch (style.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        out.append("text-align: left; ");
        break;
    case CellStyle.ALIGN_RIGHT:
        out.append("text-align: right; ");
        break;
    case CellStyle.ALIGN_CENTER:
        out.append("text-align: center; ");
        break;
    default:
        break;
    }

    // Font style, size and weight
    final HSSFFont font = style.getFont(book);
    if (font == null)
        return;
    if (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) {
        out.append("font-weight: bold; ");
    }
    if (font.getItalic()) {
        out.append("font-style: italic; ");
    }
    if (font.getUnderline() != HSSFFont.U_NONE) {
        out.append("text-decoration: underline; ");
    }
    out.append("font-size: ").append(Math.floor(font.getFontHeightInPoints() * 0.8)).append("pt; ");
    // Cell background and font colours
    final short[] backRGB = style.getFillForegroundColorColor().getTriplet();
    final HSSFColor foreColor = palette.getColor(font.getColor());
    if (foreColor != null) {
        final short[] foreRGB = foreColor.getTriplet();
        if (foreRGB[0] != 0 || foreRGB[1] != 0 || foreRGB[2] != 0) {
            out.append("color: rgb(").append(foreRGB[0]).append(',').append(foreRGB[1]).append(',')
                    .append(foreRGB[2]).append(");");
        }
    }
    if (backRGB[0] != 0 || backRGB[1] != 0 || backRGB[2] != 0) {
        out.append("background-color: rgb(").append(backRGB[0]).append(',').append(backRGB[1]).append(',')
                .append(backRGB[2]).append(");");
    }
    // Border
    if (style.getBorderTop() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-top-style: solid; ");
    }
    if (style.getBorderRight() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-right-style: solid; ");
    }
    if (style.getBorderBottom() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-bottom-style: solid; ");
    }
    if (style.getBorderLeft() != HSSFCellStyle.BORDER_NONE) {
        out.append("border-left-style: solid; ");
    }
    out.append("'>");
    String val = "";
    try {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            val = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            // POI does not distinguish between integer and double, thus:
            final double original = cell.getNumericCellValue(), rounded = Math.round(original);
            if (Math.abs(rounded - original) < 0.00000000000000001) {
                val = String.valueOf((int) rounded);
            } else {
                val = String.valueOf(original);
            }
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            final CellValue cv = evaluator.evaluate(cell);
            if (cv == null)
                return;
            switch (cv.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                out.append(cv.getBooleanValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                out.append(cv.getNumberValue());
                break;
            case Cell.CELL_TYPE_STRING:
                out.append(cv.getStringValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            case Cell.CELL_TYPE_ERROR:
                break;
            default:
                break;
            }
            break;
        default:
            // Neither string or number? Could be a date.
            try {
                val = sdf.format(cell.getDateCellValue());
            } catch (final Exception e1) {
            }
        }
    } catch (final Exception e) {
        val = e.getMessage();
    }
    if ("null".equals(val)) {
        val = "";
    }
    if (pix.containsKey(rowIndex)) {
        if (pix.get(rowIndex).containsKey(colIndex)) {
            for (final HSSFPictureData pic : pix.get(rowIndex).get(colIndex)) {
                out.append("<img alt='Image in Excel sheet' src='data:");
                out.append(pic.getMimeType());
                out.append(";base64,");
                try {
                    out.append(new String(Base64.encodeBase64(pic.getData()), "US-ASCII"));
                } catch (final UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                out.append("'/>");
            }
        }
    }

    if (isCode(val) && this.URL != null) {
        val = getHyperLink(val);
    }

    out.append(val);
    out.append("</td>\n");
}

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

private void td(final HSSFCell cell, StringBuffer buf) {
    int colspan = 1;
    if (colIndex == mergeStart) {
        // First cell in the merging region - set colspan.
        colspan = mergeEnd - mergeStart + 1;
    } else if (colIndex == mergeEnd) {
        // Last cell in the merging region - no more skipped cells.
        mergeStart = -1;//from   w  ww .j av  a 2s.c o m
        mergeEnd = -1;
        return;
    } else if (mergeStart != -1 && mergeEnd != -1 && colIndex > mergeStart && colIndex < mergeEnd) {
        // Within the merging region - skip the cell.
        return;
    }

    //KLO 05022018
    //buf.append("<td ");

    buf.append("<td height=\"15\" ");

    if (colspan > 1) {
        buf.append("colspan='").append(colspan).append("' ");
    }
    if (cell == null) {
        buf.append("/>");
        return;
    }
    buf.append("style='");
    final HSSFCellStyle style = cell.getCellStyle();
    // Text alignment
    switch (style.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        buf.append("text-align: left; ");
        break;
    case CellStyle.ALIGN_RIGHT:
        buf.append("text-align: right; ");
        break;
    case CellStyle.ALIGN_CENTER:
        buf.append("text-align: center; ");
        break;
    default:
        break;
    }
    // Font style, size and weight
    final HSSFFont font = style.getFont(book);
    if (font == null)
        return;
    if (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) {
        buf.append("font-weight: bold; ");
    }
    if (font.getItalic()) {
        buf.append("font-style: italic; ");
    }
    if (font.getUnderline() != HSSFFont.U_NONE) {
        buf.append("text-decoration: underline; ");
    }
    buf.append("font-size: ").append(Math.floor(font.getFontHeightInPoints() * 0.8)).append("pt; ");

    // Cell background and font colours
    final short[] backRGB = style.getFillForegroundColorColor().getTriplet();
    final HSSFColor foreColor = palette.getColor(font.getColor());
    if (foreColor != null) {
        final short[] foreRGB = foreColor.getTriplet();
        if (foreRGB[0] != 0 || foreRGB[1] != 0 || foreRGB[2] != 0) {
            buf.append("color: rgb(").append(foreRGB[0]).append(',').append(foreRGB[1]).append(',')
                    .append(foreRGB[2]).append(");");

        }
    }
    if (backRGB[0] != 0 || backRGB[1] != 0 || backRGB[2] != 0) {
        buf.append("background-color: rgb(").append(backRGB[0]).append(',').append(backRGB[1]).append(',')
                .append(backRGB[2]).append(");");

    }
    // Border
    if (style.getBorderTop() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-top-style: solid; ");
    }
    if (style.getBorderRight() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-right-style: solid; ");
    }
    if (style.getBorderBottom() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-bottom-style: solid; ");
    }
    if (style.getBorderLeft() != HSSFCellStyle.BORDER_NONE) {
        buf.append("border-left-style: solid; ");
    }
    buf.append("'>");
    String val = "";
    try {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            val = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            // POI does not distinguish between integer and double, thus:
            final double original = cell.getNumericCellValue(), rounded = Math.round(original);
            if (Math.abs(rounded - original) < 0.00000000000000001) {
                val = String.valueOf((int) rounded);
            } else {
                val = String.valueOf(original);
            }
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            final CellValue cv = evaluator.evaluate(cell);
            if (cv == null)
                return;
            switch (cv.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                buf.append(cv.getBooleanValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                buf.append(cv.getNumberValue());
                break;
            case Cell.CELL_TYPE_STRING:
                buf.append(cv.getStringValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            case Cell.CELL_TYPE_ERROR:
                break;
            default:
                break;
            }
            break;
        default:
            // Neither string or number? Could be a date.
            try {
                val = sdf.format(cell.getDateCellValue());
            } catch (final Exception e1) {
            }
        }
    } catch (final Exception e) {
        val = e.getMessage();
    }
    if ("null".equals(val)) {
        val = "";
    }
    if (pix.containsKey(rowIndex)) {
        if (pix.get(rowIndex).containsKey(colIndex)) {
            for (final HSSFPictureData pic : pix.get(rowIndex).get(colIndex)) {
                buf.append("<img alt='Image in Excel sheet' src='data:");
                buf.append(pic.getMimeType());
                buf.append(";base64,");

                try {
                    buf.append(new String(Base64.encodeBase64(pic.getData()), "US-ASCII"));

                } catch (final UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                buf.append("'/>");
            }
        }
    }

    if (isCode(val) && this.URL != null) {
        val = getHyperLink(val);
    }
    buf.append(val);
    buf.append("</td>");

}

From source file:org.bbreak.excella.core.test.util.TestUtil.java

License:Open Source License

private static String getCellStyleString(Workbook workbook, HSSFCellStyle cellStyle) {
    StringBuffer sb = new StringBuffer();
    if (cellStyle != null) {
        HSSFFont font = cellStyle.getFont(workbook);
        // sb.append("FontIndex=").append( cellStyle.getFontIndex()).append( ",");
        sb.append("Font=").append(getHSSFFontString((HSSFWorkbook) workbook, font)).append(",");

        sb.append("DataFormat=").append(cellStyle.getDataFormat()).append(",");
        sb.append("DataFormatString=").append(cellStyle.getDataFormatString()).append(",");
        sb.append("Hidden=").append(cellStyle.getHidden()).append(",");
        sb.append("Locked=").append(cellStyle.getLocked()).append(",");
        sb.append("Alignment=").append(cellStyle.getAlignmentEnum()).append(",");
        sb.append("WrapText=").append(cellStyle.getWrapText()).append(",");
        sb.append("VerticalAlignment=").append(cellStyle.getVerticalAlignmentEnum()).append(",");
        sb.append("Rotation=").append(cellStyle.getRotation()).append(",");
        sb.append("Indention=").append(cellStyle.getIndention()).append(",");
        sb.append("BorderLeft=").append(cellStyle.getBorderLeftEnum()).append(",");
        sb.append("BorderRight=").append(cellStyle.getBorderRightEnum()).append(",");
        sb.append("BorderTop=").append(cellStyle.getBorderTopEnum()).append(",");
        sb.append("BorderBottom=").append(cellStyle.getBorderBottomEnum()).append(",");

        sb.append("LeftBorderColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getLeftBorderColor()))
                .append(",");
        sb.append("RightBorderColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getRightBorderColor()))
                .append(",");
        sb.append("TopBorderColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getTopBorderColor())).append(",");
        sb.append("BottomBorderColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getBottomBorderColor()))
                .append(",");

        sb.append("FillPattern=").append(cellStyle.getFillPatternEnum()).append(",");
        sb.append("FillForegroundColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getFillForegroundColor()))
                .append(",");
        sb.append("FillBackgroundColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getFillBackgroundColor()));
    }// w w w.  j  av  a  2 s. c o m
    return sb.toString();
}

From source file:org.bbreak.excella.reports.ReportsTestUtil.java

License:Open Source License

/**
 * HSSF????//from  www. j av a 2s  . c  o  m
 * 
 * @param workbook 
 * @param cellStyle 
 * @return ??
 */
private static String getCellStyleString(Workbook workbook, HSSFCellStyle cellStyle) {
    StringBuffer sb = new StringBuffer();
    if (cellStyle != null) {
        HSSFFont font = cellStyle.getFont(workbook);
        // sb.append("FontIndex=").append( cellStyle.getFontIndex()).append( ",");
        sb.append("Font=").append(getHSSFFontString((HSSFWorkbook) workbook, font)).append(",");

        sb.append("DataFormat=").append(cellStyle.getDataFormat()).append(",");
        sb.append("DataFormatString=").append(cellStyle.getDataFormatString()).append(",");
        sb.append("Hidden=").append(cellStyle.getHidden()).append(",");
        sb.append("Locked=").append(cellStyle.getLocked()).append(",");
        sb.append("Alignment=").append(cellStyle.getAlignmentEnum()).append(",");
        sb.append("WrapText=").append(cellStyle.getWrapText()).append(",");
        sb.append("VerticalAlignment=").append(cellStyle.getVerticalAlignmentEnum()).append(",");
        sb.append("Rotation=").append(cellStyle.getRotation()).append(",");
        sb.append("Indention=").append(cellStyle.getIndention()).append(",");
        sb.append("BorderLeft=").append(cellStyle.getBorderLeftEnum()).append(",");
        sb.append("BorderRight=").append(cellStyle.getBorderRightEnum()).append(",");
        sb.append("BorderTop=").append(cellStyle.getBorderTopEnum()).append(",");
        sb.append("BorderBottom=").append(cellStyle.getBorderBottomEnum()).append(",");

        sb.append("LeftBorderColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getLeftBorderColor()))
                .append(",");
        sb.append("RightBorderColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getRightBorderColor()))
                .append(",");
        sb.append("TopBorderColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getTopBorderColor())).append(",");
        sb.append("BottomBorderColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getBottomBorderColor()))
                .append(",");

        sb.append("FillPattern=").append(cellStyle.getFillPatternEnum()).append(",");
        sb.append("FillForegroundColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getFillForegroundColor()))
                .append(",");
        sb.append("FillBackgroundColor=")
                .append(getHSSFColorString((HSSFWorkbook) workbook, cellStyle.getFillBackgroundColor()));
    }
    return sb.toString();
}

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

/**
 * ?xls?html?//from  ww  w .  jav a2s  .c  o  m
 * @param cell -- xls?
 * @return
 */
private String getCellStyle(HSSFCell cell) {
    HSSFCellStyle style = cell.getCellStyle();
    HSSFSheet sheet = cell.getSheet();
    String value = getCellValue(cell);

    //?
    StringBuilder sbStyle = new StringBuilder();

    //??
    sbStyle.append(getBorderStyle(style));

    //???
    sbStyle.append(getAlignStyle(style));

    //??
    HSSFFont font = style.getFont(sheet.getWorkbook());
    sbStyle.append(getFontStyle(font));

    //cell
    if (value == null || value.length() == 0) {
        sbStyle.append("color:red;");
    }

    return sbStyle.toString();
}

From source file:org.pentaho.reporting.engine.classic.core.bugs.Prd5391.java

License:Open Source License

@Test
public void testSlowExport() throws ResourceException, ReportProcessingException, IOException {
    // This establishes a baseline for the second test using the slow export.

    final MasterReport report = DebugReportRunner.parseLocalReport("Prd-5391.prpt", Prd5391.class);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ExcelReportUtil.createXLS(report, bout);

    final HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(bout.toByteArray()));
    final HSSFSheet sheetAt = wb.getSheetAt(0);
    final HSSFRow row = sheetAt.getRow(0);
    final HSSFCell cell0 = row.getCell(0);

    // assert that we are in the correct export type ..
    final HSSFCellStyle cellStyle = cell0.getCellStyle();
    final HSSFColor fillBackgroundColorColor = cellStyle.getFillBackgroundColorColor();
    final HSSFColor fillForegroundColorColor = cellStyle.getFillForegroundColorColor();
    Assert.assertEquals("0:0:0", fillBackgroundColorColor.getHexString());
    Assert.assertEquals("FFFF:8080:8080", fillForegroundColorColor.getHexString());

    HSSFFont font = cellStyle.getFont(wb);
    Assert.assertEquals("Times New Roman", font.getFontName());
}

From source file:org.pentaho.reporting.engine.classic.core.bugs.Prd5391.java

License:Open Source License

@Test
public void testFastExport() throws ResourceException, ReportProcessingException, IOException {
    // This establishes a baseline for the second test using the slow export.

    final MasterReport report = DebugReportRunner.parseLocalReport("Prd-5391.prpt", Prd5391.class);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    FastExcelReportUtil.processXls(report, bout);

    final HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(bout.toByteArray()));
    final HSSFSheet sheetAt = wb.getSheetAt(0);
    final HSSFRow row = sheetAt.getRow(0);
    final HSSFCell cell0 = row.getCell(0);

    // assert that we are in the correct export type ..
    final HSSFCellStyle cellStyle = cell0.getCellStyle();
    final HSSFColor fillBackgroundColorColor = cellStyle.getFillBackgroundColorColor();
    final HSSFColor fillForegroundColorColor = cellStyle.getFillForegroundColorColor();
    Assert.assertEquals("0:0:0", fillBackgroundColorColor.getHexString());
    Assert.assertEquals("FFFF:8080:8080", fillForegroundColorColor.getHexString());

    HSSFFont font = cellStyle.getFont(wb);
    Assert.assertEquals("Times New Roman", font.getFontName());
}

From source file:org.pentaho.reporting.engine.classic.core.bugs.Prd5391IT.java

License:Open Source License

@Test
public void testSlowExport() throws ResourceException, ReportProcessingException, IOException {
    // This establishes a baseline for the second test using the slow export.

    final MasterReport report = DebugReportRunner.parseLocalReport("Prd-5391.prpt", Prd5391IT.class);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ExcelReportUtil.createXLS(report, bout);

    final HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(bout.toByteArray()));
    final HSSFSheet sheetAt = wb.getSheetAt(0);
    final HSSFRow row = sheetAt.getRow(0);
    final HSSFCell cell0 = row.getCell(0);

    // assert that we are in the correct export type ..
    final HSSFCellStyle cellStyle = cell0.getCellStyle();
    final HSSFColor fillBackgroundColorColor = cellStyle.getFillBackgroundColorColor();
    final HSSFColor fillForegroundColorColor = cellStyle.getFillForegroundColorColor();
    Assert.assertEquals("0:0:0", fillBackgroundColorColor.getHexString());
    Assert.assertEquals("FFFF:8080:8080", fillForegroundColorColor.getHexString());

    HSSFFont font = cellStyle.getFont(wb);
    Assert.assertEquals("Times New Roman", font.getFontName());
}

From source file:org.pentaho.reporting.engine.classic.core.bugs.Prd5391IT.java

License:Open Source License

@Test
public void testFastExport() throws ResourceException, ReportProcessingException, IOException {
    // This establishes a baseline for the second test using the slow export.

    final MasterReport report = DebugReportRunner.parseLocalReport("Prd-5391.prpt", Prd5391IT.class);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    FastExcelReportUtil.processXls(report, bout);

    final HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(bout.toByteArray()));
    final HSSFSheet sheetAt = wb.getSheetAt(0);
    final HSSFRow row = sheetAt.getRow(0);
    final HSSFCell cell0 = row.getCell(0);

    // assert that we are in the correct export type ..
    final HSSFCellStyle cellStyle = cell0.getCellStyle();
    final HSSFColor fillBackgroundColorColor = cellStyle.getFillBackgroundColorColor();
    final HSSFColor fillForegroundColorColor = cellStyle.getFillForegroundColorColor();
    Assert.assertEquals("0:0:0", fillBackgroundColorColor.getHexString());
    Assert.assertEquals("FFFF:8080:8080", fillForegroundColorColor.getHexString());

    HSSFFont font = cellStyle.getFont(wb);
    Assert.assertEquals("Times New Roman", font.getFontName());
}

From source file:org.sysmodb.xml.HSSFXMLStyleHelper.java

License:BSD License

public boolean areFontsEmpty(CellStyle style) {
    HSSFCellStyle newStyle = (HSSFCellStyle) style;
    HSSFFont font = newStyle.getFont(workbook);
    if (font.getBold())
        return false;
    if (font.getItalic())
        return false;
    if (font.getUnderline() != HSSFFont.U_NONE)
        return false;
    // Ignore same-ish defaults
    if (font.getFontHeightInPoints() != 10 && font.getFontHeightInPoints() != 11)
        return false;
    // Arial is default for Excel, Calibri is default for OO
    if (!font.getFontName().equals("Arial") && !font.getFontName().equals("Calibri"))
        return false;
    if ((font.getColor() != HSSFFont.COLOR_NORMAL) && (getRGBString(font.getColor()) != null)
            && !getRGBString(font.getColor()).equals("#000"))
        return false;

    return true;/*from   w  ww  . j  av a 2 s  .c om*/
}