2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
Microsoft Excel allows you to select all the cells in the used range using the following shortcut sequence:
Micrsoft Excel output As you can see in the screen shot above, there is nothing visible in cells of 15th and 16th rows, but it is included in the selected used range. Perhaps there was a value in that cell, and it was deleted, or the cell is 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. Also, converts Excel documents to PDF files. Using this library, you can select 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? Note: 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 the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
|
2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.
sometimes when i give this , it gives compile error , some times run time error 424!!!
There should not be any compilation error or runtime exception while using this property.