1 string filePath = @"C:\Users\yangqinglin\Desktop\test.xlsx"; 2 IWorkbook wk = null; 3 string extension = System.IO.Path.GetExtension(filePath); 4 FileStream fs = File.OpenRead(filePath); 5 if (extension.Equals(".xls")) 6 { 7 //把xls文件中的数据写入wk中 8 wk = new HSSFWorkbook(fs); 9 } 10 else 11 { 12 //把xlsx文件中的数据写入wk中 13 wk = new XSSFWorkbook(fs); 14 } 15 fs.Close(); 16 17 int sheetCount = wk.NumberOfSheets;//获取sheet的数量 18 ISheet sheet = wk.GetSheetAt(0);//第一个sheet页(列表) 19 int rowCount = sheet.LastRowNum; 20 IRow row = sheet.GetRow(0); //读取当前行数据 21 22 #region 读取第一个sheet页面 23 for (int i = 0; i <= sheet.LastRowNum; i++) 24 { 25 row = sheet.GetRow(i); //读取当前行数据 26 if (row != null) 27 { 28 Console.WriteLine(row.GetCell(1).ToString_()); 29 Console.WriteLine("______________________"); 30 } 31 } 32 #endregion