引入EPPlus的NuGet程序包
一丶导出excel文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 static void Main(string[] args) 2 { 3 FileInfo newFile = new FileInfo(@"E:\test.xlsx"); 4 if (newFile.Exists) 5 { 6 newFile.Delete(); 7 newFile = new FileInfo(@"E:\test.xlsx"); 8 } 9 using (ExcelPackage package = new ExcelPackage(newFile)) 10 { 11 ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test"); 12 worksheet.Cells[1, 1].Value = "名称"; 13 worksheet.Cells[1, 2].Value = "价格"; 14 worksheet.Cells[1, 3].Value = "销量"; 15 16 worksheet.Cells[2, 1].Value = "大米"; 17 worksheet.Cells[2, 2].Value = 56; 18 worksheet.Cells[2, 3].Value = 100; 19 20 worksheet.Cells[3, 1].Value = "玉米"; 21 worksheet.Cells[3, 2].Value = 45; 22 worksheet.Cells[3, 3].Value = 150; 23 24 worksheet.Cells[4, 1].Value = "小米"; 25 worksheet.Cells[4, 2].Value = 38; 26 worksheet.Cells[4, 3].Value = 130; 27 28 worksheet.Cells[5, 1].Value = "糯米"; 29 worksheet.Cells[5, 2].Value = 22; 30 worksheet.Cells[5, 3].Value = 200; 31 32 package.Save(); 33 } 34 Console.WriteLine("ok"); 35 Console.ReadLine(); 36 }
二丶读取excel
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 static void Main(string[] args) 2 { 3 FileInfo newFile = new FileInfo(@"E:\test.xlsx"); 4 if (!newFile.Exists) 5 { 6 return; 7 } 8 using (ExcelPackage package = new ExcelPackage(newFile)) 9 { 10 //读取sheet未test1的内容 11 ExcelWorksheet worksheet = package.Workbook.Worksheets["test1"]; 12 //ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; 13 //ExcelWorksheet worksheet = package.Workbook.Worksheets.First(); 14 string tmp = worksheet.Cells[1, 1].Text; 15 Console.WriteLine(tmp); 16 } 17 Console.WriteLine("ok"); 18 Console.ReadLine(); 19 }
参考网址:https://www.cnblogs.com/rumeng/tag/Epplus/
NPOI和EPPlus都可以对excel进行处理,他们又有着怎么区别:
https://www.cnblogs.com/tanpeng/p/6155749.html
EPPlus官网地址:https://archive.codeplex.com/?p=epplus
NPOI官网地址:https://archive.codeplex.com/?p=npoi