Themes and colors help you enhance the overall appearance of the workbook. The appearance of the workbook contributes significantly to the ability to read, understand, and work with it. DsExcel supports both built-in and custom themes and colors, as described in the following sections:
DsExcel provides setTheme method of Workbook class and IWorkbook interface that allows you to set a theme for the workbook. You can change the current theme of the workbook using setTheme method and Themes class. The default theme of a workbook is the standard Office theme.
Furthermore, DsExcel also provides Theme class that enables you to create a custom theme and apply it to set up a workbook according to your preferences and requirements.
When a theme is changed, it affects all areas, including the theme font, theme color, range, chart title, etc. For instance, if you apply a built-in or a custom theme to your workbook, it is likely that the color of the range as well as the font will also be changed in accordance with the modified theme.
Refer to the following example code to set a built-in theme:
Java |
Copy Code |
---|---|
// Change workbook's theme to Berlin.
workbook.setTheme(Themes.GetBerlin()); |
Refer to the following example code to set a custom theme:
Java |
Copy Code |
---|---|
// Add custom theme. Base theme is office theme, if parameters are not given. Theme theme = new Theme("testTheme"); theme.getThemeColorScheme().get(ThemeColor.Light1).setRGB(Color.GetAntiqueWhite()); theme.getThemeColorScheme().get(ThemeColor.Accent1).setRGB(Color.GetAliceBlue()); theme.getThemeFontScheme().getMajor().get(FontLanguageIndex.Latin).setName("Buxton Sketch"); theme.getThemeFontScheme().getMinor().get(FontLanguageIndex.Latin).setName("Segoe UI"); // Apply theme. workbook.setTheme(theme); |
DsExcel allows you to set the color of a cell, cell border, tab, etc., using theme colors, standard colors, and custom colors. You can set these colors using stringToColor method of ColorUtilities class, setThemeColor method, and ThemeColor enumeration.
setThemeColor method sets the built-in theme colors using ThemeColor enumeration. Whereas stringToColor method of ColorUtilities class sets the standard and custom colors using different color string formats, such as:
Refer to the following example code to set the interior and border color of a cell to theme color:
Java |
Copy Code |
---|---|
// Set interior and border color to theme color. worksheet.getRange("D2").getInterior().setThemeColor(ThemeColor.Accent1); worksheet.getRange("D2").getBorders().setThemeColor(ThemeColor.Accent2); |
Refer to the following example code to set the interior and border color of a range to standard color:
Java |
Copy Code |
---|---|
// Set interior and border color to standard color. worksheet.getRange("B4:D6").getInterior().setColor(ColorUtilities.stringToColor("yellow")); worksheet.getRange("C4:D6").getBorders().setColor(ColorUtilities.stringToColor("black")); |
Refer to the following example code to set the interior and border colors of a range to a custom color using RGB and hexadecimal color strings:
Java |
Copy Code |
---|---|
// Set interior and border color to custom color. worksheet.getRange("B7:B18").getInterior().setColor(ColorUtilities.stringToColor("rgb(211,211,211)")); worksheet.getRange("B7:D18").getBorders().setColor(ColorUtilities.stringToColor("#000000")); |