DEV Community

CodeSharing
CodeSharing

Posted on

Java Delete Blank Rows and Columns in Excel

When we are operating a Excel document that contains a large amount of data, some blank rows or columns may appear due to the modification of the data. At this time, we can use Free Spire.XLS for Java to delete these blank rows and columns in the Excel worksheet in batches. In this article, I will share the Java code used.

Installation
Method 1: Download the Free Spire.XLS for Java and unzip it. Then add the Spire.Xls.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

Enter fullscreen mode Exit fullscreen mode

The Original Excel Document:

Alt Text

Delete Blank Rows and Columns:

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

public class MergeCells {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file
        workbook.loadFromFile("test1.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        //Merge cells by range
        sheet.getRange().get("A1:C1").merge();

        //Save the resultant file
        workbook.saveToFile("MergeCells.xlsx", FileFormat.Version2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Top comments (0)