Have seen a lot of threads that styles not applied to a Cell, but for cell everything works fine.
What i’m trying to do to set a global style for Cells that has a property Style
var cellsFactory = new CellsFactory();
worksheet.Cells.Style = cellsFactory.CreateStyle();
worksheet.Cells.Style.Pattern = BackgroundType.ThickDiagonalCrosshatch; // Solid doesnt help
worksheet.Cells.Style.ForegroundColor = ColorTranslator.FromHtml(TableStyles.Where(x => x.StyleKey == "background").FirstOrDefault().Value);
worksheet.Cells.Style.Update();
this case from above is my last attempt where i’m creating setting and updating but even this doesn’t work. It just ignoring any values i’m passing to it
@bloodboilAaromatic
It seems that you are trying to set a global style for the cells in your worksheet, but the styles are not being applied as expected. Here are a few points to consider that might help you resolve the issue:
-
Setting Styles on Cells: To apply a style to a range of cells or individual cells, you should use the SetStyle
method. This method allows you to apply a specific style to a range or a single cell. For example:
var workbook = new Workbook();
var style = workbook.CreateStyle();
style.Pattern = BackgroundType.ThickDiagonalCrosshatch;
style.ForegroundColor = ColorTranslator.FromHtml("#FF0000"); // Example color
Cells cells = workbook.Worksheets[0].Cells;
Range range = cells.CreateRange("A1:C10");
range.SetStyle(style); // Apply style to the range
-
Updating Styles: When you create a style and set its properties, make sure to apply it using the SetStyle
method rather than just updating the Style
property of the Cells
object. The Update
method you are calling may not be necessary in this context.
-
Named Styles: If you are using named styles, ensure that you are not inadvertently losing the association with the style name when applying it to cells. If you pass a second parameter to the SetStyle
method, it can cause the style name to be ignored, which might lead to unexpected behavior.
-
Example Code: Here’s a complete example of how to create and apply a style to a range of cells:
var workbook = new Workbook();
var cellsFactory = new CellsFactory();
var style = cellsFactory.CreateStyle();
style.Pattern = BackgroundType.ThickDiagonalCrosshatch;
style.ForegroundColor = ColorTranslator.FromHtml("#FF0000"); // Example color
Cells cells = workbook.Worksheets[0].Cells;
Range range = cells.CreateRange("A1:C10");
range.SetStyle(style); // Apply the style to the range
workbook.Save("output.xlsx");
By following these steps, you should be able to apply the desired styles to your cells effectively. If you continue to experience issues, please double-check the properties you are setting and ensure that they are compatible with the style you are trying to apply.
And again, i’m not allowed right now to update aspose.cells so i’m not LTS version, my version is 24.10
@bloodboilAaromatic,
Generally, Style.Update() method is valid for named styles or built-in styles only. It is not for unnamed styles or associated to cells collection in the worksheet. Please see the document on modifying an existing style for your reference.
I think you may specify the default style of the workbook. You can make use of DefaultStyle attribute to set/get for it. See the document for your reference.
https://docs.aspose.com/cells/net/create-style-object-using-cellsfactory-class/