Example usage for org.apache.poi.ss.usermodel Font setUnderline

List of usage examples for org.apache.poi.ss.usermodel Font setUnderline

Introduction

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

Prototype


void setUnderline(byte underline);

Source Link

Document

set type of text underlining to use

Usage

From source file:net.ceos.project.poi.annotated.core.CellStyleHandler.java

License:Apache License

/**
 * Apply the font to the cell style./*  w w  w .ja v  a2 s  . co m*/
 * 
 * @param wb
 *            the {@link Workbook} in use
 * @param cs
 *            the {@link CellStyle} in use
 * @param name
 *            the font name
 * @param size
 *            the font size
 * @param c
 *            the font color
 * @param b
 *            is bold format
 * @param i
 *            is italic format
 * @param u
 *            is underline format
 */
protected static void applyFont(final Workbook wb, final CellStyle cs, final String name, final short size,
        final short c, final boolean b, final boolean i, final byte u) {
    Font f = fontFactory.newInstance(wb);
    f.setFontName(name);
    f.setFontHeightInPoints(size);
    f.setColor(c);
    f.setBold(b);
    f.setItalic(i);
    f.setUnderline(u);
    cs.setFont(f);
}

From source file:opn.greenwebs.HyperlinkExample.java

public static void main(String[] args) throws Exception {
    Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();

    //cell style for hyperlinks
    //by default hyperlinks are blue and underlined
    CellStyle hlink_style = wb.createCellStyle();
    Font hlink_font = wb.createFont();
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(IndexedColors.BLUE.getIndex());
    hlink_style.setFont(hlink_font);/*ww  w  . j ava 2s  .  c om*/

    Cell cell;
    Sheet sheet = wb.createSheet("Hyperlinks");
    //URL
    cell = sheet.createRow(0).createCell((short) 0);
    cell.setCellValue("URL Link");

    Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
    link.setAddress("http://poi.apache.org/");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);

    //link to a file in the current directory
    cell = sheet.createRow(1).createCell((short) 0);
    cell.setCellValue("File Link");
    link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
    link.setAddress("link1.xls");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);

    //e-mail link
    cell = sheet.createRow(2).createCell((short) 0);
    cell.setCellValue("Email Link");
    link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL);
    //note, if subject contains white spaces, make sure they are url-encoded
    link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);

    //link to a place in this workbook

    //create a target sheet and cell
    Sheet sheet2 = wb.createSheet("Target Sheet");
    sheet2.createRow(0).createCell((short) 0).setCellValue("Target Cell");

    cell = sheet.createRow(3).createCell((short) 0);
    cell.setCellValue("Worksheet Link");
    Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
    link2.setAddress("'Target Sheet'!A1");
    cell.setHyperlink(link2);
    cell.setCellStyle(hlink_style);

    FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
    wb.write(out);
    out.close();

}

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SpreadSheetFormatOptions.java

License:Open Source License

public static Font createCommentFont(Workbook workbook, cfStructData _struct) throws Exception {
    Font font = workbook.createFont();

    if (_struct.containsKey("bold")) {
        if (_struct.getData("bold").getBoolean())
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        else/*w w  w  .ja  v a 2 s.  c  om*/
            font.setBoldweight(Font.BOLDWEIGHT_NORMAL);
    }

    if (_struct.containsKey("color")) {
        String v = _struct.getData("color").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'color' (" + v + ")");
        } else
            font.setColor(s);
    }

    if (_struct.containsKey("font")) {
        font.setFontName(_struct.getData("font").getString());
    }

    if (_struct.containsKey("italic")) {
        font.setItalic(_struct.getData("italic").getBoolean());
    }

    if (_struct.containsKey("strikeout")) {
        font.setStrikeout(_struct.getData("strikeout").getBoolean());
    }

    if (_struct.containsKey("underline")) {
        font.setUnderline((byte) _struct.getData("underline").getInt());
    }

    if (_struct.containsKey("size")) {
        font.setFontHeightInPoints((short) _struct.getData("size").getInt());
    }

    return font;
}

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SpreadSheetFormatOptions.java

License:Open Source License

