Guest User

Untitled

a guest
Feb 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. private List<string> readRow(IRow row)
  2. {
  3. var rs = new List<string>();
  4.  
  5. // Since there would be no emtpy cell,
  6. // we start from the left-most cell and read until the end.
  7. for (int colIndex = 0; colIndex < row.LastCellNum; colIndex++)
  8. {
  9. var cell = row.GetCell(colIndex);
  10.  
  11. // Get the value of this cell, whether it is part of a merged cell or not.
  12. var cellValue = getCellValue(cell);
  13.  
  14. rs.Add(cellValue);
  15. }
  16. return rs;
  17. }
  18.  
  19. private string getCellValue(ICell cell)
  20. {
  21. var dataFormatter = new DataFormatter(CultureInfo.CurrentCulture);
  22.  
  23. // If this is not part of a merge cell,
  24. // just get this cell's value like normal.
  25. if (!cell.IsMergedCell)
  26. {
  27. return dataFormatter.FormatCellValue(cell);
  28. }
  29.  
  30. // Otherwise, we need to find the value of this merged cell.
  31. else
  32. {
  33. // Get current sheet.
  34. var currentSheet = cell.Sheet;
  35.  
  36. // Loop through all merge regions in this sheet.
  37. for (int i = 0; i < currentSheet.NumMergedRegions; i++)
  38. {
  39. var mergeRegion = currentSheet.GetMergedRegion(i);
  40.  
  41. // If this merged region contains this cell.
  42. if (mergeRegion.FirstRow <= cell.RowIndex && cell.RowIndex <= mergeRegion.LastRow &&
  43. mergeRegion.FirstColumn <= cell.ColumnIndex && cell.ColumnIndex <= mergeRegion.LastColumn)
  44. {
  45. // Find the top-most and left-most cell in this region.
  46. var firstRegionCell = currentSheet.GetRow(mergeRegion.FirstRow)
  47. .GetCell(mergeRegion.FirstColumn);
  48.  
  49. // And return its value.
  50. return dataFormatter.FormatCellValue(firstRegionCell);
  51. }
  52. }
  53. // This should never happen.
  54. throw new Exception("Cannot find this cell in any merged region");
  55. }
  56. }
  57.  
  58. // And return its value.
  59. return dataFormatter.FormatCellValue(firstRegionCell);
  60. }
  61. else
  62. return dataFormatter.FormatCellValue(cell); //return cell if isMerge is true but not in range cells
Add Comment
Please, Sign In to add comment