How to Trim Cell Range
The IronXL library allow to removes all empty rows and columns on the range borders in C# code without using Office Interop. This feature allows for efficient data processing and manipulation without the overhead of interacting with the Office suite.
How to Trim Cell Range in Excel
- Download C# library to trim cell range
- Open existing or create new Excel spreadsheet
- Select a range, row, or column to be trimmed
- Use
Trim
method on the selected range, row, or column - Check the return Range of the trim method
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronXL on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming Excel with C#.
Install-Package IronXL.Excel
Consider installing the IronXL DLL directly. Download and manually install it for your project or GAC form: IronXL.zip
Manually install into your project
Download DLLTrim Cell Range Example
Select the desired Range of cells and apply Trim
method on it. This method trims leading and trailing empty cells from the selected range.
Tips
Trim
method does not remove empty cells located in the middle of rows and columns within the range. To address this, you can apply sorting to push those empty cells to either top or bottom of the range.:path=/static-assets/excel/content-code-examples/how-to/trim-cell-range-column.cs
using IronXL;
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet workSheet = workBook.DefaultWorkSheet;
workSheet["A2"].Value = "A2";
workSheet["A3"].Value = "A3";
workSheet["B1"].Value = "B1";
workSheet["B2"].Value = "B2";
workSheet["B3"].Value = "B3";
workSheet["B4"].Value = "B4";
// Retrieve column
RangeColumn column = workSheet.GetColumn(0);
// Apply trimming
Range trimmedColumn = workSheet.GetColumn(0).Trim();
Imports IronXL
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
Private workSheet("A2").Value = "A2"
Private workSheet("A3").Value = "A3"
Private workSheet("B1").Value = "B1"
Private workSheet("B2").Value = "B2"
Private workSheet("B3").Value = "B3"
Private workSheet("B4").Value = "B4"
' Retrieve column
Private column As RangeColumn = workSheet.GetColumn(0)
' Apply trimming
Private trimmedColumn As Range = workSheet.GetColumn(0).Trim()