Freeze Excel panes in WPF

2016-03-25 08:54:49 Written by  support iceblue
Rate this item
(0 votes)

Whenever you are working with lots of data, it can be difficult to compare information in your workbook. You may want to see certain rows or columns all the time in your worksheet, especially header cells. By freezing rows or columns in place, you can keep rows or columns visible while scrolling through the rest of the worksheet.

In the following sections, I will demonstrate how to freeze Excel panes in WPF.

Step 1: Initialize a new instance of Workbook class. Load the word document from the file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("SalesReport.xlsx");

Step 2: In our example, my workbook has several worksheets. We want to check the data from the second worksheet. Therefore, we select the second worksheet.

Worksheet sheet = workbook.Worksheets[1];

Step 3: In this case, we want to fix the first two rows and the leftmost column. The row and column will be frozen in place, as indicated by the solid grey line.

sheet.FreezePanes(2, 1);

Step 4: Save the workbook and launch the file.

workbook.SaveToFile("SalesReport Result.xlsx");
System.Diagnostics.Process.Start("SalesReport Result.xlsx");

Effective screenshot:

Freeze Excel panes in WPF

Full Codes:

[C#]
using System.Windows;
using Spire.Xls;

namespace SalesReport
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    { 
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load Excel File
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("SalesReport.xlsx");

            //Select the second worksheet

            Worksheet sheet = workbook.Worksheets[1];

            //Select to freeze the first two rows and the leftmost column

            sheet.FreezePanes(3, 2);

            //Save and Launch

            workbook.SaveToFile("SalesReport Result.xlsx");
            System.Diagnostics.Process.Start("SalesReport Result.xlsx");
        }
    }
}
[VB.NET]
Imports System.Windows
Imports Spire.Xls

Namespace SalesReport
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			'Load Excel File
			Dim workbook As New Workbook()
			workbook.LoadFromFile("SalesReport.xlsx")

			'Select the second worksheet

			Dim sheet As Worksheet = workbook.Worksheets(1)

			'Select to freeze the first two rows and the leftmost column

			sheet.FreezePanes(3, 2)

			'Save and Launch

			workbook.SaveToFile("SalesReport Result.xlsx")
			System.Diagnostics.Process.Start("SalesReport Result.xlsx")
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title:
Last modified on Friday, 24 September 2021 09:46