Tuesday, 14 April 2020

How to Freeze or Unfreeze Panes in Excel in Java


Freeze Panes in Excel is used to fix a row or column so that it is always displayed, regardless of what part of the spreadsheet is being displayed. There are three types of freeze panes in Excel, and they are respectively freeze top row, freeze first column as well as freeze rows and column at the same time. Besides, you can unfreeze panes so that they can been scrolled when you scroll vertically or horizontally. In this article, I’ll show you how to freeze or unfreeze panes in Excel by using Free Spire.XLS for Java.



ADD SPIRE.XLS.JAR AS DEPENDENCY




Download the latest version of FreeSpire.XLS for Java, unzip it and import Spire.Xls.jar located in the lib folder in your project as a dependency. The following screenshot is what it finally looks like.




Using the code


Freeze the Top Row
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class FreezeTopRows {
   
public static void main(String[] args) {
       
//Create a Workbook instance
       
Workbook workbook = new Workbook();

       
//Load a sample Excel file
       
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

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

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

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

Output
Freeze the First Column
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class FreezeFirstColumn {
   
public static void main(String[] args) {
       
//Create a Workbook instance
       
Workbook workbook = new Workbook();

      
//Load a sample Excel file
       
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

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

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

      
//Save to file
       
workbook.saveToFile("output/FreezeFirstColumn.xlsx", ExcelVersion.Version2016);
    }
}
Output
Freeze Rows and Columns at the Same Time
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class FreezeSpecificRowAndColumn {
   
public static void main(String[] args) {
       
//Create a Workbook instance
       
Workbook workbook = new Workbook();

       
//Load a sample Excel file
       
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

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

       
//Freeze the second row and the second column
       
sheet.freezePanes(3,3);

       
//Save to file
       
workbook.saveToFile("output/FreezeSpecificRowAndColumn.xlsx", ExcelVersion.Version2016);
    }
}
Output
Unfreeze Panes
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class UnfreezeRowsAndColumn {
   
public static void main(String[] args) {
       
//Create a Workbook instance
       
Workbook workbook = new Workbook();

       
//Load a sample Excel file
       
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\FreezeSpecificRowsAndColumns.xlsx");

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

       
//Unfreeze panes
       
sheet.removePanes();

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


}

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...