We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set cell to use a built in style like Accent1 using .net

Using the cells .net api; how can I set a cell style to use a build in cell style like “Accent1”

excel style.png (20.0 KB)

Please try the code below to achieve your requirements:

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];

// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];            

// Adding some value to the "A1" cell

// Defining a Style object
Style style;

// Get the style from A1 cell
style = cell.GetStyle();

// Setting the vertical alignment
style.VerticalAlignment = TextAlignmentType.Center;

// Setting the horizontal alignment
style.HorizontalAlignment = TextAlignmentType.Center;

// Setting the font color of the text
style.Font.Color = Color.White;

//change cell fill color here
style.ForegroundColor = Color.BlueViolet;

style.Pattern = BackgroundType.Solid;

// Setting to shrink according to the text contained in it
style.ShrinkToFit = true;

// Applying the style to A1 cell

// Saving the Excel file
workbook.Save(path + "accent_style.xlsx");

You can visit the document related to Cell formatting for more information: https://docs.aspose.com/cells/net/cells-formatting/

Let us know your feedback.


Please see the following sample code on how to set the “Accent1” Themed cell style for your reference:
Sample code:

Workbook wb = new Workbook(FileFormatType.Xlsx);
            Style headerStyle = wb.CreateStyle();
            headerStyle.ForegroundThemeColor = new ThemeColor(ThemeColorType.Accent1, 100);
            headerStyle.Pattern = BackgroundType.Solid;
            headerStyle.Font.Color = Color.White;//you may set other font attributes if you want.
            headerStyle.Name = "HeaderStyle";

            Style rowStyle = wb.CreateStyle();
            rowStyle.ForegroundColor = Color.LightGreen;
            rowStyle.Pattern = BackgroundType.Solid;

            Style alternatingRowStyle = wb.CreateStyle();
            alternatingRowStyle.ForegroundColor = Color.Yellow;
            alternatingRowStyle.Pattern = BackgroundType.Solid;

            Worksheet ws = wb.Worksheets[0];

            // Set the columns headers with style.
            for (int i = 0; i < 10; i++)
                ws.Cells[0, i].PutValue("Column #" + i.ToString());
                ws.Cells[0, i].SetStyle(headerStyle);

            // Add some rows for the columns
            for (int row = 1; row < 5; row++)
                for (int col = 0; col < 10; col++)
                    ws.Cells[row, col].PutValue(col);
                    if (row % 2 == 0)
                        ws.Cells[row, col].SetStyle(rowStyle);
                        ws.Cells[row, col].SetStyle(alternatingRowStyle);


Hope, this helps a bit.