.Net Excel 导出图表Demo(柱状图,多标签页)
程序员文章站
2022-12-11 08:06:36
1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表) 2 Epplus 的安装和引用 新建一个工程文件或控制台应用程序 打开 Vs2017 Tools 的Nuget 包管理器,使用命令 install-package Epplus 3 开始在创建的工程 ......
1 使用插件名称epplus,多个sheet页数据应用,demo为柱状图(epplus支持多种图表)
2 epplus 的安装和引用
新建一个工程文件或控制台应用程序 打开 vs2017 tools 的nuget 包管理器,使用命令 install-package epplus
3 开始在创建的工程中编写代码,实现功能
4 具体实现流程代码如下:
1 using officeopenxml; 2 using officeopenxml.drawing; 3 using officeopenxml.drawing.chart; 4 using officeopenxml.style; 5 using system; 6 using system.collections.generic; 7 using system.drawing; 8 using system.io; 9 using system.linq; 10 using system.text; 11 using system.threading.tasks; 12 13 namespace epplustest 14 { 15 public class program 16 { 17 static void main(string[] args) 18 { 19 fileinfo newfile = new fileinfo(@"d:\test.xlsx"); 20 if (newfile.exists) 21 { 22 newfile.delete(); 23 newfile = new fileinfo(@"d:\test.xlsx"); 24 } 25 26 using (excelpackage package = new excelpackage(newfile)) 27 { 28 #region 创建多个sheet页 29 for (int i = 0; i < 5; i++) 30 { 31 package.workbook.worksheets.add("demo" + i); 32 } 33 excelworksheet worksheet = package.workbook.worksheets["demo0"]; 34 excelworksheet worksheet1 = package.workbook.worksheets["demo1"]; 35 36 #endregion 37 38 #region 1 模拟填充数据 39 worksheet1.cells[1, 1].value = "名称"; 40 worksheet1.cells[1, 2].value = "价格"; 41 worksheet1.cells[1, 3].value = "销量"; 42 43 worksheet1.cells[2, 1].value = "苹果"; 44 worksheet1.cells[2, 2].value = 56; 45 worksheet1.cells[2, 3].value = 100; 46 47 worksheet1.cells[3, 1].value = "华为"; 48 worksheet1.cells[3, 2].value = 45; 49 worksheet1.cells[3, 3].value = 150; 50 51 worksheet1.cells[4, 1].value = "小米"; 52 worksheet1.cells[4, 2].value = 38; 53 worksheet1.cells[4, 3].value = 130; 54 55 worksheet1.cells[5, 1].value = "oppo"; 56 worksheet1.cells[5, 2].value = 22; 57 worksheet1.cells[5, 3].value = 200; 58 #endregion 59 60 #region 2 构造图表 61 worksheet.cells.style.wraptext = true; 62 worksheet.view.showgridlines = false;//去掉sheet的网格线 63 using (excelrange range = worksheet.cells[1, 1, 5, 3]) 64 { 65 range.style.horizontalalignment = excelhorizontalalignment.center; 66 range.style.verticalalignment = excelverticalalignment.center; 67 } 68 69 using (excelrange range = worksheet.cells[1, 1, 1, 3]) 70 { 71 range.style.font.bold = true; 72 range.style.font.color.setcolor(color.white); 73 range.style.font.name = "微软雅黑"; 74 range.style.font.size = 12; 75 range.style.fill.patterntype = excelfillstyle.solid; 76 range.style.fill.backgroundcolor.setcolor(color.fromargb(128, 128, 128)); 77 } 78 79 worksheet1.cells[1, 1].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 80 worksheet1.cells[1, 2].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 81 worksheet1.cells[1, 3].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 82 83 worksheet1.cells[2, 1].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 84 worksheet1.cells[2, 2].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 85 worksheet1.cells[2, 3].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 86 87 worksheet1.cells[3, 1].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 88 worksheet1.cells[3, 2].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 89 worksheet1.cells[3, 3].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 90 91 worksheet1.cells[4, 1].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 92 worksheet1.cells[4, 2].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 93 worksheet1.cells[4, 3].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 94 95 worksheet1.cells[5, 1].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 96 worksheet1.cells[5, 2].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 97 worksheet1.cells[5, 3].style.border.borderaround(excelborderstyle.thin, color.fromargb(191, 191, 191)); 98 99 excelchart chart = worksheet.drawings.addchart("chart", echarttype.columnclustered); 100 excelchartserie serie = chart.series.add(worksheet1.cells[2, 3, 5, 3], worksheet1.cells[2, 1, 5, 1]);//引用worksheet1的数据填充图表的x轴和y轴 101 serie.headeraddress = worksheet1.cells[1, 3]; 102 #endregion 103 104 #region 3 设置图表的样式 105 chart.setposition(40, 10); 106 chart.setsize(500, 300); 107 chart.title.text = "销量走势"; 108 chart.title.font.color = color.fromargb(89, 89, 89); 109 chart.title.font.size = 15; 110 chart.title.font.bold = true; 111 chart.style = echartstyle.style15; 112 chart.legend.border.linestyle = elinestyle.systemdash; 113 chart.legend.border.fill.color = color.fromargb(217, 217, 217); 114 #endregion 115 package.save(); 116 } 117 } 118 } 119 }
5 效果展示:
6 demo源码github下载地址:https://github.com/hj520134/.netdemo.git
上一篇: 经纬度偏移转换
下一篇: GFF高仿QQ客户端及服务器