本文介绍了 使用 C# 应用 Excel 颜色主题 的详细信息。它包含编程步骤、IDE 配置详细信息以及使用 C#** 应用 **Excel 电子表格主题的示例代码。您将了解在工作表中设置主题颜色所需的各种属性和方法。
使用 C# 设置 Excel 主题的步骤
- 设置环境以使用 Aspose.Cells for .NET 应用 Excel 主题
- 创建一个 workbook 并从其默认工作表访问单元格
- 获取单元格的样式并应用 foreground theme color 和模式
- 从样式访问字体并设置其主题颜色
- 将样式设置回单元格并放入一些文本进行测试
- 使用应用的主题保存工作簿
这些步骤总结了使用 C#* 使用 *Excel 主题的过程。通过访问工作簿中指定工作表中单元格的样式属性来开始该过程。在将此更新的样式保存到输出 Excel 文件中的单元格之前,设置 ForegroundThemeColor、Pattern 和字体 ThemeColor。
使用 C# 应用 Microsoft Excel 主题的代码
using System; | |
using Aspose.Cells; | |
class Program | |
{ | |
static void Main(string[] args) // Apply Themes to Excel in C# | |
{ | |
new License().SetLicense("License.lic"); | |
// Instantiate a Workbook. | |
Workbook workbook = new Workbook(); | |
// Get cells collection | |
Cells cells = workbook.Worksheets[0].Cells; | |
// Get the A3 cell | |
Aspose.Cells.Cell c = cells["A3"]; | |
// Get the style | |
Style s = c.GetStyle(); | |
// Set foreground theme color | |
s.ForegroundThemeColor = new ThemeColor(ThemeColorType.Accent4, 0.5); | |
// Set the pattern | |
s.Pattern = BackgroundType.DiagonalStripe; | |
// Get the font | |
Aspose.Cells.Font f = s.Font; | |
// Set the theme color | |
f.ThemeColor = new ThemeColor(ThemeColorType.Accent2, 0.4); | |
// Apply style. | |
c.SetStyle(s); | |
// Put a value. | |
c.PutValue("Testing1"); | |
// Save the excel file. | |
workbook.Save("output.xlsx"); | |
Console.WriteLine("Theme colors applied to Excel successfully"); | |
} | |
} |
此代码演示了如何在单元格中使用 C#* 中的 *Excel 工作簿主题。如果需要,您可以为一系列单元格或整个工作表设置此主题。使用 ThemeColorType 枚举器通过根据您的选择设置各种主题颜色来即兴编写代码。
本文教我们如何为不同的单元格使用Excel主题颜色。如果您想在 Excel 中应用条件格式,请参阅有关 如何在C#中的Excel中应用条件格式 的文章。