public static CellStyle createCellStyle(Workbook workbook, cfStructData _struct) throws Exception {
    CellStyle style = workbook.createCellStyle();

    if (_struct.containsKey("alignment")) {
        String v = _struct.getData("alignment").getString();
        Short s = lookup_alignment.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'alignment' (" + v + ")");
        } else//w  w  w  .  j  a  va  2  s .c  o m
            style.setAlignment(s);
    }

    if (_struct.containsKey("bottomborder")) {
        String v = _struct.getData("bottomborder").getString();
        Short s = lookup_border.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'bottomborder' (" + v + ")");
        } else
            style.setBorderBottom(s);
    }

    if (_struct.containsKey("topborder")) {
        String v = _struct.getData("topborder").getString();
        Short s = lookup_border.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'topborder' (" + v + ")");
        } else
            style.setBorderTop(s);
    }

    if (_struct.containsKey("leftborder")) {
        String v = _struct.getData("leftborder").getString();
        Short s = lookup_border.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'leftborder' (" + v + ")");
        } else
            style.setBorderLeft(s);
    }

    if (_struct.containsKey("rightborder")) {
        String v = _struct.getData("rightborder").getString();
        Short s = lookup_border.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'rightborder' (" + v + ")");
        } else
            style.setBorderRight(s);
    }

    if (_struct.containsKey("bottombordercolor")) {
        String v = _struct.getData("bottombordercolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'bottombordercolor' (" + v + ")");
        } else
            style.setBottomBorderColor(s);
    }

    if (_struct.containsKey("topbordercolor")) {
        String v = _struct.getData("topbordercolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'topbordercolor' (" + v + ")");
        } else
            style.setTopBorderColor(s);
    }

    if (_struct.containsKey("leftbordercolor")) {
        String v = _struct.getData("leftbordercolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'leftbordercolor' (" + v + ")");
        } else
            style.setLeftBorderColor(s);
    }

    if (_struct.containsKey("rightbordercolor")) {
        String v = _struct.getData("rightbordercolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'rightbordercolor' (" + v + ")");
        } else
            style.setRightBorderColor(s);
    }

    if (_struct.containsKey("fillpattern")) {
        String v = _struct.getData("fillpattern").getString();
        Short s = lookup_fillpatten.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'fillpattern' (" + v + ")");
        } else
            style.setFillPattern(s);
    }

    if (_struct.containsKey("fgcolor")) {
        String v = _struct.getData("fgcolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'fgcolor' (" + v + ")");
        } else
            style.setFillForegroundColor(s);
    }

    if (_struct.containsKey("bgcolor")) {
        String v = _struct.getData("bgcolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'bgcolor' (" + v + ")");
        } else
            style.setFillBackgroundColor(s);
    }

    if (_struct.containsKey("textwrap")) {
        Boolean b = _struct.getData("textwrap").getBoolean();
        style.setWrapText(b);
    }

    if (_struct.containsKey("hidden")) {
        Boolean b = _struct.getData("hidden").getBoolean();
        style.setHidden(b);
    }

    if (_struct.containsKey("locked")) {
        Boolean b = _struct.getData("locked").getBoolean();
        style.setLocked(b);
    }

    if (_struct.containsKey("indent")) {
        style.setIndention((short) _struct.getData("indent").getInt());
    }

    if (_struct.containsKey("rotation")) {
        style.setRotation((short) _struct.getData("rotation").getInt());
    }

    if (_struct.containsKey("dateformat")) {
        style.setDataFormat(workbook.createDataFormat().getFormat(_struct.getData("dateformat").getString()));
    }

    // Manage the fonts
    Font f = workbook.createFont();

    if (_struct.containsKey("strikeout")) {
        f.setStrikeout(true);
    }

    if (_struct.containsKey("bold")) {
        Boolean b = _struct.getData("bold").getBoolean();
        f.setBoldweight(b ? Font.BOLDWEIGHT_BOLD : Font.BOLDWEIGHT_NORMAL);
    }

    if (_struct.containsKey("underline")) {
        String v = _struct.getData("underline").getString();
        Byte b = lookup_underline.get(v);
        if (b == null) {
            throw new Exception("invalid parameter for 'underline' (" + v + ")");
        } else
            f.setUnderline(b);
    }

    if (_struct.containsKey("color")) {
        String v = _struct.getData("color").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'color' (" + v + ")");
        } else
            f.setColor(s);
    }

    if (_struct.containsKey("fontsize")) {
        int s = _struct.getData("fontsize").getInt();
        f.setFontHeightInPoints((short) s);
    }

    if (_struct.containsKey("font")) {
        f.setFontName(_struct.getData("font").getString());
    }

    style.setFont(f);

    return style;
}

