c#用EPPLUS操作excel

 参考:

http://www.cnblogs.com/rumeng/p/3785748.html

http://www.cnblogs.com/libla/p/5824296.html#3818995

结果:

 

 

 

首先新建一个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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Epplus 简介:Epplus是一个使用Open Office XML(Xlsx)文件格式,能读写Excel 2007/2010文件的开源组件 功效:支持对excel文档的汇入汇出,图表(excel自带的图表基本都可以实现)的列印 使用:首先应该下载Epplus的dll文件 1> 添加dll文件至工程bin文件中 2>在程式中添加引用 using OfficeOpenXml; using OfficeOpenXml.Drawing; using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.Style; 3>所有的操作语句需要放置在下面的using中 using (ExcelPackage package = new ExcelPackage()) { } 4.添加新的sheet var worksheet = package.Workbook.Worksheets.Add(“sheet1"); 5.单元格赋值,这里多说一句,NPOI必须先创建单元格,然后再给单元格赋值,而Epplus不需要,直接找到单元格进行赋值就可以了. worksheet.Cells[int row, int col].Value = “”; 或者 worksheet.Cells["A1"].Value = “”; 6.合并单元格 worksheet.Cells[int fromRow, fromCol, int toRow,int toCol].Merge = true; 7.获取某一个区域 var rangeData= worksheet.Cells[fromRow, fromCol, toRow, toCol]; 8.设置字体 worksheet.Cells.Style.Font.Name= “正楷”; worksheet.Cells.Style.Font.Color worksheet.Cells.Style.Font.Size 9.设置边框的属性 worksheet.Cells.Style.Border.Left.Style= ExcelBorderStyle.Thin ; worksheet.Cells.Style.Border.Right.Style= ExcelBorderStyle.Thin; worksheet.Cells.Style.Border.Top.Style= ExcelBorderStyle.Thin; worksheet.Cells.Style.Border.Bottom.Style= ExcelBorderStyle.Thin; 10.对齐方式 worksheet.Cells.Style.HorizontalAlignment=ExcelHorizontalAlignment.Center; worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom; 11. 设置整个sheet的背景色 worksheet.Cells.Style.Fill.PatternType= ExcelFillStyle.Solid; worksheet.Cells.Style.Fill.BackgroundColor.SetColor(Color.LightBlue); 12.折行显示 worksheet.Cells.Style.WrapText= true; 13.单元格自动适应大小 worksheet.Cells.Style.ShrinkToFit= true; 14.格式化单元格value值 worksheet.Cells.Style.Numberformat.Format= "0.00"; 15.锁定 worksheet.Cells["A1"].Style.Locked= true; 注:此处锁定某一个单元格的时候,只有在整个sheet被锁定的情况下才可以被锁定,不然加上锁定属性也是不起作用的~~ 二.Epplus另一个出色的地方就是支持图表的列印.功能的實現很簡單,難點在于需求比較細的點上,epplus可能不好實現,但是總的來說是比較好的一個列印圖表的工具 1.简单介绍一下可以实现的图表类型: 直條圖、折綫圖、圓形圖、橫條圖、散佈圖、區域圖 等類型的圖表 2.使用:分为三步, 第一步是将需要显示在图表中的 数据列印到excel中. 第二步是创建所需要的图表类型(折线图为例) var chart = (worksheet.Drawings.AddChart("LineChart", eChartType.Line) as ExcelLineChart); 第三步为图表添加第一步列印的数据区间就可以了 chart.Series.Add(Y軸顯示的數據源,X軸顯示的數據源) 3.图表的功能就这样实现了,很简单吧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值