Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using OfficeOpenXml;
- namespace BST.ExcelImporter.Extensions
- {
- public static class WorksheetExtensions
- {
- public static ExcelAddressBase GetValuedDimension(this ExcelWorksheet worksheet)
- {
- ExcelAddressBase dimension = worksheet.Dimension;
- if (dimension == null) return null;
- ExcelRange cells = worksheet.Cells[dimension.Address];
- Int32 minRow = 0, minCol = 0, maxRow = 0, maxCol = 0;
- bool hasValue = false;
- foreach (ExcelRangeBase cell in cells.Where(cell => cell.Value != null))
- {
- if (!hasValue)
- {
- minRow = cell.Start.Row;
- minCol = cell.Start.Column;
- maxRow = cell.End.Row;
- maxCol = cell.End.Column;
- hasValue = true;
- }
- else
- {
- if (cell.Start.Column < minCol)
- {
- minCol = cell.Start.Column;
- }
- if (cell.End.Row > maxRow)
- {
- maxRow = cell.End.Row;
- }
- if (cell.End.Column > maxCol)
- {
- maxCol = cell.End.Column;
- }
- }
- }
- return hasValue ? new ExcelAddressBase(minRow, minCol, maxRow, maxCol) : null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment