Java Delete Blank Rows and Columns in Excel 
Tags: javaexcel, rows, columns, excel
Published: 2021-12-28 08:53:27

---
title: Java Delete Blank Rows and Columns in Excel 
published: true
description: Java Excel API Delete Blank Rows and Columns
tags: JavaExcel, Rows, Columns, Excel
//cover_image: https://direct_url_to_image.jpg
---

In this article, we will demonstrate how to delete blank rows and columns in Excel in Java applications by using [Spire.XLS for Java](https://www.e-iceblue.com/Introduce/xls-for-java.html). We can detect if the rows and columns is blank or not using the method **Worksheet.getRows().isBlank()** and **Worksheet.getColumns().isBlank()**. After that we can easily remove rows and columns from Excel worksheets programmatically using the method **Worksheet.deleteRow(int)** and **Worksheet.deleteColumn(int)**.

###Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded [from this link](https://www.e-iceblue.com/Download/xls-for-java.html). If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>4.12.2</version>
    </dependency>
</dependencies>
```
###Delete Blank Rows in Excel using Java
The following are the steps to remove the blank rows in Excel:
* Create a Workbook object and load the sample Excel document using Workbook.loadFromFile() method.
* Get the desired worksheet using Workbook.getWorksheets().get() method.
* Traverse the rows and determine if the rows is blank using Worksheet.getRows().isBlank() method.
* Delete the blank rows using Worksheet.deleteRow(int) method.
* Save the document to another file using Workbook.saveToFile() method.

```java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;


public class RemoveBlankRows {
    public static void main(String[] args) throws Exception {

        //Load the sample document
        Workbook wb = new Workbook();
        wb.loadFromFile("Sample.xlsx ");

        //Get the first worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Loop through the rows
        for (int i = sheet.getLastRow(); i >= 1; i--)
        {
            //Detect if a row is blank
            if (sheet.getRows()[i-1].isBlank())
            {
                //Remove blank row
                sheet.deleteRow(i);
            }
        }


        //Save the document
        wb.saveToFile("DeleteBlankRowsAndColumns.xlsx", ExcelVersion.Version2016);


    }
}
```
![Remove Blank Rows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wt0yfogbd91999tfjuek.png)

###Delete Blank Columns in Excel using Java

The following are the steps to remove the blank columns in Excel.
* Create a Workbook object and load the sample Excel document using Workbook.loadFromFile() method.
* Get the desired worksheet using Workbook.getWorksheets().get() method.
* Traverse the columns and determine if the columns is blank using Worksheet.getColumns().isBlank() method.
* Delete the blank rows using Worksheet.deleteColumn(int) method.
* Save the document to another file using Workbook.saveToFile() method.

```java

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;


public class RemoveBlankColumns {
    public static void main(String[] args) throws Exception {

        //Load the sample document
        Workbook wb = new Workbook();
        wb.loadFromFile("Sample01.xlsx ");

        //Get the first worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Loop through the columns
        for (int i = sheet.getLastColumn(); i >= 1; i--)
        {
            //Detect if a column is blank
            if (sheet.getColumns()[i-1].isBlank())
            {
                //Remove blank column
                sheet.deleteColumn(i);
            }
        }

        //Save the document
        wb.saveToFile("DeleteBlankColumns.xlsx", ExcelVersion.Version2016);
    }
}
```

![Remove Blank Columns](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o95e1yjxqcamfsjas2vi.png)