- excel address ( var cr = new CellReference("D5");)
1 var workbook = new XSSFWorkbook(stream); 2 var sheet = workbook.GetSheetAt(0); 3 4 var cr = new CellReference("D5"); 5 var row = sheet.GetRow(cr.Row); 6 var cell = row.GetCell(cr.Col); 7 8 var val = getCellValue(cell); 9 10 private object getCellValue(ICell cell) 11 { 12 object cValue = string.Empty; 13 switch (cell.CellType) 14 { 15 case (CellType.Unknown | CellType.Formula | CellType.Blank): 16 cValue = cell.ToString(); 17 break; 18 case CellType.Numeric: 19 cValue = cell.NumericCellValue; 20 break; 21 case CellType.String: 22 cValue = cell.StringCellValue; 23 break; 24 case CellType.Boolean: 25 cValue = cell.BooleanCellValue; 26 break; 27 case CellType.Error: 28 cValue = cell.ErrorCellValue; 29 break; 30 default: 31 cValue = string.Empty; 32 break; 33 } 34 return cValue; 35 }View Code
2. excel range ( var cellRange = CellRangeAddress.ValueOf(range); )
1 var sheet = workbook.GetSheetAt(0); 2 var range = "E3:H8"; 3 var cellRange = CellRangeAddress.ValueOf(range); 4 5 for (var i = cellRange.FirstRow; i <= cellRange.LastRow; i++) 6 { 7 var row = sheet.GetRow(i); 8 for (var j = cellRange.FirstColumn; j <= cellRange.LastColumn; j++) 9 { 10 // skip cell with column index 5 (column F) 11 if (j == 5) continue; 12 13 // do your work here 14 Console.Write("{0}\t", row.GetCell(j)); 15 } 16 17 Console.WriteLine(); 18 }View Code
标签:case,cell,NPOI,snippet,CellType,break,var,cValue From: https://www.cnblogs.com/dufu/p/17878924.html