isotonicq

Untitled

Apr 22nd, 2021
625
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Linq;
  3. using OfficeOpenXml;
  4.  
  5. namespace BST.ExcelImporter.Extensions
  6. {
  7.     public static class WorksheetExtensions
  8.     {
  9.         public static ExcelAddressBase GetValuedDimension(this ExcelWorksheet worksheet)
  10.         {
  11.             ExcelAddressBase dimension = worksheet.Dimension;
  12.            
  13.             if (dimension == null) return null;
  14.            
  15.             ExcelRange cells = worksheet.Cells[dimension.Address];
  16.             Int32 minRow = 0, minCol = 0, maxRow = 0, maxCol = 0;
  17.             bool hasValue = false;
  18.            
  19.             foreach (ExcelRangeBase cell in cells.Where(cell => cell.Value != null))
  20.             {
  21.                 if (!hasValue)
  22.                 {
  23.                     minRow = cell.Start.Row;
  24.                     minCol = cell.Start.Column;
  25.                     maxRow = cell.End.Row;
  26.                     maxCol = cell.End.Column;
  27.                     hasValue = true;
  28.                 }
  29.                 else
  30.                 {
  31.                     if (cell.Start.Column < minCol)
  32.                     {
  33.                         minCol = cell.Start.Column;
  34.                     }
  35.                     if (cell.End.Row > maxRow)
  36.                     {
  37.                         maxRow = cell.End.Row;
  38.                     }
  39.                     if (cell.End.Column > maxCol)
  40.                     {
  41.                         maxCol = cell.End.Column;
  42.                     }
  43.                 }
  44.             }
  45.            
  46.             return hasValue ? new ExcelAddressBase(minRow, minCol, maxRow, maxCol) : null;
  47.         }
  48.     }
  49. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×