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.2\plan\配置表\excel\t_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 }
讀取結果部分截圖: