Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Don't work nested styles (CreateNamedStyle) #195

Closed
@j3utajisi

Description

I try to use CreateNamedStyle with Templates.

	ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet 1");
            var mainStyle = package.Workbook.Styles.CreateNamedStyle("Main");
            mainStyle.Style.Font.Name = "Calibri";
            mainStyle.Style.Font.Size = 7;
            mainStyle.Style.WrapText = true;
            mainStyle.Style.VerticalAlignment = ExcelVerticalAlignment.Top;

            var borderStyle = package.Workbook.Styles.CreateNamedStyle("Border", mainStyle.Style);
            borderStyle.Style.Border.Top.Style = ExcelBorderStyle.Thin;
            borderStyle.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
            borderStyle.Style.Border.Left.Style = ExcelBorderStyle.Thin;
            borderStyle.Style.Border.Right.Style = ExcelBorderStyle.Thin;
            borderStyle.Style.Border.Bottom.Color.SetColor(Color.Black);
            borderStyle.Style.Border.Top.Color.SetColor(Color.Black);
            borderStyle.Style.Border.Left.Color.SetColor(Color.Black);
            borderStyle.Style.Border.Right.Color.SetColor(Color.Black);

            var headerStyle = package.Workbook.Styles.CreateNamedStyle("header", borderStyle.Style);
            headerStyle.Style.Font.Bold = true;
            headerStyle.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            headerStyle.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

I catch bug System.ThrowArgumentOutOfRangeException

I used to several methods for fix it. The best things for me was "crutch":
I init a cell before CreateNamedStyle. For fix problem I create more styles than have my nested styles.

            #region Init syles
            worksheet.Cells["A1"].Style.Font.Bold = true;
            worksheet.Cells["A1"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            worksheet.Cells["A1"].Style.Fill.BackgroundColor.SetColor(Color.Aqua);
            worksheet.Cells["A1"].Style.Fill.BackgroundColor.SetColor(Color.Beige);
            worksheet.Cells["A1"].Style.Fill.BackgroundColor.SetColor(Color.Red);
            worksheet.Cells["A1"].Style.Border.Top.Style = ExcelBorderStyle.DashDot;
            worksheet.Cells["A1"].Style.Border.Left.Style = ExcelBorderStyle.DashDot;
            worksheet.Cells["A1"].Style.Border.Right.Style = ExcelBorderStyle.DashDot;
            worksheet.Cells["A1"].Style.Border.Bottom.Style = ExcelBorderStyle.DashDot;
            worksheet.Cells["A1"].Style.Numberformat.Format = "#,##0.0";
            worksheet.Cells["A1"].Style.Border.Top.Color.SetColor(Color.Black);
            worksheet.Cells["A1"].Style.Border.Bottom.Color.SetColor(Color.Black);
            worksheet.Cells["A1"].Style.Border.Left.Color.SetColor(Color.Black);
            worksheet.Cells["A1"].Style.Border.Right.Color.SetColor(Color.Black);
            ExcelRange styleRng = worksheet.Cells["A1"];
            #endregion

This is method work for me, but it is crutch". Could you help me resolve my problem correctly.
Sorry for my English :)

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions