c# NPOI讀取xlsx
阿新 • • 發佈:2021-12-30
[TestMethod] public void Test() { _config = new AppSettingOptions() { ResDir = "D:\\VR2\\" }; var resDir = _config.ResDir; if (Directory.Exists(resDir)) { DirectoryInfo dir = new DirectoryInfo(resDir); FileInfo[] files= dir.GetFiles("*.xlsx"); foreach (FileInfo file in files) { string filePath = file.FullName; var workbook = new XSSFWorkbook(filePath); var sheet01 = workbook.GetSheet("活動"); int rowCount = sheet01.LastRowNum;//總行數 if (rowCount > 0) { IRow firstRow = sheet01.GetRow(0);//第一行 int cellCount = firstRow.LastCellNum;//列數 var rowList = new List<string>(); for (int i = 1; i <= rowCount; i++) {var row = sheet01.GetRow(i); if (row == null) continue; foreach (var cell in row.Cells) { rowList.Add(cell.ToString()); } } } } } }