Spire.XLS支持创建多种不同类型的图表,同时也支持对Excel表格中已有的图表进行保存、编辑等操作。本篇文章将介绍使用Spire.XLS来设置Excel的图表背景色。要点包括:
- 纯色背景填充
- 加载图片填充
注:这里填充背景,可分为填充整个图表、填充图表中的绘图区域2种情况。
测试文档的图表:
1. 设置纯色背景填充
C#
//实例化Workbook类的对象
Workbook workbook = new Workbook();
//加载测试文档
workbook.LoadFromFile("sample.xlsx");
//获取第2个工作表中的第一个图表
Worksheet ws = workbook.Worksheets[1];
Chart chart = ws.Charts[0];
//设置图表的背景填充色(纯色)
chart.ChartArea.Fill.ForeColor = Color.LightSkyBlue;
////设置图表绘图区域的背景填充色(纯色)
//chart.PlotArea.ForeGroundColor = Color.LightGreen;
//保存文档
workbook.SaveToFile("result1.xlsx", ExcelVersion.Version2010);
VB.NET
'实例化Workbook类的对象
Dim workbook As New Workbook()
'加载测试文档
workbook.LoadFromFile("sample.xlsx")
'获取第2个工作表中的第一个图表
Dim ws As Worksheet = workbook.Worksheets(1)
Dim chart As Chart = ws.Charts(0)
'设置图表的背景填充色(纯色)
chart.ChartArea.Fill.ForeColor = Color.LightSkyBlue
'设置图表绘图区域的背景填充色(纯色)
'chart.PlotArea.ForeGroundColor = Color.LightGreen;
'保存文档
workbook.SaveToFile("result1.xlsx", ExcelVersion.Version2010)
填充整个图表:
填充绘图区域:
2. 加载图片填充
C#
//实例化Workbook类的对象
Workbook workbook = new Workbook();
//加载测试文档
workbook.LoadFromFile("sample.xlsx");
//获取第2个工作表中的第一个图表
Worksheet ws = workbook.Worksheets[1];
Chart chart = ws.Charts[0];
//加载图片,填充整个图表区域
chart.ChartArea.Fill.CustomPicture(Image.FromFile("tp.png"), "None");
//设置绘图区域的图片填充的透明度
chart.PlotArea.Fill.Transparency = 0.8;
////加载图片,仅填充图表中的绘图区域
//chart.PlotArea.Fill.CustomPicture(Image.FromFile("tp.png"), "None");
//保存文档
workbook.SaveToFile("result3.xlsx", ExcelVersion.Version2010);
VB.NET
'实例化Workbook类的对象
Dim workbook As New Workbook()
'加载测试文档
workbook.LoadFromFile("sample.xlsx")
'获取第2个工作表中的第一个图表
Dim ws As Worksheet = workbook.Worksheets(1)
Dim chart As Chart = ws.Charts(0)
'加载图片,填充整个图表区域
chart.ChartArea.Fill.CustomPicture(Image.FromFile("tp.png"), "None")
'设置绘图区域的图片填充的透明度
chart.PlotArea.Fill.Transparency = 0.8
'加载图片,仅填充图表中的绘图区域
'chart.PlotArea.Fill.CustomPicture(Image.FromFile("tp.png"), "None");
'保存文档
workbook.SaveToFile("result3.xlsx", ExcelVersion.Version2010)
C# 在 Excel 中创建散点图表
'实例化Workbook类的对象,并获取第一个工作表
Dim workbook As New Workbook()
Dim sheet As Worksheet = workbook.Worksheets(0)
'设置列宽,工作表名
sheet.Range("A1:B1").ColumnWidth = 15F
sheet.Name = "散点图"
'创建散点图
Dim chart As Chart = sheet.Charts.Add(ExcelChartType.ScatterMarkers)
chart.DataRange = sheet.Range("B2:B10")
chart.SeriesDataFromRange = False
'指定散点图在sheet中的位置
chart.LeftColumn = 4
chart.TopRow = 1
chart.RightColumn = 15
chart.BottomRow = 25
'添加图表标题
chart.ChartTitle = "城市化与耕地面积变化散点图"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12
chart.PlotArea.ForeGroundColor = Color.Beige
'添加图表数据源
sheet.Range("A1").Value = "城市化水平"
sheet.Range("A2").Value = "10"
sheet.Range("A3").Value = "15"
sheet.Range("A4").Value = "17"
sheet.Range("A5").Value = "25"
sheet.Range("A6").Value = "35"
sheet.Range("A7").Value = "40"
sheet.Range("A8").Value = "38"
sheet.Range("A10").Value = "17"
sheet.Range("A9").Value = "24"
sheet.Range("B1").Value = "耕地面积"
sheet.Range("B2").Value = "26780"
sheet.Range("B3").Value = "24086"
sheet.Range("B4").Value = "20546"
sheet.Range("B5").Value = "15057"
sheet.Range("B6").Value = "11036"
sheet.Range("B7").Value = "12546"
sheet.Range("B8").Value = "9854"
sheet.Range("B9").Value = "13506"
sheet.Range("B10").Value = "18756"
chart.Series(0).CategoryLabels = sheet.Range("B2:B10")
chart.Series(0).Values = sheet.Range("A2:A10")
sheet.Range("A1:B10").Style.HorizontalAlignment = HorizontalAlignType.Center
sheet.Range("A1:B1").Style.Font.IsBold = True
'添加趋势线
Dim trendLine As IChartTrendLine = chart.Series(0).TrendLines.Add(TrendLineType.Exponential)
trendLine.Name = "趋势线"
'添加坐标轴名称
chart.PrimaryValueAxis.Title = "城市化水平(%)"
chart.PrimaryCategoryAxis.Title = "耕地面积(万hm^2)"
'保存文档
workbook.SaveToFile("result.xlsx", FileFormat.Version2013)
填充整个图表:
填充绘图区域: