|
| 1 | +--- |
| 2 | +title: Retrieve the first cell in the used range in Excel | Syncfusion |
| 3 | +description: Code example to retrieve the first cell in the used range in an Excel worksheet using Syncfusion .NET Excel library (XlsIO). |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to retrieve the first cell in the used range in Excel? |
| 10 | + |
| 11 | +The following code examples demonstrate retrieving the first cell in the used range of an Excel worksheet using C# (Cross-platform and Windows-specific) and VB.NET. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/First%20used%20cell%20in%20used%20range/.NET/FirstUsedCellInUsedRange/FirstUsedCellInUsedRange/Program.cs,180" %} |
| 15 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 16 | +{ |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 19 | + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); |
| 20 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 21 | + |
| 22 | + //Get the used range of the worksheet |
| 23 | + IRange usedRange = worksheet.UsedRange; |
| 24 | + |
| 25 | + //Get the first cell from the used range |
| 26 | + IRange firstCell = worksheet.Range[usedRange.Row, usedRange.Column]; |
| 27 | + |
| 28 | + //Get the address of the first cell |
| 29 | + string firstCellAddress = firstCell.AddressLocal; |
| 30 | + |
| 31 | + //Display the address of the first cell |
| 32 | + Console.WriteLine("The address of the first used cell in used range is: " + firstCellAddress); |
| 33 | + |
| 34 | + //Save the workbook |
| 35 | + workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx")); |
| 36 | +} |
| 37 | +{% endhighlight %} |
| 38 | + |
| 39 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 40 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 41 | +{ |
| 42 | + IApplication application = excelEngine.Excel; |
| 43 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 44 | + IWorkbook workbook = application.Workbooks.Open("Input.xlsx"); |
| 45 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 46 | + |
| 47 | + //Get the used range of the worksheet |
| 48 | + IRange usedRange = worksheet.UsedRange; |
| 49 | + |
| 50 | + //Get the first cell from the used range |
| 51 | + IRange firstCell = worksheet.Range[usedRange.Row, usedRange.Column]; |
| 52 | + |
| 53 | + //Get the address of the first cell |
| 54 | + string firstCellAddress = firstCell.AddressLocal; |
| 55 | + |
| 56 | + //Display the address of the first cell |
| 57 | + Console.WriteLine("The address of the first used cell in used range is: " + firstCellAddress); |
| 58 | + |
| 59 | + //Save the workbook |
| 60 | + workbook.SaveAs("Output.xlsx"); |
| 61 | +} |
| 62 | +{% endhighlight %} |
| 63 | + |
| 64 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 65 | +Using excelEngine As New ExcelEngine() |
| 66 | + Dim application As IApplication = excelEngine.Excel |
| 67 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 68 | + Dim workbook As IWorkbook = application.Workbooks.Open("Input.xlsx") |
| 69 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 70 | + |
| 71 | + 'Get the used range of the worksheet |
| 72 | + Dim usedRange As IRange = worksheet.UsedRange |
| 73 | + |
| 74 | + 'Get the first cell from the used range |
| 75 | + Dim firstCell As IRange = worksheet.Range(usedRange.Row, usedRange.Column) |
| 76 | + |
| 77 | + 'Get the address of the first cell |
| 78 | + Dim firstCellAddress As String = firstCell.AddressLocal |
| 79 | + |
| 80 | + 'Display the address of the first cell |
| 81 | + Console.WriteLine("The address of the first used cell in used range is: " & firstCellAddress) |
| 82 | + |
| 83 | + 'Save the workbook |
| 84 | + workbook.SaveAs("Output.xlsx") |
| 85 | +End Using |
| 86 | +{% endhighlight %} |
| 87 | +{% endtabs %} |
| 88 | + |
| 89 | +A complete working example in C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/First%20used%20cell%20in%20used%20range/.NET/FirstUsedCellInUsedRange">this GitHub page</a>. |
0 commit comments