1、创建工程后,需要下载 EPPlus.dll 添加到工程中,这里有一个下载地址:https://download.csdn.net/download/myunity/10784634
2、下面仅实现读取Excel表格的列数据:
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using OfficeOpenXml; 5 6 namespace ConsoleApplication1 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 List<string> list = GetExcelColumnValue(@"D:UniuAndroid5.6.2plan配置表excel _equip.xlsx", 1, 3); 13 14 for (int i = 0; i < list.Count; ++i) 15 { 16 Console.WriteLine(list[i]); 17 } 18 19 Console.ReadKey(); 20 } 21 22 /// <summary> 23 /// 读取Excel表格列数据 24 /// </summary> 25 /// <param name="path">Excel表格所在的路径</param> 26 /// <param name="sheetIndex">需要读取的Sheet页码序号</param> 27 /// <param name="columnIndex">需要读取的列序号</param> 28 /// <returns></returns> 29 static List<string> GetExcelColumnValue(string path, int sheetIndex, int columnIndex) 30 { 31 List<string> list = new List<string>(); 32 33 FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); 34 ExcelPackage excel = new ExcelPackage(fileStream); 35 ExcelWorksheet sheet = excel.Workbook.Worksheets[sheetIndex]; 36 37 try 38 { 39 for (int i = 5; i <= sheet.Dimension.End.Row; i++) 40 { 41 var cell = sheet.Cells[i, columnIndex]; 42 if (cell != null && cell.Value != null) 43 list.Add(cell.Value.ToString()); 44 } 45 } 46 catch 47 { 48 throw; 49 } 50 finally 51 { 52 sheet.Dispose(); 53 excel.Dispose(); 54 fileStream.Dispose(); 55 } 56 57 return list; 58 } 59 } 60 }
读取结果部分截图: