From a7f3ea30262623ce38f3f4d4c868171053d269d9 Mon Sep 17 00:00:00 2001 From: VinothSF5015 Date: Tue, 12 May 2026 15:49:44 +0530 Subject: [PATCH 1/3] 1005966 - Content Added --- Document-Processing-toc.html | 3 + ...ast-row-of-a-particular-column-in-excel.md | 164 ++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 Document-Processing/Excel/Excel-Library/NET/faqs/how-to-find-the-last-row-of-a-particular-column-in-excel.md diff --git a/Document-Processing-toc.html b/Document-Processing-toc.html index 610dc233e..33ffd4ecc 100644 --- a/Document-Processing-toc.html +++ b/Document-Processing-toc.html @@ -6805,6 +6805,9 @@
  • How to merge cells preserving topleft value and format in XlsIO?
  • +
  • + How to find the last row of a particular column in an Excel file using XlsIO? +
  • diff --git a/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-find-the-last-row-of-a-particular-column-in-excel.md b/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-find-the-last-row-of-a-particular-column-in-excel.md new file mode 100644 index 000000000..85a22f9fe --- /dev/null +++ b/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-find-the-last-row-of-a-particular-column-in-excel.md @@ -0,0 +1,164 @@ +--- +title: Last row of a particular column in an Excel file using XlsIO | Syncfusion +description: This FAQ explains how to find the last row with data in a specific column of an Excel worksheet using Syncfusion XlsIO and retrieve used range boundaries. +platform: document-processing +control: XlsIO +documentation: UG +--- + +# How to find the last row of a particular column in an Excel file using XlsIO? + +Finding the last row of a specific column helps you determine the exact extent of data in that column. XlsIO provides access to the worksheet's used range through the [IWorksheet.UsedRange](https://help.syncfusion.com/cr/document-processing/Syncfusion.XlsIO.IWorksheet.html) property, which you can use to iterate backward through rows and identify cells with data in a particular column. + +The following code example illustrates how to find the last row containing data in a specific column. + +{% tabs %} +{% highlight c# tabtitle="C# [Cross-platform]" %} +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + // Create an instance of ExcelEngine + IApplication application = excelEngine.Excel; + IWorkbook workbook = application.Workbooks.Create(1); + + // Access the first worksheet in the workbook + IWorksheet sheet = workbook.Worksheets[0]; + + // Populate data in specific ranges to demonstrate last row detection + sheet["A1:B10"].Text = "10"; + sheet["C1:C5"].Text = "20"; + + // Find the last row in column C (column index 3) + int lastRow = GetLastRow(3, sheet as WorksheetImpl); + Console.WriteLine("Last Row in Column C: " + lastRow); + + // Save the workbook to the specified output path + workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx")); +} + +// Helper method to find the last row containing data in a specified column +private static int GetLastRow(int column, WorksheetImpl worksheet) +{ + // Get the starting and ending row indices of the used range + int firstRow = worksheet.UsedRange.Row; + int lastRow = worksheet.UsedRange.LastRow; + + // Iterate backward through rows to find the last cell with data in the column + for (int iRow = lastRow; iRow >= firstRow; iRow--) + { + // Get or create the row storage for the current row + RowStorage rowStorage = WorksheetHelper.GetOrCreateRow(worksheet, iRow - 1, false); + if (rowStorage != null) + { + // Enumerate through cells in the row to check for data in the target column + RowStorageEnumerator enumerator = rowStorage.GetEnumerator(worksheet.RecordExtractor) as RowStorageEnumerator; + while (enumerator.MoveNext()) + { + // Return the row number when the column is found + if (enumerator.ColumnIndex + 1 == column) + return iRow; + } + } + } + return -1; +} +{% highlight %} + +{% highlight c# tabtitle="C# [Windows-specific]" %} +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + // Create an instance of ExcelEngine + IApplication application = excelEngine.Excel; + IWorkbook workbook = application.Workbooks.Create(1); + + // Access the first worksheet in the workbook + IWorksheet sheet = workbook.Worksheets[0]; + + // Populate data in specific ranges to demonstrate last row detection + sheet["A1:B10"].Text = "10"; + sheet["C1:C5"].Text = "20"; + + // Find the last row in column C (column index 3) + int lastRow = GetLastRow(3, sheet as WorksheetImpl); + Console.WriteLine("Last Row in Column C: " + lastRow); + + // Save the workbook to the specified output path + workbook.SaveAs("Output.xlsx"); +} + +// Helper method to find the last row containing data in a specified column +private static int GetLastRow(int column, WorksheetImpl worksheet) +{ + // Get the starting and ending row indices of the used range + int firstRow = worksheet.UsedRange.Row; + int lastRow = worksheet.UsedRange.LastRow; + + // Iterate backward through rows to find the last cell with data in the column + for (int iRow = lastRow; iRow >= firstRow; iRow--) + { + // Get or create the row storage for the current row + RowStorage rowStorage = WorksheetHelper.GetOrCreateRow(worksheet, iRow - 1, false); + if (rowStorage != null) + { + // Enumerate through cells in the row to check for data in the target column + RowStorageEnumerator enumerator = rowStorage.GetEnumerator(worksheet.RecordExtractor) as RowStorageEnumerator; + while (enumerator.MoveNext()) + { + // Return the row number when the column is found + if (enumerator.ColumnIndex + 1 == column) + return iRow; + } + } + } + return -1; +} +{% highlight %} + +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} +Sub Main(args As String()) + Using excelEngine As New ExcelEngine() + ' Create an instance of ExcelEngine + Dim application As IApplication = excelEngine.Excel + Dim workbook As IWorkbook = application.Workbooks.Create(1) + + ' Access the first worksheet in the workbook + Dim sheet As IWorksheet = workbook.Worksheets(0) + + ' Populate data in specific ranges to demonstrate last row detection + sheet("A1:B10").Text = "10" + sheet("C1:C5").Text = "20" + + ' Find the last row in column C (column index 3) + Dim lastRow As Integer = GetLastRow(3, TryCast(sheet, WorksheetImpl)) + Console.WriteLine("Last Row in Column C: " & lastRow) + + ' Save the workbook to the specified output path + workbook.SaveAs("Output.xlsx") + End Using +End Sub +' Helper method to find the last row containing data in a specified column +Private Function GetLastRow(column As Integer, worksheet As WorksheetImpl) As Integer + ' Get the starting and ending row indices of the used range + Dim firstRow As Integer = worksheet.UsedRange.Row + Dim lastRow As Integer = worksheet.UsedRange.LastRow + + ' Iterate backward through rows to find the last cell with data in the column + For iRow As Integer = lastRow To firstRow Step -1 + ' Get or create the row storage for the current row + Dim rowStorage As RowStorage = WorksheetHelper.GetOrCreateRow(worksheet, iRow - 1, False) + If rowStorage IsNot Nothing Then + ' Enumerate through cells in the row to check for data in the target column + Dim enumerator As RowStorageEnumerator = TryCast(rowStorage.GetEnumerator(worksheet.RecordExtractor), RowStorageEnumerator) + While enumerator.MoveNext() + ' Return the row number when the column is found + If enumerator.ColumnIndex + 1 = column Then + Return iRow + End If + End While + End If + Next + Return -1 +End Function +{% highlight %} +{% endtabs %} + +A complete working example in C# is present on this GitHub page. \ No newline at end of file From 06ef138fd096480c8031db91df16863e3078d04b Mon Sep 17 00:00:00 2001 From: VinothSF5015 Date: Tue, 12 May 2026 16:05:23 +0530 Subject: [PATCH 2/3] 1005966 - Content added --- Document-Processing-toc.html | 2 +- ...t-the-last-row-of-a-column-using-xlsio-in-excel.md} | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) rename Document-Processing/Excel/Excel-Library/NET/faqs/{how-to-find-the-last-row-of-a-particular-column-in-excel.md => how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md} (97%) diff --git a/Document-Processing-toc.html b/Document-Processing-toc.html index 33ffd4ecc..bad8307d8 100644 --- a/Document-Processing-toc.html +++ b/Document-Processing-toc.html @@ -6806,7 +6806,7 @@ How to merge cells preserving topleft value and format in XlsIO?
  • - How to find the last row of a particular column in an Excel file using XlsIO? + How to get the last row of a column using XlsIO in Excel?
  • diff --git a/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-find-the-last-row-of-a-particular-column-in-excel.md b/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md similarity index 97% rename from Document-Processing/Excel/Excel-Library/NET/faqs/how-to-find-the-last-row-of-a-particular-column-in-excel.md rename to Document-Processing/Excel/Excel-Library/NET/faqs/how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md index 85a22f9fe..364bb4b2f 100644 --- a/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-find-the-last-row-of-a-particular-column-in-excel.md +++ b/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md @@ -1,12 +1,12 @@ --- -title: Last row of a particular column in an Excel file using XlsIO | Syncfusion +title: Last row of a column in Excel file using XlsIO | Syncfusion description: This FAQ explains how to find the last row with data in a specific column of an Excel worksheet using Syncfusion XlsIO and retrieve used range boundaries. platform: document-processing control: XlsIO documentation: UG --- -# How to find the last row of a particular column in an Excel file using XlsIO? +# How to get the last row of a column using XlsIO in Excel? Finding the last row of a specific column helps you determine the exact extent of data in that column. XlsIO provides access to the worksheet's used range through the [IWorksheet.UsedRange](https://help.syncfusion.com/cr/document-processing/Syncfusion.XlsIO.IWorksheet.html) property, which you can use to iterate backward through rows and identify cells with data in a particular column. @@ -61,7 +61,7 @@ private static int GetLastRow(int column, WorksheetImpl worksheet) } return -1; } -{% highlight %} +{% endhighlight %} {% highlight c# tabtitle="C# [Windows-specific]" %} using (ExcelEngine excelEngine = new ExcelEngine()) @@ -111,7 +111,7 @@ private static int GetLastRow(int column, WorksheetImpl worksheet) } return -1; } -{% highlight %} +{% endhighlight %} {% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} Sub Main(args As String()) @@ -158,7 +158,7 @@ Private Function GetLastRow(column As Integer, worksheet As WorksheetImpl) As In Next Return -1 End Function -{% highlight %} +{% endhighlight %} {% endtabs %} A complete working example in C# is present on this GitHub page. \ No newline at end of file From 06d75294d7167e3f6246ee45d2fe88bf8cc9d7b3 Mon Sep 17 00:00:00 2001 From: VinothSF5015 Date: Tue, 12 May 2026 17:30:05 +0530 Subject: [PATCH 3/3] 1005966 - Sample Modified --- .../how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md b/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md index 364bb4b2f..4fa7dcefc 100644 --- a/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md +++ b/Document-Processing/Excel/Excel-Library/NET/faqs/how-to-get-the-last-row-of-a-column-using-xlsio-in-excel.md @@ -161,4 +161,4 @@ End Function {% endhighlight %} {% endtabs %} -A complete working example in C# is present on this GitHub page. \ No newline at end of file +A complete working example in C# is present on this GitHub page. \ No newline at end of file