Excel 主题和颜色

获取和设置主题颜色

Aspose.Cells API 提供了自定义主题和颜色的功能。以下是几种实现主题颜色的方法和属性。

  • Style.ForegroundThemeColor 属性可用于设置前景色。
  • Style.BackgroundThemeColor 属性可用于设置背景颜色。
  • Font.ThemeColor 属性可用于设置字体颜色。
  • Workbook.getThemeColor 方法可用于获取主题颜色。
  • Workbook.setThemeColor 方法可用于设置主题颜色。

以下示例演示如何获取和设置主题颜色。

以下示例使用了一个模板 XLSX 文件,获取了不同主题颜色类型的颜色,更改了颜色,然后保存了 Microsoft Excel 文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(GetSetThemeColors.class);
// Instantiate Workbook object
// Open an exiting excel file
Workbook workbook = new Workbook(dataDir + "book1.xlsx");
// Get the Background1 theme color
Color c = workbook.getThemeColor(ThemeColorType.BACKGROUND_1);
// Print the color
System.out.println("theme color Background1: " + c);
// Get the Accent2 theme color
c = workbook.getThemeColor(ThemeColorType.ACCENT_1);
// Print the color
System.out.println("theme color Accent2: " + c);
// Change the Background1 theme color
workbook.setThemeColor(ThemeColorType.BACKGROUND_1, Color.getRed());
// Get the updated Background1 theme color
c = workbook.getThemeColor(ThemeColorType.BACKGROUND_1);
// Print the updated color for confirmation
System.out.println("theme color Background1 changed to: " + c);
// Change the Accent2 theme color
workbook.setThemeColor(ThemeColorType.ACCENT_1, Color.getBlue());
// Get the updated Accent2 theme color
c = workbook.getThemeColor(ThemeColorType.ACCENT_1);
// Print the updated color for confirmation
System.out.println("theme color Accent2 changed to: " + c);
// Save the updated file
workbook.save(dataDir + "GetAndSetThemeColorBook.xlsx");

自定义主题

以下示例演示如何应用带有您所需颜色的自定义主题。该示例使用了在 Microsoft Excel 2007 中手动创建的样本模板文件。

模板 CustomThemeColor.xlsx 文件

todo:image_alt_text

下面的示例加载一个模板XLSX文件,为不同的主题颜色类型定义颜色,应用自定义颜色并保存Excel文件。

使用自定义主题颜色生成的文件

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CustomizingThemes.class);
// Define Color array (of 12 colors) for the Theme
Color[] carr = new Color[12];
carr[0] = Color.getAntiqueWhite(); // Background1
carr[1] = Color.getBrown(); // Text1
carr[2] = Color.getAliceBlue(); // Background2
carr[3] = Color.getYellow(); // Text2
carr[4] = Color.getYellowGreen(); // Accent1
carr[5] = Color.getRed(); // Accent2
carr[6] = Color.getPink(); // Accent3
carr[7] = Color.getPurple(); // Accent4
carr[8] = Color.getPaleGreen(); // Accent5
carr[9] = Color.getOrange(); // Accent6
carr[10] = Color.getGreen(); // Hyperlink
carr[11] = Color.getGray(); // Followed Hyperlink
// Instantiate a Workbook
// Open the spreadsheet file
Workbook workbook = new Workbook(dataDir + "book1.xlsx");
// Set the custom theme with specified colors
workbook.customTheme("CustomeTheme1", carr);
// Save as the excel file
workbook.save(dataDir + "CustomThemeColor.xlsx");

使用主题颜色

下面的示例根据工作簿的默认主题颜色类型,应用单元格的前景色和字体颜色。它还将Excel文件保存到磁盘。

执行代码时会生成以下输出。

工作表的单元格 D3 应用的主题颜色

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(UseThemeColors.class);
// Instantiate a Workbook
Workbook workbook = new Workbook();
// Get cells collection in the first (default) worksheet
Cells cells = workbook.getWorksheets().get(0).getCells();
// Get the D3 cell
Cell c = cells.get("D3");
// Get the style of the cell
Style s = c.getStyle();
// Set background color for the cell from the default theme Accent2 color
s.setBackgroundThemeColor(new ThemeColor(ThemeColorType.ACCENT_2, 0.5));
// Set the pattern type
s.setPattern(BackgroundType.SOLID);
// Get the font for the style
Font f = s.getFont();
// Set the theme color
f.setThemeColor(new ThemeColor(ThemeColorType.ACCENT_4, 0.1));
// Apply style
c.setStyle(s);
// Put a value
c.putValue("Testing");
// Save the excel file
workbook.save(dataDir + "UseThemeColors.xlsx");

高级主题