博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#用EPPLUS操作excel
阅读量:6657 次
发布时间:2019-06-25

本文共 6742 字,大约阅读时间需要 22 分钟。

 参考:

结果:

 

 

 

首先新建一个winform程序,然后加一个button,

之后双击button1,进入代码区域,输入下面的代码:

 

 

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.ComponentModel;  8 using System.Data;  9 using System.Drawing; 10 using System.IO; 11 using System.Linq; 12 using System.Text; 13 using System.Threading.Tasks; 14 using System.Windows.Forms; 15  16 namespace WindowsFormsApplication1epplus 17 { 18     public partial class Form1 : Form 19     { 20         public Form1() 21         { 22             InitializeComponent(); 23         } 24  25         private void button1_Click(object sender, EventArgs e) 26         { 27             using (ExcelPackage package = new ExcelPackage(new FileStream(@"E:\test.xlsx", FileMode.Open))) 28             { 29                 for (int i = 1; i <= package.Workbook.Worksheets.Count; ++i)//循环sheet 30                 { 31                     ExcelWorksheet sheet = package.Workbook.Worksheets[i]; 32                     for (int j = sheet.Dimension.Start.Column, k = sheet.Dimension.End.Column; j <= k; j++) 33                     { 34                         for (int m = sheet.Dimension.Start.Row, n = sheet.Dimension.End.Row; m <= n; m++) 35                         { 36                             string str = GetValue(sheet, m, j); 37                             if (str != null) 38                             { 39                                 // do something 40                             } 41                         } 42                     } 43                 } 44             } 45  46  47  48  49             FileInfo newFile = new FileInfo(@"E:\test.xlsx"); 50             if (newFile.Exists) 51             { 52                 newFile.Delete(); 53                 newFile = new FileInfo(@"E:\test.xlsx"); 54             } 55             using (ExcelPackage package = new ExcelPackage(newFile)) 56             { 57                 ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test"); 58  59                 worksheet.Cells.Style.WrapText = true; 60                 worksheet.View.ShowGridLines = false;//去掉sheet的网格线 61  62                 worksheet.Cells[1, 1].Value = "名称"; 63                 worksheet.Cells[1, 2].Value = "价格"; 64                 worksheet.Cells[1, 3].Value = "销量"; 65  66                 worksheet.Cells[2, 1].Value = "大米"; 67                 worksheet.Cells[2, 2].Value = 56; 68                 worksheet.Cells[2, 3].Value = 100; 69  70                 worksheet.Cells[3, 1].Value = "玉米"; 71                 worksheet.Cells[3, 2].Value = 45; 72                 worksheet.Cells[3, 3].Value = 150; 73  74                 worksheet.Cells[4, 1].Value = "小米"; 75                 worksheet.Cells[4, 2].Value = 38; 76                 worksheet.Cells[4, 3].Value = 130; 77  78                 worksheet.Cells[5, 1].Value = "糯米"; 79                 worksheet.Cells[5, 2].Value = 22; 80                 worksheet.Cells[5, 3].Value = 200; 81  82                 using (ExcelRange range = worksheet.Cells[1, 1, 5, 3]) 83                 { 84                     range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; 85                     range.Style.VerticalAlignment = ExcelVerticalAlignment.Center; 86                 } 87  88                 using (ExcelRange range = worksheet.Cells[1, 1, 1, 3]) 89                 { 90                     range.Style.Font.Bold = true; 91                     range.Style.Font.Color.SetColor(Color.White); 92                     range.Style.Font.Name = "微软雅黑"; 93                     range.Style.Font.Size = 12; 94                     range.Style.Fill.PatternType = ExcelFillStyle.Solid; 95                     range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128)); 96                 } 97  98                 worksheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); 99                 worksheet.Cells[1, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));100                 worksheet.Cells[1, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));101 102                 worksheet.Cells[2, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));103                 worksheet.Cells[2, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));104                 worksheet.Cells[2, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));105 106                 worksheet.Cells[3, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));107                 worksheet.Cells[3, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));108                 worksheet.Cells[3, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));109 110                 worksheet.Cells[4, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));111                 worksheet.Cells[4, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));112                 worksheet.Cells[4, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));113 114                 worksheet.Cells[5, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));115                 worksheet.Cells[5, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));116                 worksheet.Cells[5, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));117 118                 ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);119 120                 ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1]);121                 serie.HeaderAddress = worksheet.Cells[1, 3];122 123                 chart.SetPosition(150, 10);124                 chart.SetSize(500, 300);125                 chart.Title.Text = "销量走势";126                 chart.Title.Font.Color = Color.FromArgb(89, 89, 89);127                 chart.Title.Font.Size = 15;128                 chart.Title.Font.Bold = true;129                 chart.Style = eChartStyle.Style15;130                 chart.Legend.Border.LineStyle = eLineStyle.Solid;131                 chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217);132 133                 package.Save();134             }135 136 137 138 139 140         }141     }142 }
View Code

 会提示错误, 注意要引用EPPlus,安装好效果如下:

方法是:

 输入epplus,搜索,安装即可。

注意路径在E盘,没有E盘的肯定会出错。

 

转载于:https://www.cnblogs.com/zhubinglong/p/7857736.html

你可能感兴趣的文章
加号+和减号-
查看>>
详解Mysql分布式事务XA(跨数据库事务)
查看>>
2018年,成功的创业公司网站是怎么设计的?
查看>>
MySQL 创始人:写代码比打游戏还爽,程序员应该多泡开源社区
查看>>
构造器内部的多态方法的行为
查看>>
Android Studio库Module引用aar文件
查看>>
008-tar,gzip,bzip2的使用
查看>>
静态单元格
查看>>
浅谈 MySQL 集群高可用架构
查看>>
两个路径与四个centos7命令
查看>>
SQLCMD命令的几种用法
查看>>
CSS:父子元素浮动分析和清除浮动
查看>>
关于 SSHKey
查看>>
【GO 笔记】 20180907 golang GUI
查看>>
Rabbitmq-springboot集成
查看>>
mysql实用命令
查看>>
Maven部署Struts2环境详解
查看>>
jenkins------结合maven将svn项目自动部署到tomcat下
查看>>
JAVA中的内存映射文件
查看>>
2016年上半年系统集成中项3月28日作业
查看>>