Articles in this section
Category / Section

How to set a gradient background color for Excel charts in C#, VB.NET

2 mins read

Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files. Using this library, you can set a gradient background color for Excel charts in C# and VB.NET.

Steps to set gradient background color in Excel charts, programmatically:

Step 1: Create a new C# console application project.

Create a new C# console application

Create a new C# console application.

Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.

Install XlsIO NuGet package

Install the XlsIO NuGet package

Step 3: Include the following namespaces in the Program.cs file.

C#

using Syncfusion.XlsIO;
using System.Drawing;

 

VB.NET

Imports Syncfusion.XlsIO
Imports System.Drawing

 

Step 4: Use the following code snippet to set a gradient background color for Excel charts in C# and VB.NET.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    // Instantiate the Excel application object
    IApplication application = excelEngine.Excel;

    // Open a sample workbook that contains a chart
    IWorkbook workbook = application.Workbooks.Open("ChartTemplate.xlsx");

    // The first worksheet object in the worksheets collection is accessed
    IWorksheet worksheet = workbook.Worksheets[0];

    // Access the chart
    IChart chart = worksheet.Charts[0];

    // Set gradient background color to Excel chart
    chart.ChartArea.Fill.FillType = ExcelFillType.Gradient;
    chart.ChartArea.Fill.GradientStyle = ExcelGradientStyle.Horizontal;
    chart.ChartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
    chart.ChartArea.Fill.ForeColor = Color.FromArgb(255, 190, 32, 38);
    chart.ChartArea.Fill.BackColor = Color.FromArgb(255, 32, 190, 38);

    // Save the workbook
    workbook.SaveAs("Output.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    ' Instantiate the Excel application object
    Dim application As IApplication = excelEngine.Excel

    ' Open a sample workbook that contains a chart
    Dim workbook As IWorkbook = application.Workbooks.Open("ChartTemplate.xlsx")

    ' The first worksheet object in the worksheets collection is accessed
    Dim worksheet As IWorksheet = workbook.Worksheets(0)

    ' Access the chart
    Dim chart As IChart = worksheet.Charts(0)

    ' Set gradient background color to Excel chart
    chart.ChartArea.Fill.FillType = ExcelFillType.Gradient
    chart.ChartArea.Fill.GradientStyle = ExcelGradientStyle.Horizontal
    chart.ChartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor
    chart.ChartArea.Fill.ForeColor = Color.FromArgb(255, 190, 32, 38)
    chart.ChartArea.Fill.BackColor = Color.FromArgb(255, 32, 190, 38)

    ' Save the workbook
    workbook.SaveAs("Output.xlsx")
End Using

 

A complete working example of how to set a gradient background color for Excel charts in C# and VB.NET can be downloaded from Excel-charts-with-gradient-background-color.zip.

By executing the program, you will get the output Excel document as shown below.

Output Excel document

Output Excel document

Refer here to explore the rich set of Syncfusion Essential XlsIO features.

See Also:

How to change the color of a single Data point in XlsIO chart?

How to apply or change colors in Excel pie chart?

How can I change the font color of the title in a Chart?

How can we change the color of the gridlines in XlsIO chart?

How to set trendlines for Excel chart series in C#, VB.NET?

Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.

Refer here to explore the rich set of Syncfusion Essential XlsIO features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about how to set a gradient background color for Excel charts in C#, VB.NET in XlsIO.

 

You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied