下面小编就为大家分享一篇C# 添加、修改以及删除Excel迷你图表的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
下面小编就为大家分享一篇C# 添加、修改以及删除Excel迷你图表的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
下面小编就为大家分享一篇C# 添加、修改以及删除Excel迷你图表的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
excel表格中的迷你图表能够直观地向我们展示出数据的变化趋势。本文将介绍c#如何实现为表格数据生成迷你图表,以及修改和删除迷你图表的方法。下面将详细讲述。
所用组件工具:spire.xls for .net
原excel图表:

一、添加迷你图表(折线图、柱形图、盈亏图)
1.添加命名空间
|
1
2
3
|
using system;using spire.xls;using system.drawing; |
2.主要代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
//创建一个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.添加命名空间
|
1
2
3
|
using system;using spire.xls;using spire.xls.core.spreadsheet; |
2.主要代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//实例化一个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.添加命名空间
|
1
2
3
|
using system;using spire.xls;using spire.xls.core.spreadsheet; |
2.主要代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//实例化一个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); |
以上全部内容为本次关于excel迷你图表生成、修改和删除的全部操作,希望对你有所帮助。
这篇c# 添加、修改以及删除excel迷你图表的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持米米素材网。
原文链接:http://www.cnblogs.com/Yesi/archive/2017/12/14/8038310.html
发表评论