本文将介绍通过Spire.XLS 来设置Excel图表中的图例背景色填充,包括:
- 单色填充
- 渐变色填充
- 纹理填充
- 加载图片填充
测试文档:
C#
//实例化Workbook类的对象,并加载测试文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//获取工作表中的第一个图表
Worksheet ws = workbook.Worksheets[0];
Spire.Xls.Chart chart = ws.Charts[0];
//填充图例背景色
XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
//单色填充
x.Fill.FillType = ShapeFillType.SolidColor;
x.ForeGroundColor = Color.Gainsboro;
////渐变色填充
//x.Fill.FillType = ShapeFillType.Gradient;
//x.ForeGroundColor = Color.AliceBlue;
//x.BackGroundColor = Color.Bisque;
////纹理填充
//x.Fill.FillType = ShapeFillType.Texture;
//x.Fill.Texture = GradientTextureType.Bouquet;
////图片填充
//x.Fill.CustomPicture("tp.png");
//保存文档
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
VB.NET
'实例化Workbook类的对象,并加载测试文档
Dim workbook As New Workbook()
workbook.LoadFromFile("test.xlsx")
'获取工作表中的第一个图表
Dim ws As Worksheet = workbook.Worksheets(0)
Dim chart As Spire.Xls.Chart = ws.Charts(0)
'填充图例背景色
Dim x As XlsChartFrameFormat = TryCast(chart.Legend.FrameFormat, XlsChartFrameFormat)
'单色填充
x.Fill.FillType = ShapeFillType.SolidColor
x.ForeGroundColor = Color.Gainsboro
'渐变色填充
'x.Fill.FillType = ShapeFillType.Gradient;
'x.ForeGroundColor = Color.AliceBlue;
'x.BackGroundColor = Color.Bisque;
'纹理填充
'x.Fill.FillType = ShapeFillType.Texture;
'x.Fill.Texture = GradientTextureType.Bouquet;
'图片填充
'x.Fill.CustomPicture("tp.png");
'保存文档
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
单色填充效果:
渐变填充效果:
纹理填充效果:
图片填充效果: