Background color can be filled with different styles, including pure color background, gradient background, image background or texture background. The following sections will introduce how to set the background color of the chart in Excel and the legend background color in the chart through C #.
Use tools: Spire. XLS for. NET
DLL reference: After downloading and installing, pay attention to adding a reference Spire. Xls. DLL (dll file is obtained in Bin folder under the installation path) to the program.
[Example 1] Fill in the background color of the chart
The test documents are as follows:
Step1: Loading documents
// Objects that instantiate Workbook classes
Workbook workbook = new Workbook();
// Loading test documents
workbook.LoadFromFile("sample.xlsx");
Step2: Get the chart
// Get the first chart in the first worksheet
Worksheet ws = workbook.Worksheets[1];
Chart chart = ws.Charts[0];
Step3: Fill in the background color of the chart (pure color)
// Set the background fill color of the chart (solid color)
chart.ChartArea.Fill.ForeColor = Color.LightSkyBlue;
//// Set the background fill color (solid color) for the drawing area of the chart
///chart.PlotArea.ForeGroundColor =System.Drawing.Color.LightYellow;
Step4: Load the picture to fill in the chart background
// Load the picture and fill the entire chart area
chart.ChartArea.Fill.CustomPicture(Image.FromFile("tp.png"), "None");
// Set the transparency of picture filling in the drawing area
chart.PlotArea.Fill.Transparency = 0.8;
//// Load the picture and fill only the drawing area in the chart
//chart.PlotArea.Fill.CustomPicture(Image.FromFile("tp.png"), "None");
Step5: Save Documents
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
Pure background color setting effect:
1. Fill in the whole chart
2. Fill in the drawing area in the chart
Picture filling effect:
1. Fill in the whole chart
2. Fill in the drawing area in the chart
All code:
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Charts;
using System.Drawing;
namespace FillChartWithImg_XLS
{
class Program
{
static void Main(string[] args)
{
// Objects that instantiate Workbook classes
Workbook workbook = new Workbook();
// Loading test documents
workbook.LoadFromFile("sample.xlsx");
// Get the first chart in the first worksheet
Worksheet ws = workbook.Worksheets[1];
Chart chart = ws.Charts[0];
// Set the background fill color of the chart (solid color)
chart.ChartArea.Fill.ForeColor = Color.LightSkyBlue;
// Set the background fill color (solid color) for the drawing area of the chart
//chart.PlotArea.ForeGroundColor = System.Drawing.Color.LightYellow;
//// Load the picture and fill the entire chart area
//chart.ChartArea.Fill.CustomPicture(Image.FromFile("tp.png"), "None");
//// Set the transparency of picture filling in the drawing area
//chart.PlotArea.Fill.Transparency = 0.8;
//// Load the picture and fill only the drawing area in the chart
//chart.PlotArea.Fill.CustomPicture(Image.FromFile("tp.png"), "None");
// Save Documents
workbook.SaveToFile("result3.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result3.xlsx");
}
}
}
[Example 2] Fill in the legend background color in the chart
Test charts:
Step1: Loading documents
// Instantiate the object of the Workbook class and load the test document
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
Step2: Get the chart
// Get the first chart in the worksheet
Worksheet ws = workbook.Worksheets[0];
Spire.Xls.Chart chart = ws.Charts[0];
Step3: Monochromatic Filled Legend Background
// Fill in legend background color
XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
// Monochromatic Filling
x.Fill.FillType = ShapeFillType.SolidColor;
x.ForeGroundColor = Color.Gainsboro;
Step4: Gradient Filling Legend Background
// gradient filling
x.Fill.FillType = ShapeFillType.Gradient;
x.ForeGroundColor = Color.AliceBlue;
x.BackGroundColor = Color.Bisque;
Step5: Texture Filling Legend Background
// Texture Filling
x.Fill.FillType = ShapeFillType.Texture;
x.Fill.Texture = GradientTextureType.Bouquet;
Step6: Load the picture to fill the legend background
// Picture Filling
x.Fill.CustomPicture("img.png");
Step7: Save Documents
workbook.SaveToFile("output.xlsx", ExcelVersion.Version2010);
Legend Background Color Filling Effect:
1. Monochromatic filling effect
2. Gradual Filling Effect
3. Texture Filling Effect
4. Picture Filling Effect
All code:
using Spire.Xls;
using Spire.Xls.Charts;
using Spire.Xls.Core.Spreadsheet.Charts;
using System.Drawing;
namespace SetFontOfLegendInChart_XLS
{
class Program
{
static void Main(string[] args)
{
//Instantiate the object of the workbook class and load the test document
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
// Get the first chart in the worksheet
Worksheet ws = workbook.Worksheets[0];
Spire.Xls.Chart chart = ws.Charts[0];
// Fill in legend background color
XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
// Monochromatic Filling
x.Fill.FillType = ShapeFillType.SolidColor;
x.ForeGroundColor = Color.Gainsboro;
//// gradient filling
//x.Fill.FillType = ShapeFillType.Gradient;
//x.ForeGroundColor = Color.AliceBlue;
//x.BackGroundColor = Color.Bisque;
//// Texture Filling
//x.Fill.FillType = ShapeFillType.Texture;
//x.Fill.Texture = GradientTextureType.Bouquet;
//// Picture Filling
//x.Fill.CustomPicture("img.png");
// Save Documents
workbook.SaveToFile("output.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("output.xlsx");
}
}
}
summary
Above is the example code of C# filling Excel chart and legend background color introduced by Xiaobian. Need friends to follow Xiaobian to have a look!