سمات وألوان Excel
توفر النسق مظهرًا موحدًا مع أنماط مسماة وتأثيرات رسومية وكائنات أخرى مستخدمة في مصنف. على سبيل المثال ، يبدو نمط Accent1 مختلفًا في نسق Office و Apex. في كثير من الأحيان ، تقوم بتطبيق نسق المستند ثم تعديله حسب احتياجاتك.
تطبيق السمات في Microsoft Excel
الحصول على ألوان النسق وتعيينها
توفر واجهات برمجة التطبيقات Aspose.Cells ميزات لتخصيص السمات والألوان. فيما يلي بعض الطرق والخصائص التي تنفذ ألوان النسق.
- يمكن استخدام خاصية Style.ForegroundThemeColor لتعيين لون المقدمة.
- يمكن استخدام خاصية Style.BackgroundThemeColor لتعيين لون الخلفية.
- يمكن استخدام خاصية Font.ThemeColor لتعيين لون الخط.
- يمكن استخدام طريقة Workbook.getThemeColor للحصول على لون النسق.
- يمكن استخدام طريقة Workbook.setThemeColor لتعيين لون النسق.
يوضح المثال التالي كيفية الحصول على ألوان النسق وتعيينها.
يستخدم المثال التالي ملف قالب XLSX ، ويحصل على ألوان لأنواع مختلفة من ألوان النسق ، ويغير الألوان ويحفظ ملف Excel Microsoft.
// 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
يقوم المثال التالي بتحميل ملف قالب XLSX ، وتحديد الألوان لأنواع ألوان السمة المختلفة ، وتطبيق الألوان المخصصة وحفظ ملف 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(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 من ورقة العمل
// 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"); |