迷你图是excel 2010及以上版本所支持的功能,它是绘制在单元格中的一种微型图表,用迷你图可以清晰直观地反映数据的变化趋势。本文将介绍如何使用spire.xls组件在excel中添加迷你图、修改现有迷你图以及删除迷你图。
添加迷你图
spire.xls支持excel目前所提供的三种迷你图类型,即“折线迷你图”、“柱形迷你图”和“盈亏迷你图”,这里我们将介绍如何使用spire.xls添加这三种迷你图到excel文档。
c#
//创建workbook实例
workbook workbook = new workbook();
//载入excel文档
workbook.loadfromfile("sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//添加折线迷你图组
sparklinegroup sparklinegroup1 = sheet.sparklinegroups.addgroup();
sparklinegroup1.sparklinetype = sparklinetype.line;
//设置迷你图组的颜色
sparklinegroup1.sparklinecolor = color.orange;
//设置迷你图组最高点的颜色
sparklinegroup1.highpointcolor = color.red;
//添加迷你图到图组
sparklinecollection sparklines1 = sparklinegroup1.add();
sparklines1.add(sheet["b3:m3"], sheet["n3"]);
//添加柱形迷你图组
sparklinegroup sparklinegroup2 = sheet.sparklinegroups.addgroup();
sparklinegroup2.sparklinetype = sparklinetype.column;
//设置迷你图组的颜色
sparklinegroup2.sparklinecolor = color.blueviolet;
//设置迷你图组最高点的颜色
sparklinegroup2.highpointcolor = color.red;
//添加迷你图到图组
sparklinecollection sparklines2 = sparklinegroup2.add();
sparklines2.add(sheet["b4:m4"], sheet["n4"]);
//添加盈亏迷你图组
sparklinegroup sparklinegroup3 = sheet.sparklinegroups.addgroup();
sparklinegroup3.sparklinetype = sparklinetype.stacked;
//设置迷你图组的颜色
sparklinegroup3.sparklinecolor = color.darkblue;
//设置迷你图组最高点的颜色
sparklinegroup3.highpointcolor = color.red;
//添加迷你图到图组
sparklinecollection sparklines3 = sparklinegroup3.add();
sparklines3.add(sheet["b5:m5"], sheet["n5"]);
//保存文档
workbook.savetofile("添加迷你图.xlsx", excelversion.version2010);
vb.net
'创建workbook实例
dim workbook as new workbook()
'载入excel文档
workbook.loadfromfile("sample.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'添加折线迷你图组
dim sparklinegroup1 as sparklinegroup = sheet.sparklinegroups.addgroup()
sparklinegroup1.sparklinetype = sparklinetype.line
'设置迷你图组的颜色
sparklinegroup1.sparklinecolor = color.orange
'设置迷你图组最高点的颜色
sparklinegroup1.highpointcolor = color.red
'添加迷你图到图组
dim sparklines1 as sparklinecollection = sparklinegroup1.add()
sparklines1.add(sheet("b3:m3"), sheet("n3"))
'添加柱形迷你图组
dim sparklinegroup2 as sparklinegroup = sheet.sparklinegroups.addgroup()
sparklinegroup2.sparklinetype = sparklinetype.column
'设置迷你图组的颜色
sparklinegroup2.sparklinecolor = color.blueviolet
'设置迷你图组最高点的颜色
sparklinegroup2.highpointcolor = color.red
'添加迷你图到图组
dim sparklines2 as sparklinecollection = sparklinegroup2.add()
sparklines2.add(sheet("b4:m4"), sheet("n4"))
'添加盈亏迷你图组
dim sparklinegroup3 as sparklinegroup = sheet.sparklinegroups.addgroup()
sparklinegroup3.sparklinetype = sparklinetype.stacked
'设置迷你图组的颜色
sparklinegroup3.sparklinecolor = color.darkblue
'设置迷你图组最高点的颜色
sparklinegroup3.highpointcolor = color.red
'添加迷你图到图组
dim sparklines3 as sparklinecollection = sparklinegroup3.add()
sparklines3.add(sheet("b5:m5"), sheet("n5"))
'保存文档
workbook.savetofile("添加迷你图.xlsx", excelversion.version2010)
修改现有迷你图
c#
//创建workbook实例
workbook workbook = new workbook();
//载入excel文档
workbook.loadfromfile("添加迷你图.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//修改第一个迷你图组中迷你图的类型和数据范围
isparklinegroup sparklinegroup = sheet.sparklinegroups[0];
sparklinegroup.sparklinetype = sparklinetype.column;
isparklines sparklines = sparklinegroup[0];
sparklines.refreshranges(sheet.range["b3:g3"], sheet.range["n3"]);
//保存文档
workbook.savetofile("修改迷你图.xlsx", excelversion.version2010);
vb.net
'创建workbook实例
dim workbook as new workbook()
'载入excel文档
workbook.loadfromfile("添加迷你图.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'修改第一个迷你图组中迷你图的类型和数据范围
dim sparklinegroup as isparklinegroup = sheet.sparklinegroups(0)
sparklinegroup.sparklinetype = sparklinetype.column
dim sparklines as isparklines = sparklinegroup(0)
sparklines.refreshranges(sheet.range("b3:g3"), sheet.range("n3"))
'保存文档
workbook.savetofile("修改迷你图.xlsx", excelversion.version2010)
删除迷你图
spire.xls支持从迷你图组中删除指定的迷你图,也支持将整个迷你图组从工作表中删除。
c#
//创建workbook实例
workbook workbook = new workbook();
//载入excel文档
workbook.loadfromfile("添加迷你图.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//获取第一个迷你图组
isparklinegroup sparklinegroup = sheet.sparklinegroups[0];
////删除图组中第一个迷你图
//isparklines sparklines = sparklinegroup[0];
//sparklines.remove(sparklines[0]);
//从工作表中删除第一个迷你图组
sheet.sparklinegroups.remove(sparklinegroup);
//保存文档
workbook.savetofile("删除迷你图.xlsx", excelversion.version2010);
vb.net
'创建workbook实例
dim workbook as new workbook()
'载入excel文档
workbook.loadfromfile("添加迷你图.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'获取第一个迷你图组
dim sparklinegroup as isparklinegroup = sheet.sparklinegroups(0)
'删除图组中第一个迷你图
'isparklines sparklines = sparklinegroup[0];
'sparklines.remove(sparklines[0]);
'从工作表中删除第一个迷你图组
sheet.sparklinegroups.remove(sparklinegroup)
'保存文档
workbook.savetofile("删除迷你图.xlsx", excelversion.version2010)