经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
.Net Excel 导出图表Demo(柱状图,多标签页)
来源:cnblogs  作者:卖身也卖艺  时间:2018/11/25 20:09:19  对本文有异议

1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表)

2 Epplus 的安装和引用

   新建一个工程文件或控制台应用程序 打开 Vs2017 Tools 的Nuget 包管理器,使用命令 install-package Epplus 

  

3 开始在创建的工程中编写代码,实现功能

  

4 具体实现流程代码如下:

 

  
  1. 1 using OfficeOpenXml;
  2. 2 using OfficeOpenXml.Drawing;
  3. 3 using OfficeOpenXml.Drawing.Chart;
  4. 4 using OfficeOpenXml.Style;
  5. 5 using System;
  6. 6 using System.Collections.Generic;
  7. 7 using System.Drawing;
  8. 8 using System.IO;
  9. 9 using System.Linq;
  10. 10 using System.Text;
  11. 11 using System.Threading.Tasks;
  12. 12
  13. 13 namespace EpplusTest
  14. 14 {
  15. 15 public class Program
  16. 16 {
  17. 17 static void Main(string[] args)
  18. 18 {
  19. 19 FileInfo newFile = new FileInfo(@"d:\test.xlsx");
  20. 20 if (newFile.Exists)
  21. 21 {
  22. 22 newFile.Delete();
  23. 23 newFile = new FileInfo(@"d:\test.xlsx");
  24. 24 }
  25. 25
  26. 26 using (ExcelPackage package = new ExcelPackage(newFile))
  27. 27 {
  28. 28 #region 创建多个Sheet页
  29. 29 for (int i = 0; i < 5; i++)
  30. 30 {
  31. 31 package.Workbook.Worksheets.Add("Demo" + i);
  32. 32 }
  33. 33 ExcelWorksheet worksheet = package.Workbook.Worksheets["Demo0"];
  34. 34 ExcelWorksheet worksheet1 = package.Workbook.Worksheets["Demo1"];
  35. 35
  36. 36 #endregion
  37. 37
  38. 38 #region 1 模拟填充数据
  39. 39 worksheet1.Cells[1, 1].Value = "名称";
  40. 40 worksheet1.Cells[1, 2].Value = "价格";
  41. 41 worksheet1.Cells[1, 3].Value = "销量";
  42. 42
  43. 43 worksheet1.Cells[2, 1].Value = "苹果";
  44. 44 worksheet1.Cells[2, 2].Value = 56;
  45. 45 worksheet1.Cells[2, 3].Value = 100;
  46. 46
  47. 47 worksheet1.Cells[3, 1].Value = "华为";
  48. 48 worksheet1.Cells[3, 2].Value = 45;
  49. 49 worksheet1.Cells[3, 3].Value = 150;
  50. 50
  51. 51 worksheet1.Cells[4, 1].Value = "小米";
  52. 52 worksheet1.Cells[4, 2].Value = 38;
  53. 53 worksheet1.Cells[4, 3].Value = 130;
  54. 54
  55. 55 worksheet1.Cells[5, 1].Value = "OPPO";
  56. 56 worksheet1.Cells[5, 2].Value = 22;
  57. 57 worksheet1.Cells[5, 3].Value = 200;
  58. 58 #endregion
  59. 59
  60. 60 #region 2 构造图表
  61. 61 worksheet.Cells.Style.WrapText = true;
  62. 62 worksheet.View.ShowGridLines = false;//去掉sheet的网格线
  63. 63 using (ExcelRange range = worksheet.Cells[1, 1, 5, 3])
  64. 64 {
  65. 65 range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
  66. 66 range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
  67. 67 }
  68. 68
  69. 69 using (ExcelRange range = worksheet.Cells[1, 1, 1, 3])
  70. 70 {
  71. 71 range.Style.Font.Bold = true;
  72. 72 range.Style.Font.Color.SetColor(Color.White);
  73. 73 range.Style.Font.Name = "微软雅黑";
  74. 74 range.Style.Font.Size = 12;
  75. 75 range.Style.Fill.PatternType = ExcelFillStyle.Solid;
  76. 76 range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128));
  77. 77 }
  78. 78
  79. 79 worksheet1.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  80. 80 worksheet1.Cells[1, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  81. 81 worksheet1.Cells[1, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  82. 82
  83. 83 worksheet1.Cells[2, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  84. 84 worksheet1.Cells[2, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  85. 85 worksheet1.Cells[2, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  86. 86
  87. 87 worksheet1.Cells[3, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  88. 88 worksheet1.Cells[3, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  89. 89 worksheet1.Cells[3, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  90. 90
  91. 91 worksheet1.Cells[4, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  92. 92 worksheet1.Cells[4, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  93. 93 worksheet1.Cells[4, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  94. 94
  95. 95 worksheet1.Cells[5, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  96. 96 worksheet1.Cells[5, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  97. 97 worksheet1.Cells[5, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  98. 98
  99. 99 ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);
  100. 100 ExcelChartSerie serie = chart.Series.Add(worksheet1.Cells[2, 3, 5, 3], worksheet1.Cells[2, 1, 5, 1]);//引用worksheet1的数据填充图表的X轴和Y轴
  101. 101 serie.HeaderAddress = worksheet1.Cells[1, 3];
  102. 102 #endregion
  103. 103
  104. 104 #region 3 设置图表的样式
  105. 105 chart.SetPosition(40, 10);
  106. 106 chart.SetSize(500, 300);
  107. 107 chart.Title.Text = "销量走势";
  108. 108 chart.Title.Font.Color = Color.FromArgb(89, 89, 89);
  109. 109 chart.Title.Font.Size = 15;
  110. 110 chart.Title.Font.Bold = true;
  111. 111 chart.Style = eChartStyle.Style15;
  112. 112 chart.Legend.Border.LineStyle = eLineStyle.SystemDash;
  113. 113 chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217);
  114. 114 #endregion
  115. 115 package.Save();
  116. 116 }
  117. 117 }
  118. 118 }
  119. 119 }
View Code

 

5 效果展示:

 


6 Demo源码GitHub下载地址:https://github.com/HJ520134/.NetDemo.git

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号