服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#使用Gembox.SpreadSheet向Excel写入数据及图表的实例

C#使用Gembox.SpreadSheet向Excel写入数据及图表的实例

2022-02-15 16:29cnc C#

下面小编就为大家分享一篇C#使用Gembox.SpreadSheet向Excel写入数据及图表的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

开发工具:vs2017

语言:c#

dotnet版本:.net framework 4.0及以上

使用的dll工具名称:gembox.spreadsheet.dll (版本:37.3.30.1185)

一、gembox.spreadsheet工具:

该dll是由gembox公司开发的基于excel功能的开发工具,该dll很轻量,且使用起来很方便,在这里推荐下来来使用。

下载地址:

gembox_spreadsheet.zip

本文就是使用该工具进行excel的写入操作。

二、创建excel

为了能使用该dll,必须在调用前写入以下代码:

?
1
spreadsheetinfo.setlicense("free-limited-key");

 

创建excel文件如下:

?
1
excelfile excel = new excelfile();

 

这里仅仅只是创建一个excel,代表的是excel整个文件,而保存该文件的代码如下:

?
1
excel.save("文件路径");

 

三、给excel添加一些属性

我们可以给excel添加一些诸如文档标题、作者、公司及备注等内容,实现这些内容的代码如下:

?
1
2
3
4
excel.documentproperties.builtin.add(new keyvaluepair<builtindocumentproperties, string>(builtindocumentproperties.title, title));
excel.documentproperties.builtin.add(new keyvaluepair<builtindocumentproperties, string>(builtindocumentproperties.author, "cnxy"));
excel.documentproperties.builtin.add(new keyvaluepair<builtindocumentproperties, string>(builtindocumentproperties.company, "cnxy"));
excel.documentproperties.builtin.add(new keyvaluepair<builtindocumentproperties, string>(builtindocumentproperties.comments, "by cnxy.website: http://www.cnc6.cn"));

四、给excel默认字体

这是给整个excel设置统一的字体,具体代码如下:

?
1
excel.defaultfontname = "times new roman";

 

五、添加一个sheet表格

要知道,excel是由sheet表格构成的,因此添加sheet表格的代码如下:

?
1
excelworksheet sheet = excel.worksheets.add("表格名称");

 

以上,已经在excel上添加了一个名为“表格名称”的数据表格。

六、给sheet添加密码保护

有时候,为了保护自己的excel不被篡改,需要设置一下sheet的密码,具体代码如下:

?
1
2
sheet.protectionsettings.setpassword("cnxy");
sheet.protected = true;

 

七、让网格线不可见

默认情况下,sheet的网格线是可见的,有时候,我们可以设置网格线不可见,具体代码如下:

?
1
sheet.viewoptions.showgridlines = false;

八、写入单元格

访问单元格的方式有三种,三种分别如下:

?
1
2
3
sheet.cells["a1"]
sheet.cells[0,0]
sheet.rows[0].cells[0]

以上三种方法都可以访问单元格,但如下写入单元格呢,其实方法很简单,如下:

?
1
sheet.cells["a1"].value= 内容

 

以上没有加双引号的原因是:内容不一定是字符串,有可能是数字、日期等。

九、单元格样式设置

单元格设置需要使用cellstyle对象,其代码如下:

?
1
2
3
4
5
6
7
8
9
10
cellstyle style = new cellstyle();
//设置水平对齐模式
style.horizontalalignment = horizontalalignmentstyle.center;
//设置垂直对齐模式
style.verticalalignment = verticalalignmentstyle.center;
//设置字体
style.font.size = 22 * pt; //pt=20
style.font.weight = excelfont.boldweight;
style.font.color = color.blue;
sheet.cells["a1"].style = style;

填充方式如下:

?
1
2
sheet.cells[24,1].style.fillpattern.patternstyle = fillpatternstyle.solid;
sheet.rows[24].cells[1].style.fillpattern.patternforegroundcolor = color.gainsboro;

 

设置边框如下:

?
1
style.borders.setborders(multipleborders.outside, color.black, linestyle.thin);

 

十、合并单元格

合并单元格需使用cellrange对象,我们可以从sheet.cells.getsubrange或getsubrangeabsolute获得,代码如下:

?
1
2
3
4
cellrange range = sheet.cells.getsubrange("b2", "j3");
range.value = "chart";
range.merged = true;
sheet.cells.getsubrangeabsolute(24, 1, 24, 9).merged = true;

十一、创建chart图表对象

使用的是linechart对象,代码如下:

?
1
linechart chart =(linechart)sheet.charts.add(charttype.line,"b4","j22");

 

以上意思是从b4到j22创建一个linechart对象。

设置图表标题不可见,代码如下:

?
1
chart.title.isvisible = false;

 

设置x轴与y轴的标题可见,代码如下:

?
1
2
chart.axes.horizontal.title.text = "time";
chart.axes.vertical.title.text = "voltage";

 

十二、给y轴设置属性

主要使用了chart.axes.verticalvalue返回的valueaxis对象,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
valueaxis axisy = chart.axes.verticalvalue;
//y轴最大刻度与最小刻度
axisy.minimum = -100;
axisy.maximum = 100;
//y轴主要与次要单位大小
axisy.majorunit = 20;
axisy.minorunit = 10;
//y轴主要与次要网格是否可见
axisy.majorgridlines.isvisible = true;
axisy.minorgridlines.isvisible = true;
//y轴刻度线类型
axisy.majortickmarktype = tickmarktype.cross;
axisy.minortickmarktype = tickmarktype.inside;

