本篇文章将介绍如何通过控件spire.xls for .net更改excel图表中的字体及格式。包括更改图表标题、图例、坐标轴、坐标轴标题、图表数据标签等字体。可应用格式包括字体、字号、颜色、粗细、倾斜等。
测试文档
c#
//实例化workbook类的对象,并加载测试文档
workbook workbook = new workbook();
workbook.loadfromfile("test.xlsx");
//获取工作表中的第一个图表
worksheet ws = workbook.worksheets[0];
spire.xls.chart chart = ws.charts[0];
//更改图例字体、字号、字体颜色、字体粗细等
excelfont font = workbook.createfont();
font.size = 10.0;
font.color = color.brown;
font.isbold = true;
font.fontname = "幼圆";
//应用字体设置到图例
chart.legend.textarea.setfont(font);
//应用字体设置到图表数据标签
foreach (chartserie cs in chart.series)
{
cs.datapoints.defaultdatapoint.datalabels.textarea.setfont(font);
}
//更改坐标轴标题名称及格式
//横轴
chart.primarycategoryaxis.title = "月 份";
chart.primarycategoryaxis.titlearea.font.fontname = "幼圆";
chart.primarycategoryaxis.titlearea.font.isbold = true;
//纵轴
chart.primaryvalueaxis.title = "销 售 额";
chart.primaryvalueaxis.titlearea.font.fontname = "幼圆";
chart.primaryvalueaxis.titlearea.font.isbold = true;
//更改坐标轴字体格式
//横轴
chart.primarycategoryaxis.font.color = color.blueviolet;
chart.primarycategoryaxis.font.isbold = true;
chart.primarycategoryaxis.font.fontname = "幼圆";
//纵轴
chart.primaryvalueaxis.font.color = color.blueviolet;
chart.primaryvalueaxis.font.isbold = true;
chart.primaryvalueaxis.font.fontname = "幼圆";
//更改图表标题名称
chart.charttitle = "1季度销售额";
//保存文档
workbook.savetofile("output.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 font as excelfont = workbook.createfont()
font.size = 10.0
font.color = color.brown
font.isbold = true
font.fontname = "幼圆"
'应用字体设置到图例
chart.legend.textarea.setfont(font)
'应用字体设置到图表数据标签
for each cs as chartserie in chart.series
cs.datapoints.defaultdatapoint.datalabels.textarea.setfont(font)
next
'更改坐标轴标题名称及格式
'横轴
chart.primarycategoryaxis.title = "月 份"
chart.primarycategoryaxis.titlearea.font.fontname = "幼圆"
chart.primarycategoryaxis.titlearea.font.isbold = true
'纵轴
chart.primaryvalueaxis.title = "销 售 额"
chart.primaryvalueaxis.titlearea.font.fontname = "幼圆"
chart.primaryvalueaxis.titlearea.font.isbold = true
'更改坐标轴字体格式
'横轴
chart.primarycategoryaxis.font.color = color.blueviolet
chart.primarycategoryaxis.font.isbold = true
chart.primarycategoryaxis.font.fontname = "幼圆"
'纵轴
chart.primaryvalueaxis.font.color = color.blueviolet
chart.primaryvalueaxis.font.isbold = true
chart.primaryvalueaxis.font.fontname = "幼圆"
'更改图表标题名称
chart.charttitle = "1季度销售额"
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2010)
结果文档