Dimension/Used range
The ExcelWorksheet.Dimension property exposes an address that indicates the area of the worksheet that contains cell values, formulas or styling. See example below:
using var package = new ExcelPackage();
var sheet = package.Workbook.Worksheets.Add("Sheet 1");
sheet.Cells["A1"].Value = 1;
sheet.Cells["A2"].Value = 2;
sheet.Cells["A3"].Value = 3;
sheet.Cells["B1"].Value = 4;
sheet.Cells["B2"].Value = 5;
sheet.Cells["B3"].Value = 6;
Console.WriteLine($"Dimension address: {sheet.Dimension.Address}");
Console.WriteLine($"Dimension from row: {sheet.Dimension.Start.Row}");
Console.WriteLine($"Dimension to row: {sheet.Dimension.End.Row}");
Console.WriteLine($"Dimension from column: {sheet.Dimension.Start.Column}");
Console.WriteLine($"Dimension to column: {sheet.Dimension.End.Column}");
Using package As New ExcelPackage()
Dim sheet = package.Workbook.Worksheets.Add("Sheet 1")
sheet.Cells("A1").Value = 1
sheet.Cells("A2").Value = 2
sheet.Cells("A3").Value = 3
sheet.Cells("B1").Value = 4
sheet.Cells("B2").Value = 5
sheet.Cells("B3").Value = 6
Console.WriteLine($"Dimension address: {sheet.Dimension.Address}")
Console.WriteLine($"Dimension from row: {sheet.Dimension.Start.Row}")
Console.WriteLine($"Dimension to row: {sheet.Dimension.End.Row}")
Console.WriteLine($"Dimension from column: {sheet.Dimension.Start.Column}")
Console.WriteLine($"Dimension to column: {sheet.Dimension.End.Column}")
End Using
This code will result in the following output:
Dimension address: A1:B3
Dimension from row: 1
Dimension to row: 3
Dimension from column: 1
Dimension to column: 2
Getting the worksheet dimensions for values only¶
If you want to know the first or last value or the dimension for the worksheets cells having values, your can use the properties FirstValueCell, LastValueCell and DimensionByValue exposed by the ExcelWorksheet class.