十三、附上完整的源代码

  1. using gembox.spreadsheet; 
  2. using gembox.spreadsheet.charts; 
  3. using system; 
  4. using system.collections.generic; 
  5. using system.diagnostics; 
  6. using system.drawing; 
  7. namespace spreadsheetchartdemo 
  8.  class program 
  9.  { 
  10.  const int pt = 20; 
  11.  const int length = 200; 
  12.  const string timesnewroman = "times new roman"
  13.  const string title = "spread sheet chart demo"
  14.  static void main(string[] args) 
  15.  { 
  16.  spreadsheetinfo.setlicense("free-limited-key"); 
  17.  excelfile excel = new excelfile(); 
  18.  //excel默认字体 
  19.  excel.defaultfontname = timesnewroman; 
  20.  //excel文档属性设置 
  21.  excel.documentproperties.builtin.add(new keyvaluepair<builtindocumentproperties, string>(builtindocumentproperties.title, title)); 
  22.  excel.documentproperties.builtin.add(new keyvaluepair<builtindocumentproperties, string>(builtindocumentproperties.author, "cnxy")); 
  23.  excel.documentproperties.builtin.add(new keyvaluepair<builtindocumentproperties, string>(builtindocumentproperties.company, "cnxy")); 
  24.  excel.documentproperties.builtin.add(new keyvaluepair<builtindocumentproperties, string>(builtindocumentproperties.comments, "by cnxy.website: http://www.cnc6.cn")); 
  25.  //新建一个sheet表格 
  26.  excelworksheet sheet = excel.worksheets.add(title); 
  27.  //设置表格保护 
  28.  sheet.protectionsettings.setpassword("cnxy"); 
  29.  sheet.protected = true
  30.  //设置网格线不可见 
  31.  sheet.viewoptions.showgridlines = false
  32.  //定义一个b2-g3的单元格范围 
  33.  cellrange range = sheet.cells.getsubrange("b2""j3"); 
  34.  range.value = "chart"
  35.  range.merged = true
  36.  //定义一个单元格样式 
  37.  cellstyle style = new cellstyle(); 
  38.  //设置边框 
  39.  style.borders.setborders(multipleborders.outside, color.black, linestyle.thin); 
  40.  //设置水平对齐模式 
  41.  style.horizontalalignment = horizontalalignmentstyle.center; 
  42.  //设置垂直对齐模式 
  43.  style.verticalalignment = verticalalignmentstyle.center; 
  44.  //设置字体 
  45.  style.font.size = 22 * pt; 
  46.  style.font.weight = excelfont.boldweight; 
  47.  style.font.color = color.blue; 
  48.  range.style = style; 
  49.  //增加chart 
  50.  linechart chart = (linechart)sheet.charts.add(charttype.line,"b4","j22"); 
  51.  chart.title.isvisible = false
  52.  chart.axes.horizontal.title.text = "time"
  53.  chart.axes.vertical.title.text = "voltage"
  54.  valueaxis axisy = chart.axes.verticalvalue; 
  55.  //y轴最大刻度与最小刻度 
  56.  axisy.minimum = -100; 
  57.  axisy.maximum = 100; 
  58.  //y轴主要与次要单位大小 
  59.  axisy.majorunit = 20; 
  60.  axisy.minorunit = 10; 
  61.  //y轴主要与次要网格是否可见 
  62.  axisy.majorgridlines.isvisible = true
  63.  axisy.minorgridlines.isvisible = true
  64.  //y轴刻度线类型 
  65.  axisy.majortickmarktype = tickmarktype.cross; 
  66.  axisy.minortickmarktype = tickmarktype.inside; 
  67.  random random = new random(); 
  68.  double[] data = new double[length]; 
  69.  for (int i=0;i< length; i++) 
  70.  { 
  71.  if( random.next(0,100) > 50) 
  72.  data[i] = random.nextdouble() * 100; 
  73.  else 
  74.  data[i] = -random.nextdouble() * 100; 
  75.  } 
  76.  chart.series.add("random", data); 
  77.  //尾部信息 
  78.  range = sheet.cells.getsubrange("b23""j24"); 
  79.  range.value = $"write time:{datetime.now:yyyy-mm-dd hh:mm:ss} by cnxy"
  80.  range.merged = true
  81.  //b25(三种单元格模式) 
  82.  sheet.cells["b25"].value = "http://www.cnc6.cn"
  83.  sheet.cells[24,1].style.fillpattern.patternstyle = fillpatternstyle.solid; 
  84.  sheet.rows[24].cells[1].style.fillpattern.patternforegroundcolor = color.gainsboro; 
  85.  //b25,j25 
  86.  sheet.cells.getsubrangeabsolute(24, 1, 24, 9).merged = true
  87.  string filepath = $@"{environment.currentdirectory}sheetchart.xlsx"
  88.  try 
  89.  { 
  90.  excel.save(filepath); 
  91.  process.start(filepath); 
  92.  console.writeline("write successfully"); 
  93.  } 
  94.  catch(exception ex) 
  95.  { 
  96.  console.writeline(ex); 
  97.  } 
  98.  console.write("press any key to continue."); 
  99.  console.readkey(); 
  100.  } 
  101.  } 

十四、生成的excel

演示的excel下载地址:

sheetchart.zip

C#使用Gembox.SpreadSheet向Excel写入数据及图表的实例

C#使用Gembox.SpreadSheet向Excel写入数据及图表的实例

十五、生成的exe

下载地址如下:

spreadsheetchartdemo.zip

运行结果如下:

C#使用Gembox.SpreadSheet向Excel写入数据及图表的实例

以上这篇c#使用gembox.spreadsheet向excel写入数据及图表的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/cncc/archive/2017/12/06/7992383.html

延伸 · 阅读

精彩推荐
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08