DEV Community

CodeSharing
CodeSharing

Posted on

Freeze/ Unfreeze Excel Panes in Java Application

When viewing an Excel worksheet with a lot of data, freezing panes can help us freeze the specified row or column and keep them visible while scrolling through the rest of the worksheet, which is very convenient for us to view and compare the data.

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

Freeze Top Row:

import com.spire.xls.*;

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

        ////Load a sample Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("sample1.xlsx");

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

        //Freeze top row
        sheet.freezePanes(2,1);

        //Save to file
        workbook.saveToFile("FreezeTopRow.xlsx", ExcelVersion.Version2010);
    }
}

Enter fullscreen mode Exit fullscreen mode

Freeze Fisrt Column:

import com.spire.xls.*;

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

        //Load a sample Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("sample1.xlsx");

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

        //Freeze frist column
        sheet.freezePanes(1, 2);

        //Save to file
        workbook.saveToFile("FreezeFirstColumn.xlsx", ExcelVersion.Version2016);
    }
}

Enter fullscreen mode Exit fullscreen mode

Freeze Few Rows and Columns:

import com.spire.xls.*;

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

        //Load a sample Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("sample1.xlsx");

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

        //Freeze the first two rows and first two columns
        sheet.freezePanes(3, 3);

        //Save to file
        workbook.saveToFile("FreezePanes.xlsx", ExcelVersion.Version2016);
    }
}
Enter fullscreen mode Exit fullscreen mode

Unfreeze Panes:

import com.spire.xls.*;

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

        //Load a sample Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("sample1.xlsx");

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

        //Unfreeze panes
        sheet.removePanes();

        //Save to file
        workbook.saveToFile("UnfreezePanes.xlsx", ExcelVersion.Version2016);
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)