Excel styles and formatting in Java

Using GemBox.Spreadsheet you can style and format an Excel file in Java. You can set custom number formatting, cell styles (alignment, indentation, rotation, borders, shading, protection, text wrapping, shrinking, etc.) and font formatting (size, color, font type, italic and strikeout properties, different levels of boldness, underlining, subscript and superscript).

Following example shows how to set different style-specific properties on Excel rows, columns and cells.

Excel styles and formatting set with GemBox.Spreadsheet for Java
Screenshot of Excel styles and formatting set with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Styles and Formatting");

        worksheet.getCell(0, 1).setValue("Cell style examples:");
        worksheet.getPrintOptions().setPrintGridlines(true);

        int row = 0;

        // Column width of 4, 30 and 36 characters.
        worksheet.getColumn(0).setWidth(4 * 256);
        worksheet.getColumn(1).setWidth(30 * 256);
        worksheet.getColumn(2).setWidth(36 * 256);

        worksheet.getCell(row += 2, 1).setValue(".Style.Borders.SetBorders(...)");
        worksheet.getCell(row, 2).getStyle().getBorders().setBorders(MultipleBorders.outside(), SpreadsheetColor.fromArgb(252, 1, 1), LineStyle.THIN);

        worksheet.getCell(row += 2, 1).setValue(".Style.FillPattern.SetPattern(...)");
        worksheet.getCell(row, 2).getStyle().getFillPattern().setPattern(FillPatternStyle.THIN_HORIZONTAL_CROSSHATCH, SpreadsheetColor.fromName(ColorName.GREEN), SpreadsheetColor.fromName(ColorName.YELLOW));

        worksheet.getCell(row += 2, 1).setValue(".Style.Font.Color =");
        worksheet.getCell(row, 2).setValue("Color.Blue");
        worksheet.getCell(row, 2).getStyle().getFont().setColor(SpreadsheetColor.fromName(ColorName.BLUE));

        worksheet.getCell(row += 2, 1).setValue(".Style.Font.Italic =");
        worksheet.getCell(row, 2).setValue("true");
        worksheet.getCell(row, 2).getStyle().getFont().setItalic(true);

        worksheet.getCell(row += 2, 1).setValue(".Style.Font.Name =");
        worksheet.getCell(row, 2).setValue("Comic Sans MS");
        worksheet.getCell(row, 2).getStyle().getFont().setName("Comic Sans MS");

        worksheet.getCell(row += 2, 1).setValue(".Style.Font.ScriptPosition =");
        worksheet.getCell(row, 2).setValue("ScriptPosition.Superscript");
        worksheet.getCell(row, 2).getStyle().getFont().setScriptPosition(ScriptPosition.SUPERSCRIPT);

        worksheet.getCell(row += 2, 1).setValue(".Style.Font.Size =");
        worksheet.getCell(row, 2).setValue("18 * 20");
        worksheet.getCell(row, 2).getStyle().getFont().setSize(18 * 20);

        worksheet.getCell(row += 2, 1).setValue(".Style.Font.Strikeout =");
        worksheet.getCell(row, 2).setValue("true");
        worksheet.getCell(row, 2).getStyle().getFont().setStrikeout(true);

        worksheet.getCell(row += 2, 1).setValue(".Style.Font.UnderlineStyle =");
        worksheet.getCell(row, 2).setValue("UnderlineStyle.Double");
        worksheet.getCell(row, 2).getStyle().getFont().setUnderlineStyle(UnderlineStyle.DOUBLE);

        worksheet.getCell(row += 2, 1).setValue(".Style.Font.Weight =");
        worksheet.getCell(row, 2).setValue("ExcelFont.BoldWeight");
        worksheet.getCell(row, 2).getStyle().getFont().setWeight(ExcelFont.BOLD_WEIGHT);

        worksheet.getCell(row += 2, 1).setValue(".Style.HorizontalAlignment =");
        worksheet.getCell(row, 2).setValue("HorizontalAlignmentStyle.Center");
        worksheet.getCell(row, 2).getStyle().setHorizontalAlignment(HorizontalAlignmentStyle.CENTER);

        worksheet.getCell(row += 2, 1).setValue(".Style.Indent");
        worksheet.getCell(row, 2).setValue("five");
        worksheet.getCell(row, 2).getStyle().setHorizontalAlignment(HorizontalAlignmentStyle.LEFT);
        worksheet.getCell(row, 2).getStyle().setIndent(5);

        worksheet.getCell(row += 2, 1).setValue(".Style.IsTextVertical = ");
        worksheet.getCell(row, 2).setValue("true");
        // Set row height to 60 points.
        worksheet.getRow(row).setHeight(60 * 20);
        worksheet.getCell(row, 2).getStyle().setTextVertical(true);

        worksheet.getCell(row += 2, 1).setValue(".Style.NumberFormat");
        worksheet.getCell(row, 2).setValue(1234);
        worksheet.getCell(row, 2).getStyle().setNumberFormat("#,##0.00 [$Krakozhian Money Units]");

        worksheet.getCell(row += 2, 1).setValue(".Style.Rotation");
        worksheet.getCell(row, 2).setValue("35 degrees up");
        worksheet.getCell(row, 2).getStyle().setRotation(35);

        worksheet.getCell(row += 2, 1).setValue(".Style.ShrinkToFit");
        worksheet.getCell(row, 2).setValue("This property is set to true so this text appears shrunk.");
        worksheet.getCell(row, 2).getStyle().setShrinkToFit(true);

        worksheet.getCell(row += 2, 1).setValue(".Style.VerticalAlignment =");
        worksheet.getCell(row, 2).setValue("VerticalAlignmentStyle.Top");
        // Set row height to 30 points.
        worksheet.getRow(row).setHeight(30 * 20);
        worksheet.getCell(row, 2).getStyle().setVerticalAlignment(VerticalAlignmentStyle.TOP);

        worksheet.getCell(row += 2, 1).setValue(".Style.WrapText");
        worksheet.getCell(row, 2).setValue("This property is set to true so this text appears broken into multiple lines.");
        worksheet.getCell(row, 2).getStyle().setWrapText(true);

        workbook.save("Styles and Formatting.%OutputFileType%");
    }
}

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet for Java yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.