迷你图是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)