We have Excel workbooks where we’ve created custom cell styles that we re-use and need to modify via C# code. Is there a way to get a collection of all of the cell styles used in a workbook? I see how to get a style used in a specific cell but not the actual styles that are re-used within the workbook.
Thank you.
@mattgabriel
Yes – Aspose.Cells lets you work with the complete style collection of a workbook.
All styles (built‑in and user‑defined) are stored in the Workbook.Styles collection, which you can enumerate, read, modify, add or delete.
Typical workflow (C#)
using Aspose.Cells;
using System.Drawing; // for Color
class Program
{
static void Main()
{
// Load the workbook
Workbook wb = new Workbook(@"C:\Data\MyWorkbook.xlsx");
// Get the global style collection
Styles styles = wb.Styles;
// Enumerate every style
foreach (Style style in styles)
{
// Show the style name and its index
Console.WriteLine($"Index: {style.Index}, Name: {style.Name}, Built‑in: {style.IsBuiltIn}");
// Example: change the font colour of all *custom* styles
if (!style.IsBuiltIn) // only user‑defined styles
{
style.Font.Color = Color.DarkRed;
}
}
// Save the workbook with the modifications
wb.Save(@"C:\Data\MyWorkbook_Modified.xlsx");
}
}
What the code does
| Step |
Explanation |
new Workbook(path) |
Loads the Excel file. |
wb.Styles |
Returns the workbook‑wide Styles collection. |
foreach (Style style in styles) |
Enumerates every style (built‑in + custom). |
style.IsBuiltIn |
true for built‑in styles (Normal, Good, …); false for user‑defined ones. |
style.Font.Color = … |
Modifies a property of the style; the change is reflected in every cell that uses that style. |
wb.Save(path) |
Writes the updated workbook to disk. |
Key points
| Topic |
Details |
| Accessing the collection |
Workbook.Styles (or wb.Styles) – not tied to any worksheet. |
| Retrieving a style by index |
Style s = wb.Styles[3]; |
| Retrieving a style by name |
Style s = wb.Styles["MyCustomStyle"]; |
| Identifying custom styles |
Use style.IsBuiltIn (or check that style.Name is not one of the built‑in names). |
| Modifying a style |
Changing a property updates all cells that reference the style – no need to walk through cells. |
| Adding a new style |
csharp int idx = wb.Styles.Add(); Style newStyle = wb.Styles[idx]; // configure newStyle |
| Assigning a style to a cell |
cell.SetStyle(newStyle); |
| Removing a style |
wb.Styles.RemoveAt(index); – only safe if the style is not used by any cell. |
Quick reference
| API |
Purpose |
Workbook.Styles |
Returns the Styles collection for the whole workbook. |
Styles.Count |
Number of styles present. |
Style.Name |
Name of the style (empty for some built‑in styles). |
Style.Index |
Zero‑based index inside the collection. |
Style.IsBuiltIn |
Indicates whether the style is a built‑in Excel style. |
Styles.Add() |
Adds a new empty style and returns its index. |
Styles.RemoveAt(int index) |
Deletes a style (use only when you know it is unused). |
Cell.GetStyle() / Cell.SetStyle(Style) |
Retrieve or assign a style to a specific cell. |
TL;DR
Workbook wb = new Workbook("file.xlsx");
Styles styles = wb.Styles; // all workbook styles
foreach (Style s in styles)
{
if (!s.IsBuiltIn) s.Font.Color = Color.DarkRed; // example modification
}
wb.Save("file_modified.xlsx");
This gives you full, programmatic access to every style defined in the workbook, allowing you to read, modify, add, or delete them as needed.
Was this added in a recent update? I am currently using 25.8 and wb.Styles does not exist in this version.
@mattgabriel,
Hello, and welcome to Aspose Cells Cloud forum!
To assist you quickly, please let us know which product you’re using:
Aspose.Cells Cloud SDK for .NET, or
Aspose.Cells for .NET
Note: The Cloud API currently does not offer a built-in batch cell-info endpoint. If you need this capability, we can create a custom feature for you—just let us know your requirements.