Documents > Cookbook >Style Handling

Style handling methods provide convenient methods to set font and borders.

Font handling
The most simple method to define font settings is to create a font object, and set it to a cell object. The below code snippet defines a font object to describe "Arial" italic font with size "12pt" and black color, and then set it to a cell. The font will work for western characters by default.

        SpreadsheetDocument document = SpreadsheetDocument.newSpreadsheetDocument();
Table table = document.getTableByName("Sheet1");
Font font = new Font("Arial", StyleTypeDefinitions.FontStyle.ITALIC, 12, Color.BLACK);
Cell cell = table.getCellByPosition("A1");

The most simple method to get font settings of western characters is:

        Font theFont = cell.getFont();
double size = theFont.getSize();
String fontName = theFont.getFamilyName();
StyleTypeDefinitions.FontStyle fontStyle = theFont.getFontStyle();
Color fontColor = theFont.getColor();

Advanced font handling
CellStyleHandler can help you to achieve advanced functions. In Open Document Format, there can be different font settings for different script types. For example, a font setting for English characters and another font setting for Chinese characters. If you want to define the font setting for other script types, you can reference to below codes. The below code snippet defines a font for Chinese characters.

        cell.getStyleHandler().setFont(font, new Locale(Locale.CHINESE.getLanguage(), Locale.CHINA.getCountry()));

The below code snippet shows how to get the font setting for other kinds of scripts.

        CellStyleHandler styleHandler = cell.getStyleHandler();
Font westernFont = styleHandler.getFont(Document.ScriptType.WESTERN);
Font chineseFont = styleHandler.getFont(Document.ScriptType.CJK);
Font complexFont = styleHandler.getFont(Document.ScriptType.CTL);

Border handling
The most simple way to set border is to create a border object and then set it to a cell object. Below code snippet illustrates how to set a cell object with four borders.

        cell = table.getCellByPosition("A1");
cell.setStringValue("four border");
Border border = new Border(Color.RED, 1, StyleTypeDefinitions.SupportedLinearMeasure.PT);
cell.setBorders(CellBordersType.ALL_FOUR, border);

Below code snippet illustrates how to set a cell object with left and right borders, top and bottom borders and diagonal lines.

        cell.setBorders(CellBordersType.LEFT_RIGHT, border);
cell.setBorders(CellBordersType.TOP_BOTTOM, border);
cell.setBorders(CellBordersType.DIAGONAL_LINES, border);

Below code snippet illustrates how to set a cell object with left border, top border and diagonal from bottom left to top right.

        cell.setBorders(CellBordersType.LEFT, border);
cell.setBorders(CellBordersType.TOP, border);
cell.setBorders(CellBordersType.DIAGONALBLTR, border);

Below code snippet illustrates how to get a border definition.

        Border thisBorder = cell.getBorder(CellBordersType.LEFT);
thisBorder = cell.getBorder(CellBordersType.TOP);
thisBorder = cell.getBorder(CellBordersType.DIAGONALBLTR);

Powered by the Apache CMS.

Apache "ODF Toolkit" is an effort undergoing incubation at The Apache Software Foundation (ASF), sponsored by the Apache Incubator. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

Copyright © 2011 The Apache Software Foundation Licensed under the Apache License, Version 2.0. Contact Us
Apache and the Apache feather logos are trademarks of The Apache Software Foundation.
Other names appearing on the site may be trademarks of their respective owners.