From source file:org.apache.tika.eval.reports.XLSXHREFFormatter.java

License:Apache License

@Override
public void reset(XSSFWorkbook workbook) {
    this.workbook = workbook;
    style = workbook.createCellStyle();/*  w w w  .  j  ava  2 s.c om*/
    Font hlinkFont = workbook.createFont();
    hlinkFont.setUnderline(Font.U_SINGLE);
    hlinkFont.setColor(IndexedColors.BLUE.getIndex());
    style.setFont(hlinkFont);
    links = 0;

}

From source file:org.cerberus.service.export.ExportServiceFactory.java

License:Open Source License

private int createRow(String test, HashMap<String, List<TestCaseExecution>> executionsPerTestCase, Sheet sheet,
        int currentIndex, List<String> mapCountries) {

    int lastRow = currentIndex + executionsPerTestCase.size();

    int current = currentIndex;

    TreeMap<String, List<TestCaseExecution>> sortedKeys = new TreeMap<String, List<TestCaseExecution>>(
            executionsPerTestCase);/*from w ww .  j a  v  a 2 s  .  c o m*/
    CellStyle wrapStyle = sheet.getColumnStyle(0); //Create new style
    wrapStyle.setWrapText(true); //Set wordwrap

    for (String testCaseKey : sortedKeys.keySet()) {
        List<String> browserEnvironment = new LinkedList<String>();
        String application;
        String description;
        Row r = sheet.createRow(current);
        List<TestCaseExecution> executionList = executionsPerTestCase.get(testCaseKey);
        Cell testCell = r.createCell(0);
        testCell.setCellValue(test);
        testCell.setCellStyle(wrapStyle);
        r.createCell(1).setCellValue(testCaseKey);

        //gets the first object to retrieve the application - at least exists one test case execution
        if (executionList.isEmpty()) {
            application = "N/D";
            description = "N/D";
        } else {
            application = executionList.get(0).getApplication();
            description = executionList.get(0).getTestCaseObj().getBehaviorOrValueExpected();
        }
        //Sets the application and description
        r.createCell(2).setCellValue(application);
        r.createCell(3).setCellValue(description);

        int rowStartedTestCaseInfo = current;

        for (TestCaseExecution exec : executionList) {
            if (browserEnvironment.isEmpty()) {
                browserEnvironment.add(exec.getEnvironment() + "_" + exec.getBrowser());
                r.createCell(4).setCellValue(exec.getEnvironment());
                r.createCell(5).setCellValue(exec.getBrowser());
            } else {
                int index = browserEnvironment.indexOf(exec.getEnvironment() + "_" + exec.getBrowser());

                //Does not exist any information about browser and environment
                if (browserEnvironment.indexOf(exec.getEnvironment() + "_" + exec.getBrowser()) == -1) {
                    //need to add another row with the same characteristics
                    r = sheet.createRow(++current);
                    r.createCell(0).setCellValue(test);
                    r.createCell(1).setCellValue(testCaseKey);
                    r.createCell(2).setCellValue(application);
                    r.createCell(3).setCellValue(description);
                    r.createCell(4).setCellValue(exec.getEnvironment());
                    r.createCell(5).setCellValue(exec.getBrowser());

                    browserEnvironment.add(exec.getEnvironment() + "_" + exec.getBrowser());
                } else {
                    //there is information about the browser and environment
                    Row rowExisting = sheet.getRow(rowStartedTestCaseInfo + index);
                    r = rowExisting;
                }

            }

            //TODO:FN tirar daqui estes valores
            int indexOfCountry = mapCountries.indexOf(exec.getCountry()) + 6;
            Cell executionResult = r.createCell(indexOfCountry);
            executionResult.setCellValue(exec.getControlStatus());
            //Create hyperling
            CreationHelper createHelper = sheet.getWorkbook().getCreationHelper();
            CellStyle hlinkstyle = sheet.getWorkbook().createCellStyle();
            Font hlinkfont = sheet.getWorkbook().createFont();
            hlinkfont.setUnderline(XSSFFont.U_SINGLE);
            hlinkfont.setColor(HSSFColor.BLUE.index);
            hlinkstyle.setFont(hlinkfont);

            Hyperlink link = (Hyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
            link.setAddress("http://www.tutorialspoint.com/");
            executionResult.setHyperlink((Hyperlink) link);
            executionResult.setCellStyle(hlinkstyle);

        }
        current++;

    }

    /*r.createCell(1).setCellValue("");
     r.createCell(2).setCellValue("");
     r.createCell(3).setCellValue("");
     r.createCell(4).setCellValue("");
     r.createCell(5).setCellValue("");
     */
    //        for(TestCaseWithExecution exec : execution){
    //            
    //            //r.createCell(2).setCellValue(exec.getDescription());
    //            //r.createCell(3).setCellValue(exec.getApplication());
    //            //r.createCell(4).setCellValue(exec.getEnvironment());
    //            //r.createCell(5).setCellValue(exec.getBrowser());
    //            int indexOfCountry = mapCountries.indexOf(exec.getCountry()) + 6;
    //            r.createCell(indexOfCountry).setCellValue(exec.getControlStatus());
    //            //current++;
    //        }
    //puts the test name in the first column
    /*r = sheet.getRow(currentIndex);
     r.getCell(0).setCellValue(test);
     */
    /*CellRangeAddress range = new CellRangeAddress(currentIndex, lastRow, 0, 0);
     sheet.addMergedRegion(range);*/
    return lastRow;
}

From source file:org.gedantic.web.servlet.WorkbookCreator.java

License:Open Source License

/**
 * Setup styles./* w ww.ja  va 2s .c  o m*/
 */
private void setupStyles() {
    styleTitle = wb.createCellStyle();
    Font title_font = wb.createFont();
    title_font.setFontName("Helvetica");
    title_font.setColor(IndexedColors.BLACK.getIndex());
    title_font.setFontHeightInPoints((short) 24);
    styleTitle.setFont(title_font);

    styleSubtitle = wb.createCellStyle();
    Font subtitle_font = wb.createFont();
    subtitle_font.setFontName("Helvetica");
    subtitle_font.setColor(IndexedColors.GREY_50_PERCENT.getIndex());
    subtitle_font.setFontHeightInPoints((short) 18);
    styleSubtitle.setFont(subtitle_font);

    styleHyperlink = wb.createCellStyle();
    Font hlink_font = wb.createFont();
    hlink_font.setFontName("Helvetica");
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(IndexedColors.BLUE.getIndex());
    styleHyperlink.setFont(hlink_font);

    styleNormal = wb.createCellStyle();
    Font normal_font = wb.createFont();
    normal_font.setFontName("Helvetica");
    normal_font.setColor(IndexedColors.BLACK.getIndex());
    normal_font.setFontHeightInPoints((short) 12);
    styleNormal.setFont(normal_font);
    styleNormal.setWrapText(true);

    styleHeader = wb.createCellStyle();
    Font header_font = wb.createFont();
    header_font.setFontName("Helvetica");
    header_font.setColor(IndexedColors.WHITE.getIndex());
    header_font.setBold(true);
    header_font.setFontHeightInPoints((short) 12);
    XSSFColor bg = new XSSFColor(new java.awt.Color(0x28, 0x60, 0x90));
    ((XSSFCellStyle) styleHeader).setFillForegroundColor(bg);
    styleHeader.setFillPattern(CellStyle.SOLID_FOREGROUND);
    styleHeader.setFont(header_font);

}

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

License:Apache License

@SuppressWarnings("unused")
private static void writeCell(Cell cell, Object val, boolean userTemplate,
        ExcelWriteFieldMappingAttribute attribute, Object bean) {
    if (attribute != null && attribute.getLinkField() != null) {
        String addressFieldName = attribute.getLinkField();
        String address = null;//  w  ww.ja  va2  s. c  o m
        if (bean != null) {
            address = (String) getFieldValue(bean, addressFieldName, true);
        }
        Workbook wb = cell.getRow().getSheet().getWorkbook();

        Hyperlink link = wb.getCreationHelper().createHyperlink(attribute.getLinkType());
        link.setAddress(address);
        cell.setHyperlink(link);
        // Its style can't inherit from cell.
        CellStyle style = wb.createCellStyle();
        Font hlinkFont = wb.createFont();
        hlinkFont.setUnderline(Font.U_SINGLE);
        hlinkFont.setColor(IndexedColors.BLUE.getIndex());
        style.setFont(hlinkFont);
        if (cell.getCellStyle() != null) {
            style.setFillBackgroundColor(cell.getCellStyle().getFillBackgroundColor());
        }
        cell.setCellStyle(style);
    }
    if (val == null) {
        cell.setCellValue((String) null);
        return;
    }
    Class<?> clazz = val.getClass();
    if (val instanceof Byte) {// Double
        Byte temp = (Byte) val;
        cell.setCellValue((double) temp.byteValue());
    } else if (val instanceof Short) {
        Short temp = (Short) val;
        cell.setCellValue((double) temp.shortValue());
    } else if (val instanceof Integer) {
        Integer temp = (Integer) val;
        cell.setCellValue((double) temp.intValue());
    } else if (val instanceof Long) {
        Long temp = (Long) val;
        cell.setCellValue((double) temp.longValue());
    } else if (val instanceof Float) {
        Float temp = (Float) val;
        cell.setCellValue((double) temp.floatValue());
    } else if (val instanceof Double) {
        Double temp = (Double) val;
        cell.setCellValue((double) temp.doubleValue());
    } else if (val instanceof Date) {// Date
        Date dateVal = (Date) val;
        long time = dateVal.getTime();
        // read is based on 1899/12/31 but DateUtil.getExcelDate is base on
        // 1900/01/01
        if (time >= TIME_1899_12_31_00_00_00_000 && time < TIME_1900_01_01_00_00_00_000) {
            Date incOneDay = new Date(time + 24 * 60 * 60 * 1000);
            double d = DateUtil.getExcelDate(incOneDay);
            cell.setCellValue(d - 1);
        } else {
            cell.setCellValue(dateVal);
        }

        if (!userTemplate) {
            Workbook wb = cell.getRow().getSheet().getWorkbook();
            CellStyle cellStyle = cell.getCellStyle();
            if (cellStyle == null) {
                cellStyle = wb.createCellStyle();
            }
            DataFormat dataFormat = wb.getCreationHelper().createDataFormat();
            // @see #BuiltinFormats
            // 0xe, "m/d/yy"
            // 0x14 "h:mm"
            // 0x16 "m/d/yy h:mm"
            // {@linke https://en.wikipedia.org/wiki/Year_10,000_problem}
            /** [1899/12/31 00:00:00:000~1900/01/01 00:00:000) */
            if (time >= TIME_1899_12_31_00_00_00_000 && time < TIME_1900_01_02_00_00_00_000) {
                cellStyle.setDataFormat(dataFormat.getFormat("h:mm"));
                // cellStyle.setDataFormat(dataFormat.getFormat("m/d/yy h:mm"));
            } else {
                // if ( time % (24 * 60 * 60 * 1000) == 0) {//for time
                // zone,we can't use this way.
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(dateVal);
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                int millisecond = calendar.get(Calendar.MILLISECOND);
                if (millisecond == 0 && second == 0 && minute == 0 && hour == 0) {
                    cellStyle.setDataFormat(dataFormat.getFormat("m/d/yy"));
                } else {
                    cellStyle.setDataFormat(dataFormat.getFormat("m/d/yy h:mm"));
                }
            }
            cell.setCellStyle(cellStyle);
        }
    } else if (val instanceof Boolean) {// Boolean
        cell.setCellValue(((Boolean) val).booleanValue());
    } else {// String
        cell.setCellValue((String) val.toString());
    }
}

From source file:org.joeffice.spreadsheet.TableStyleable.java

License:Apache License

/**
 * Add the attribute as defined in {@link AttributedString} to the {@link MutableAttributeSet} for the JTextPane.
 *
 * @see java.awt.font.TextAttribute//  w ww . ja v a  2  s.  co  m
 */
protected void addAttribute(AttributedCharacterIterator.Attribute attribute, Object attributeValue, Cell cell) {
    CellStyle oldStyle = cell.getCellStyle();
    Workbook workbook = cell.getSheet().getWorkbook();
    CellStyle style = cell.getSheet().getWorkbook().createCellStyle();
    style.cloneStyleFrom(oldStyle);
    Font newFont = copyFont(cell);
    if (attribute == FAMILY) {
        newFont.setFontName((String) attributeValue);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == FOREGROUND) {
        Color color = (Color) attributeValue;
        if (cell instanceof XSSFCell) {
            ((XSSFCellStyle) style).setFillForegroundColor(new XSSFColor(color));
        } else {
            HSSFWorkbook xlsWorkbook = (HSSFWorkbook) workbook;
            HSSFColor xlsColor = xlsWorkbook.getCustomPalette().findColor((byte) color.getRed(),
                    (byte) color.getGreen(), (byte) color.getBlue());
            if (xlsColor == null) {
                xlsColor = xlsWorkbook.getCustomPalette().addColor((byte) color.getRed(),
                        (byte) color.getGreen(), (byte) color.getBlue());
            }
            style.setFillForegroundColor(xlsColor.getIndex());
        }
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    } else if (attribute == BACKGROUND) {
        Color color = (Color) attributeValue;
        if (cell instanceof XSSFCell) {
            ((XSSFCellStyle) style).setFillBackgroundColor(new XSSFColor(color));
        } else {
            HSSFWorkbook xlsWorkbook = (HSSFWorkbook) workbook;
            HSSFColor xlsColor = xlsWorkbook.getCustomPalette().findColor((byte) color.getRed(),
                    (byte) color.getGreen(), (byte) color.getBlue());
            if (xlsColor == null) {
                xlsColor = xlsWorkbook.getCustomPalette().addColor((byte) color.getRed(),
                        (byte) color.getGreen(), (byte) color.getBlue());
            }
            style.setFillBackgroundColor(xlsColor.getIndex());
        }
    } else if (attribute == WEIGHT) {
        short boldValue = Font.BOLDWEIGHT_BOLD;
        if (newFont.getBoldweight() == Font.BOLDWEIGHT_BOLD) {
            boldValue = Font.BOLDWEIGHT_NORMAL;
        }
        newFont.setBoldweight(boldValue);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == UNDERLINE) {
        byte underlineValue = Font.U_SINGLE;
        if (newFont.getUnderline() == Font.U_SINGLE) {
            underlineValue = Font.U_NONE;
        }
        newFont.setUnderline(underlineValue);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == SUPERSCRIPT) {
        short superscriptValue = Font.SS_NONE;
        if (SUPERSCRIPT_SUB.equals(attributeValue)) {
            superscriptValue = Font.SS_SUB;
        } else if (SUPERSCRIPT_SUPER.equals(attributeValue)) {
            superscriptValue = Font.SS_SUPER;
        }
        newFont.setTypeOffset(superscriptValue);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == STRIKETHROUGH) {
        boolean strikeThrough = true;
        if (newFont.getStrikeout()) {
            strikeThrough = false;
        }
        newFont.setStrikeout(strikeThrough);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == POSTURE) {
        boolean italic = true;
        if (newFont.getItalic()) {
            italic = false;
        }
        newFont.setItalic(italic);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == SIZE) {
        newFont.setFontHeightInPoints(((Number) attributeValue).shortValue());
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == JUSTIFICATION) {
        CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_JUSTIFY);
    } else if (attribute == ALIGNMENT) {
        if (attributeValue.equals(StyleConstants.ALIGN_LEFT)) {
            CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_LEFT);
        } else if (attributeValue.equals(StyleConstants.ALIGN_RIGHT)) {
            CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_RIGHT);
        } else if (attributeValue.equals(StyleConstants.ALIGN_CENTER)) {
            CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_CENTER);
        }
    } else if (attribute == INDENTATION) {
        style.setIndention(((Number) attributeValue).shortValue());
    } else if (attribute == TEXT_TRANSFORM) {
        String text = CellUtils.getFormattedText(cell);
        String transformedText = ((TextTransformer) attributeValue).transformText(text);
        cell.setCellValue(transformedText);
    }
}

From source file:org.joeffice.spreadsheet.TableStyleable.java

License:Apache License

private Font copyFont(Cell cell) {
    CellStyle style = cell.getCellStyle();
    Workbook workbook = cell.getSheet().getWorkbook();
    short fontIndex = style.getFontIndex();
    Font xlsFont = cell.getSheet().getWorkbook().getFontAt(fontIndex);
    Font newFont = workbook.createFont();
    newFont.setFontName(xlsFont.getFontName());
    newFont.setFontHeight((short) xlsFont.getFontHeight());
    newFont.setBoldweight(xlsFont.getBoldweight());
    newFont.setItalic(xlsFont.getItalic());
    newFont.setUnderline(xlsFont.getUnderline());
    newFont.setColor(xlsFont.getColor());
    return newFont;
}