c#用EPPLUS操作excel

时间:2023-03-09 09:33:30
c#用EPPLUS操作excel

参考:

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

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

结果:

c#用EPPLUS操作excel

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

c#用EPPLUS操作excel

之后双击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,安装好效果如下:

c#用EPPLUS操作excel

方法是:

c#用EPPLUS操作excel

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

c#用EPPLUS操作excel

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