c#用EPPLUS操作excel
参考:
http://www.cnblogs.com/rumeng/p/3785748.html
http://www.cnblogs.com/libla/p/5824296.html#3818995
结果:
首先新建一个winform程序,然后加一个button,
之后双击button1,进入代码区域,输入下面的代码:
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApplication1epplus
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
using (ExcelPackage package = new ExcelPackage(new FileStream(@"E:\test.xlsx", FileMode.Open)))
{
for (int i = ; i <= package.Workbook.Worksheets.Count; ++i)//循环sheet
{
ExcelWorksheet sheet = package.Workbook.Worksheets[i];
for (int j = sheet.Dimension.Start.Column, k = sheet.Dimension.End.Column; j <= k; j++)
{
for (int m = sheet.Dimension.Start.Row, n = sheet.Dimension.End.Row; m <= n; m++)
{
string str = GetValue(sheet, m, j);
if (str != null)
{
// do something
}
}
}
}
} FileInfo newFile = new FileInfo(@"E:\test.xlsx");
if (newFile.Exists)
{
newFile.Delete();
newFile = new FileInfo(@"E:\test.xlsx");
}
using (ExcelPackage package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test"); worksheet.Cells.Style.WrapText = true;
worksheet.View.ShowGridLines = false;//去掉sheet的网格线 worksheet.Cells[, ].Value = "名称";
worksheet.Cells[, ].Value = "价格";
worksheet.Cells[, ].Value = "销量"; worksheet.Cells[, ].Value = "大米";
worksheet.Cells[, ].Value = ;
worksheet.Cells[, ].Value = ; worksheet.Cells[, ].Value = "玉米";
worksheet.Cells[, ].Value = ;
worksheet.Cells[, ].Value = ; worksheet.Cells[, ].Value = "小米";
worksheet.Cells[, ].Value = ;
worksheet.Cells[, ].Value = ; worksheet.Cells[, ].Value = "糯米";
worksheet.Cells[, ].Value = ;
worksheet.Cells[, ].Value = ; using (ExcelRange range = worksheet.Cells[, , , ])
{
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
} using (ExcelRange range = worksheet.Cells[, , , ])
{
range.Style.Font.Bold = true;
range.Style.Font.Color.SetColor(Color.White);
range.Style.Font.Name = "微软雅黑";
range.Style.Font.Size = ;
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(, , ));
} worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered); ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[, , , ], worksheet.Cells[, , , ]);
serie.HeaderAddress = worksheet.Cells[, ]; chart.SetPosition(, );
chart.SetSize(, );
chart.Title.Text = "销量走势";
chart.Title.Font.Color = Color.FromArgb(, , );
chart.Title.Font.Size = ;
chart.Title.Font.Bold = true;
chart.Style = eChartStyle.Style15;
chart.Legend.Border.LineStyle = eLineStyle.Solid;
chart.Legend.Border.Fill.Color = Color.FromArgb(, , ); package.Save();
} }
}
}
会提示错误, 注意要引用EPPlus,安装好效果如下:
方法是:
输入epplus,搜索,安装即可。
注意路径在E盘,没有E盘的肯定会出错。
c#用EPPLUS操作excel的更多相关文章
- 使用EPPLUS操作EXcel
使用EPPLUS操作EXcel 时间 2014-11-06 19:28:01 姜糖水 原文 http://www.cnphp6.com/archives/58648 主题 Excel 1 下载Ep ...
- C#使用第三方组件Epplus操作Excel表
Epplus操作Excel基础详解 1.什么是Epplus Epplus是一个使用Open Office XML文件格式,能读写Excel2007/2010文件的开源组件,在导出Excel的时候不需要 ...
- ASP.NET Core使用EPPlus操作Excel
1.前言 本篇文章通过ASP.NET Core的EPPlus包去操作Excel(导入导出),其使用原理与NPOI类似,导出Excel的时候不需要电脑上安装office,非常好用 2.使用 新建一个AS ...
- 使用LinqToExcel和EPPlus操作excel
1.使用LinqToExcel LinqToExcel是一个.net framework平台下开源项目,它主要实现了LINQ的语法查询Excel电子表格.类型之前的LINQToXXX如果你是LINQ语 ...
- [Solution] NPOI操作Excel
NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...
- .net 操作excel
.net 操作excel的常用组件:EPPlus,NPOI 1.NPOI,即POI的.NET版本(POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office文件, ...
- 【代码沉淀】 - EPPlus - 操作xlsx表格文件
EPPlus - Create advanced Excel spreadsheets on the server.web: http://epplus.codeplex.com/nuget: Ins ...
- C# 操作 Excel(.xls和.xlsx)文件
C#创建Excel(.xls和.xlsx)文件的三种方法 .NET 使用NPOI导入导出标准Excel C# 使用NPOI 实现Excel的简单导入导出 NET使用NPOI组件将数据导出Excel-通 ...
- [Asp.net] C# 操作Excel的几种方式 优缺点比较
在项目中我们常常需要将数据库中的数据导出成Excel文件 有一次工作中我的目的就是读取Excel到内存中,整理成指定格式 整理后再导出到Excel. 因为我要处理的每个Excel表格文件很大.一个表格 ...
随机推荐
- java类封装成dll
@参考文章1,@参考文章2,@参考文章3 1,建立测试类,注意英文注释部分,用汉语直接编译会乱码 public class Hello { //native method is used for ca ...
- swift 数组 的一些快速方法
1. filter (过滤器):返回符合条件的一个数组 let arr = [1,5,6,7,10,0] //写法1 let arr1 = arr.filter { (item) -> Bool ...
- 支付宝H5 与网页端支付开发
在日常生活中,我们基本上都是进行微信与支付宝的支付方式尽心支付,这种方式确实大大便利了我们的生活,那么如何在我们的产品中进行微信与支付宝支付的植入开发呢? 我们先进行支付宝的H5与网页端支付开发,这里 ...
- Android系统显示原理
Android的显示过程可以概括为:Android应用程序把经过测量.布局.绘制后的surface缓存数据,通过SurfaceFlinger把数据渲染到屏幕上,通过Android的刷新机制来刷新数据. ...
- GridView(网格视图)+MotionEvent(触控事件)实现可以拖动排序的网格图
1.一触碰,就回调onInterceptTouchEvent(MotionEvent ev)事件,每次的触碰都只回调一次,接着只要还没有放手,就一直回调onTouchEvent(MotionEvent ...
- php 图像处理函数
gd_info 函数:获取当前安装的GD库的信息 getimagesize 函数:获取图像的大小 image_type_to_extension 函数:获取图像类型的文件后缀 ima ...
- guide dpdk
Welcome to DPDK Guide! Contents: Setting up DPDK Important Prerequisites Setting up repositories Red ...
- HDU 2255.奔小康赚大钱 最大权匹配
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- UI设计教程:如何在设计中运用颜色
灰度优先 我们习惯在设计阶段的早期就开始调整颜色和色调.但是,当你意识到自己花了3个小时来调整主色调的时候,你发现这种行为毫无帮助.虽然把玩颜色很有吸引力,但是你应该避免在设计初期进行这种行为. 相反 ...
- Python的程序入口 __name__属性
python中每个模块都有一个 '__name__' 属性,当其值为 '__main__' 时,表名该模块自身在运行,否则是被引入的. 当一个模块被当做一个整体调用的时候,模块名.__name__ 的 ...