MsoFillFormatHelper.SetOneColorGradient

MsoFillFormatHelper.SetOneColorGradient method

Sets the specified fill to a one-color gradient.

public void SetOneColorGradient(Color color, double degree, GradientStyleType style, int variant)
ParameterTypeDescription
colorColorOne gradient color.
degreeDoubleThe gradient degree. Can be a value from 0.0 (dark) through 1.0 (light).
styleGradientStyleTypeGradient shading style.
variantInt32The gradient variant. Can be a value from 1 through 4, corresponding to one of the four variants on the Gradient tab in the Fill Effects dialog box. If style is GradientStyle.FromCenter, the Variant argument can only be 1 or 2.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using Aspose.Cells.Drawing;
    using System;
    using System.Drawing;

    public class MsoFillFormatHelperMethodSetOneColorGradientWithColorDoubleGradientStyleTyInt32Demo
    {
        public static void Run()
        {
            // Create a new workbook and get the first worksheet
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Put a simple label in the sheet for context
            worksheet.Cells["A1"].Value = "Gradient Fill Demo";

            try
            {
                // Create an instance of MsoFillFormatHelper using reflection (no public ctor)
                MsoFillFormatHelper fillHelper = (MsoFillFormatHelper)Activator.CreateInstance(
                    typeof(MsoFillFormatHelper), nonPublic: true);

                // Parameters for the gradient
                Color gradientColor = Color.FromArgb(255, 0, 120, 215); // a shade of blue
                double degree = 45.0;                                 // gradient angle in degrees
                GradientStyleType style = GradientStyleType.Horizontal; // horizontal gradient
                int variant = 0;                                      // variant (0 = default)

                // Apply the one‑color gradient
                fillHelper.SetOneColorGradient(gradientColor, degree, style, variant);

                // Optional: show that the properties can be read after the call
                Console.WriteLine($"ForeColor after gradient: {fillHelper.ForeColor}");
                Console.WriteLine($"ForeColorTransparency after gradient: {fillHelper.ForeColorTransparency}");

                // Save the workbook (no visual change will be seen because the helper is not attached to a shape)
                workbook.Save("SetOneColorGradientDemo.xlsx");

                Console.WriteLine("SetOneColorGradient method called successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error calling SetOneColorGradient: {ex.Message}");
            }
        }
    }
}

See Also