NativeExcel2 for .NET Developer's Reference

Split and Freeze

Example

This example demonstrates how to split and freeze panes.

Output file: console-split.xls

[C#]
using System;
using NativeExcel;

namespace Console_Split
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName = "console-split.xls";
            CreateWorkbook(FileName);
            OpenWorkbookWithExcel(FileName);
        }

        static void CreateWorkbook(string FileName) {
            IWorksheet sheet;
            IRange range;
            //Create a new workbook
            IWorkbook book = NativeExcel.Factory.CreateWorkbook();
        
            //Split sheet
            //Add worksheet
            sheet = book.Worksheets.Add();
            sheet.Name = "Split"; 
            range = sheet.Range["A1:P200"];
            //fill some data
            for (int r=1; r < range.Rows.Count; r++) {
                for (int c=1; c < range.Columns.Count; c++) {    
                     range[r,c].Value = r * 100 + c;
                } 
            } 
            sheet.SplitColumn = 3;
            sheet.SplitRow = 2;


            //Freeze panes
            sheet = book.Worksheets.Add();
            sheet.Name = "Freeze"; 
            range = sheet.Range["A1:P200"];
            //fill some data
            for (int r=1; r < range.Rows.Count; r++) {
                for (int c=1; c < range.Columns.Count; c++) {    
                     range[r,c].Value = r * 100 + c;
                } 
            } 

            //Split
            sheet.SplitColumn = 3;
            sheet.SplitRow = 2;
            //Freeze
            sheet.FreezePanes = true;

            book.SaveAs(FileName);
        }

        static void OpenWorkbookWithExcel(string FileName){
            try {
                System.Diagnostics.Process.Start(FileName);
            } catch {
                Console.WriteLine(FileName + " created in application folder");
            }
        }
    }
}
[Visual Basic]
imports System
imports NativeExcel

Module Console_Split

    Sub Main()
        Dim FileName As String = "console-split.xls"
        CreateWorkbook(FileName)
        OpenWorkbookWithExcel(FileName)
    End Sub

    Sub CreateWorkbook(FileName As String)
        Dim sheet As IWorksheet
        Dim range As IRange
        'Create a new workbook
        Dim book As IWorkbook = NativeExcel.Factory.CreateWorkbook()
        
        'Split sheet
        'Add worksheet
        sheet = book.Worksheets.Add()
        sheet.Name = "Split"
        range = sheet.Range("A1:P200")

        'fill some data
        For r As Integer = 1 To range.Rows.Count
            For c As Integer = 1 To range.Columns.Count    
                range(r,c).Value = r * 100 + c
            Next c 
        Next r

        sheet.SplitColumn = 3
        sheet.SplitRow = 2


        'Freeze panes
        sheet = book.Worksheets.Add()
        sheet.Name = "Freeze"
        range = sheet.Range("A1:P200")
        'fill some data
        For r As Integer = 1 To range.Rows.Count
            For c As Integer = 1 To range.Columns.Count
               range(r,c).Value = r * 100 + c
            Next c 
        Next r

        'Split
        sheet.SplitColumn = 3
        sheet.SplitRow = 2

        'Freeze
        sheet.FreezePanes = True

        'Save workbook
        book.SaveAs(FileName)

    End Sub

    Sub OpenWorkbookWithExcel(FileName As String)
        Try
           System.Diagnostics.Process.Start(FileName)
        Catch
           Console.WriteLine(FileName + " created in application folder")
        End Try  
    End Sub

End Module