EPPlus
Posted chenze-index
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EPPlus相关的知识,希望对你有一定的参考价值。
引入EPPlus的NuGet程序包
一丶导出excel文件
1 static void Main(string[] args) 2 { 3 FileInfo newFile = new FileInfo(@"E: est.xlsx"); 4 if (newFile.Exists) 5 { 6 newFile.Delete(); 7 newFile = new FileInfo(@"E: est.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: est.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/
以上是关于EPPlus的主要内容,如果未能解决你的问题,请参考以下文章
.NET Core(C#) EPPlus写入保存Excel(.xlsx)文件的方法及示例代码