EPPlus
引入EPPlus的NuGet程序包
一丶导出excel文件
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
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
作者:chenze 出处:https://www.cnblogs.com/chenze-Index/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |