C# 添加、修改以及删除Excel迷你图表的方法
程序员文章站
2022-03-28 10:32:39
...
Excel表格中的迷你图表能够直观地向我们展示出数据的变化趋势。本文将介绍C#如何实现为表格数据生成迷你图表,以及修改和删除迷你图表的方法。下面将详细讲述。
所用组件工具:Spire.XLS for .NET
原Excel图表:
一、添加迷你图表(折线图、柱形图、盈亏图)
1.添加命名空间
using System; using Spire.Xls; using System.Drawing;
2.主要代码
//创建一个Workbook类对象并加载Excel文档 Workbook workbook = new Workbook(); workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx"); //获取第一个工作表,在特定单元格添加文本并设置格式 Worksheet sheet = workbook.Worksheets[0]; sheet.Range["H2"].Text = "外贸进/出口额走势"; sheet.Range["H2"].Style.Font.FontName = "Arial Narrow"; sheet.Range["H2"].Style.Font.Color = Color.Black; sheet.Range["A1:H5"].Columns[7].ColumnWidth = 15F; sheet.Range["H2"].Style.Font.IsBold = true; sheet.Range["H2:H5"].BorderInside(LineStyleType.Thin); sheet.Range["H2:H5"].BorderAround(LineStyleType.Thin); //添加折线迷你图 SparklineGroup sparklineGroup1 = sheet.SparklineGroups.AddGroup(); sparklineGroup1.SparklineType = SparklineType.Line; //设置折折线迷你图格式 sparklineGroup1.SparklineColor = Color.Tomato; sparklineGroup1.HighPointColor = Color.Red; //设置添加折现迷你图表的单元格以及图表生成的数据范围 SparklineCollection sparklines1 = sparklineGroup1.Add(); sparklines1.Add(sheet["B3:G3"], sheet["H3"]); //添加柱形迷你图并设置图表颜色 SparklineGroup sparklineGroup2 = sheet.SparklineGroups.AddGroup(); sparklineGroup2.SparklineType = SparklineType.Column; sparklineGroup2.SparklineColor = Color.PaleGreen; sparklineGroup2.HighPointColor = Color.SeaGreen; //设置添加柱形迷你图表的单元格以及图表生成的数据范围 SparklineCollection sparklines2 = sparklineGroup2.Add(); sparklines2.Add(sheet["B4:G4"], sheet["H4"]); //添加盈亏迷你图并设置颜色 SparklineGroup sparklineGroup3 = sheet.SparklineGroups.AddGroup(); sparklineGroup3.SparklineType = SparklineType.Stacked; sparklineGroup3.SparklineColor = Color.SkyBlue; sparklineGroup3.HighPointColor = Color.Blue; //设置盈亏迷你图表的单元格以及图表生成的数据范围 SparklineCollection sparklines3 = sparklineGroup3.Add(); sparklines3.Add(sheet["B5:G5"], sheet["H5"]); //保存文档 workbook.SaveToFile("添加迷你图.xlsx", ExcelVersion.Version2010);
调试运行项目程序,生成文件
二、修改迷你图(图表类型/数据范围)
1.添加命名空间
using System; using Spire.Xls; using Spire.Xls.Core.Spreadsheet;
2.主要代码
//实例化一个Workbook类,加载Excel文档 Workbook workbook = new Workbook(); 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["C3:G3"], sheet.Range["H3"]); //保存文档 workbook.SaveToFile("修改迷你图.xlsx", ExcelVersion.Version2010);
三、删除迷你图表
1.添加命名空间
using System; using Spire.Xls; using Spire.Xls.Core.Spreadsheet;
2.主要代码
//实例化一个Workbook类,加载Excel文档 Workbook workbook = new Workbook(); workbook.LoadFromFile("添加迷你图.xlsx"); //获取第一个工作表 Worksheet sheet = workbook.Worksheets[0]; //获取第2个迷你图 ISparklineGroup sparklineGroup = sheet.SparklineGroups[1]; //从工作表中删除图表 sheet.SparklineGroups.Remove(sparklineGroup); //保存文件 workbook.SaveToFile("删除迷你图.xlsx", ExcelVersion.Version2010);
本文转载自博客:http://www.cnblogs.com/Yesi/p/8038310.html