Delete blank rows / columns in Java Excel

When operating an Excel table that contains a large amount of data, blank rows and columns may remain due to changes in the data. At that time, Free Spire.XLS for Java can solve it. Through this library, you can delete blank rows and columns in Excel worksheets at once. Now, I will share the Java code related to the countermeasure with you.

** Import JAR package ** ** Method 1: ** Download Free Spire.XLS for Java, unzip it, and then in the lib folder Import the Spire.Xls.jar package as a dependency into your Java application.

** Method 2: ** After installing the JAR package directly from the Maven repository, configure the pom.xml file as follows:

<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>3.9.1</version>
    </dependency>
</dependencies>

** The original Excel document is: ** 11.jpg

** Java code **

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

public class DeleteBlankRowsAndColumns {
    public static void main(String[] args) {

        //Import an Excel document
        Workbook wb = new Workbook();
        wb.loadFromFile("sample1.xlsx ");

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

        //Loop all lines
        for (int i = sheet.getLastRow(); i >= 1; i--)
        {
            //Determine if it is a blank line
            if (sheet.getRows()[i-1].isBlank())
            {
                //Deletes the specified line
                sheet.deleteRow(i);
            }
        }

        //Loop through all columns
        for (int j = sheet.getLastColumn(); j >= 1; j--)
        {
            //Check if it is a blank column
            if (sheet.getColumns()[j-1].isBlank())
            {
                //Deletes the specified column
                sheet.deleteColumn(j);
            }
        }

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

Execution result: 22.png

Recommended Posts

Delete blank rows / columns in Java Excel
Add, read, and delete Excel comments in Java
Save Java PDF in Excel
Importing Excel data in Java 2
Importing Excel data in Java 3
Java sets different background colors for adjacent rows in Excel
Partization in Java
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Delete All from Java SDK in Azure Cosmos DB
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Adding columns in Rails
Azure functions in java
Simple htmlspecialchars in Java
Print Java Excel Worksheet
Boyer-Moore implementation in Java
Hello World in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Activate Excel file A1 cell of each sheet in Java