diff --git a/FAQ/Last Row/.NET/Last Row/Last Row.slnx b/FAQ/Last Row/.NET/Last Row/Last Row.slnx new file mode 100644 index 00000000..307586d0 --- /dev/null +++ b/FAQ/Last Row/.NET/Last Row/Last Row.slnx @@ -0,0 +1,3 @@ + + + diff --git a/FAQ/Last Row/.NET/Last Row/Last Row/Last Row.csproj b/FAQ/Last Row/.NET/Last Row/Last Row/Last Row.csproj new file mode 100644 index 00000000..3384bc4c --- /dev/null +++ b/FAQ/Last Row/.NET/Last Row/Last Row/Last Row.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + Last_Row + enable + enable + + + + + + + + + Always + + + + diff --git a/FAQ/Last Row/.NET/Last Row/Last Row/Output/.gitkeep b/FAQ/Last Row/.NET/Last Row/Last Row/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/FAQ/Last Row/.NET/Last Row/Last Row/Program.cs b/FAQ/Last Row/.NET/Last Row/Last Row/Program.cs new file mode 100644 index 00000000..360a8bee --- /dev/null +++ b/FAQ/Last Row/.NET/Last Row/Last Row/Program.cs @@ -0,0 +1,45 @@ +using Syncfusion.XlsIO; +using Syncfusion.XlsIO.Implementation; +using Syncfusion.XlsIO.Implementation.Collections; + +class Program +{ + static void Main(string[] args) + { + // Create an instance of ExcelEngine + using (ExcelEngine excelEngine = new ExcelEngine()) + { + IApplication application = excelEngine.Excel; + IWorkbook workbook = application.Workbooks.Create(1); + + IWorksheet sheet = workbook.Worksheets[0]; + sheet["A1:B10"].Text = "10"; + sheet["C1:C5"].Text = "20"; + + int lastRow = GetLastRow(3, sheet as WorksheetImpl); + Console.WriteLine("Last Row in Column C: " + lastRow); + + workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx")); + } + } + + private static int GetLastRow(int column, WorksheetImpl worksheet) + { + int firstRow = worksheet.UsedRange.Row; + int lastRow = worksheet.UsedRange.LastRow; + for (int iRow = lastRow; iRow >= firstRow; iRow--) + { + RowStorage rowStorage = WorksheetHelper.GetOrCreateRow(worksheet, iRow - 1, false); + if (rowStorage != null) + { + RowStorageEnumerator enumerator = rowStorage.GetEnumerator(worksheet.RecordExtractor) as RowStorageEnumerator; + while (enumerator.MoveNext()) + { + if (enumerator.ColumnIndex + 1 == column) + return iRow; + } + } + } + return -1; + } +}