How do I Avoid Used Range that Include Empty Cells in WinForms XlsIO?
Microsoft Excel allows you to select all the cells in the used range using the following shortcut sequence:
- Press Ctrl + Home to select cell A1.
- Press Ctrl + Shift + End to select all cells from A1 to the last used cell.
Microsoft Excel output
As you can see in the screenshot above, there is nothing visible in cells of the 15th and 16th rows, but they are included in the selected used range. Perhaps there was a value in those cells that was deleted, or the cells are formatted. Let us see how to avoid the empty cells and select the actual used range in an Excel document.
Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files. Using this library, you can select the actual used range in an Excel document excluding the empty cells that contain formatting without text.
Steps to select used range in a sample document without including the empty cells, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package
Step 3: Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 4: Use the following code snippet to select the used range without empty cells in the sample Excel document, and to add border around the selected cells which highlights the actual used range in the saved Excel document.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { // Instantiate the application object IApplication application = excelEngine.Excel; // Add a workbook IWorkbook workbook = application.Workbooks.Open("Sample.xlsx"); // Get the first sheet IWorksheet sheet = workbook.Worksheets[0]; // UsedRange excludes the blank cells sheet.UsedRangeIncludesFormatting = false; // Adding border to highlight the used range sheet.UsedRange.BorderAround(); // Save the file workbook.SaveAs("UsedRange.xlsx"); System.Diagnostics.Process.Start("UsedRange.xlsx"); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the application object Dim application As IApplication = excelEngine.Excel 'Add a workbook Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx") 'Get the first sheet Dim sheet As IWorksheet = workbook.Worksheets(0) 'UsedRange excludes the blank cells sheet.UsedRangeIncludesFormatting = False 'Adding border to highlight the used range sheet.UsedRange.BorderAround() 'Save the file workbook.SaveAs("UsedRange.xlsx") System.Diagnostics.Process.Start("UsedRange.xlsx") End Using
A complete Windows Forms working example of how to exclude empty cells used range in an Excel document can be downloaded from exclude empty cells in Excel used range.zip.
By executing the program, you will get the output Excel file as shown below.
Output Excel document
Refer here to explore the rich set of Syncfusion® Excel (XlsIO) library features.
See Also:
How to set an active cell in a worksheet?
How to retrieve cell value without enabling sheet calculations in C#,VB.NET?
Is it possible to AutoFit a row that contains a cell merged with cells in other rows?
How to resize comment box using C#,VB.NET?
How to set Freeze panes in Excel using C#,VB.NET?
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering Syncfusion® license keys in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how do I Avoid Used Range that Include Empty Cells in WinForms XlsIO
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!