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)
填充整个图表:
填充绘图区域: