Set Print Titles and Area in Excel Files in C# and VB.NET

With GemBox.Spreadsheet, you can set worksheet print titles and the print area programmatically.

The following example shows how to set these options using C# and VB.NET.

Excel print titles and area set with GemBox.Spreadsheet
Screenshot of Excel print titles and area set with GemBox.Spreadsheet
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();

        // Always print 1st row.
        var worksheet1 = workbook.Worksheets.Add("Sheet1");
        worksheet1.NamedRanges.SetPrintTitles(worksheet1.Rows[0], 1);

        // Set print area (from A1 to I120):
        worksheet1.NamedRanges.SetPrintArea(worksheet1.Cells.GetSubrange("A1", "I120"));

        // Always print columns from A to F.
        var worksheet2 = workbook.Worksheets.Add("Sheet2");
        worksheet2.NamedRanges.SetPrintTitles(worksheet2.Columns[0], 6);

        // Always print columns from A to F and first row.
        var worksheet3 = workbook.Worksheets.Add("Sheet3");
        worksheet3.NamedRanges.SetPrintTitles(worksheet3.Rows[0], 1, worksheet3.Columns[0], 6);

        // Fill Sheet1 with some data.
        for (int i = 0; i < 9; i++)
            worksheet1.Cells[0, i].Value = "Column " + ExcelColumnCollection.ColumnIndexToName(i);

        for (int i = 1; i < 120; i++)
            for (int j = 0; j < 9; j++)
                worksheet1.Cells[i, j].SetValue(i + j);

        workbook.Save("Print Titles and Area.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook As New ExcelFile()

        ' Always print 1st row.
        Dim worksheet1 = workbook.Worksheets.Add("Sheet1")
        worksheet1.NamedRanges.SetPrintTitles(worksheet1.Rows(0), 1)

        ' Set print area (from A1 to I120):
        worksheet1.NamedRanges.SetPrintArea(worksheet1.Cells.GetSubrange("A1", "I120"))

        ' Always print columns from A to F.
        Dim worksheet2 = workbook.Worksheets.Add("Sheet2")
        worksheet2.NamedRanges.SetPrintTitles(worksheet2.Columns(0), 6)

        ' Always print columns from A to F and first row.
        Dim worksheet3 = workbook.Worksheets.Add("Sheet3")
        worksheet3.NamedRanges.SetPrintTitles(worksheet3.Rows(0), 1, worksheet3.Columns(0), 6)

        ' Fill Sheet1 with some data
        For i As Integer = 0 To 8
            worksheet1.Cells(0, i).Value = "Column " & ExcelColumnCollection.ColumnIndexToName(i)
        Next

        For i As Integer = 1 To 119
            For j As Integer = 0 To 8
                worksheet1.Cells(i, j).SetValue(i + j)
            Next
        Next

        workbook.Save("Print Titles and Area.%OutputFileType%")
    End Sub
End Module

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy