How to Add Repeated Headers and Footers to Excel XLSX in .NET C# using a .NET Spreadsheet API
Tags: webdev, devops, csharp, tutorial
Published: 2024-03-06 21:23:16

---
canonical_url: https://developer.mescius.com/blogs/how-to-add-repeated-headers-and-footers-to-excel-xlsx-in-net-c-sharp-using-a-net-spreadsheet-api
description: Learn how to add repeated headers and footers to Excel XLSX in .NET C# using a spreadsheet API. See more from Document Solutions today.
---
**What You Will Need**
- Visual Studio (this sample uses v2022)
- .NET 8 Core Console App
- NuGet packages: [DS.Documents.Excel](https://www.nuget.org/packages/DS.Documents.Excel)

**Controls Referenced**
- [Document Solutions for Excel, .NET Edition](https://developer.mescius.com/document-solutions/dot-net-excel-api) - A .NET Excel API
[Documentation](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html) | [Demo Explorer](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/)

**Tutorial Concept**
Advanced Excel Reporting and Printing Features in .NET applications using C# and an Excel API. Explore step-by-step guidance on programmatically configuring repeating bottom rows and spreadsheet columns in dynamically generated and printed Excel reports.

---
When dealing with multi-page printed worksheet/spreadsheet reports in Excel, you may want repeating rows at the bottom and columns at the right for better presentation. For instance, if displaying a student's final grade with subject scores on each page, repeating columns on the right is essential. Similarly, for showcasing monthly attendance, repeating rows at the bottom and columns at the right ensures clarity across the printed PDF pages:

![Excel Headers/Footers C#](//cdn.mescius.io/umb/media/rsfc3cci/03-excelheaders.png?rmode=max&width=750&height=375)

**Microsoft Excel** lacks a direct way to repeat rows at the bottom or columns at the right while printing, limiting options to the top or left. Enter [**<span>Document Solutions for Excel, .NET Edition</span>** **(DsExcel, previously GcExcel)**](https://developer.mescius.com/document-solutions/dot-net-excel-api "https://developer.mescius.com/document-solutions/dot-net-excel-api"), a server-side .NET Excel API that provides [**Page Setup** options](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ConfigurePrintSettingsviaPageSetup.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ConfigurePrintSettingsviaPageSetup.html"), including [**PrintTailRows** ](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup~PrintTailRows.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup~PrintTailRows.html")and[**PrintTailColumns**](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup~PrintTailColumns.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup~PrintTailColumns.html")properties that provide a direct solution for repeating rows or columns on each printed page. For details, please see the help topics [**Configure Columns to Repeat** **at Left and Right**](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ConfigureColumnstoRepeatatLeft.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ConfigureColumnstoRepeatatLeft.html") and [**Configure Rows to Repeat at Top and Bottom**](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ConfigureRowstoRepeatatTop.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ConfigureRowstoRepeatatTop.html").

During this tutorial, we will create a "Monthly Class Attendance" report with several rows and columns. The "Monthly Class Attendance" report will be printed onto a three-page **PDF** file. Since all of the columns won't get printed on a single **PDF** page, there is a need to repeat certain columns (Total attendance of a student in a month) at the right of each printed page. Similarly, since the abbreviated letters **T, U, E, and P** are used in every row, the user needs to see this abbreviation at the bottom of every printed page.

### Steps to Programmatically Invoke Repeating Headers and Footers in Printed Excel Files From a .NET Application

1.  [<span>Create .NET 8 Console App with .NET Excel API NuGet Package Installed</span>](#create)
2.  [<span>Open the Excel File in the .NET Excel API Workbook</span>](#open-excel)
3.  [<span>Set PrintTailColumns & PrintTialRows Properties</span>](#set)
4.  [<span>Export Excel Report to PDF</span>](#export)

Download the .NET 8 [sample app](https://cdn.mescius.io/assets/developer/blogs/gcdocuments/20240206-How-To-Add-Repeated-Headers-and-Footers-to-Excel/Excel_Repeating_Rows_Columns.zip) to follow along with the tutorial.

### <a id="create"></a>Create .NET 8 Console App with .NET Excel API NuGet Package Installed

In **Visual Studio 2022**, create a new project, and select “Console App (.NET Core)”. Add the DsExcel .NET references to the project. In the **Solution Explorer**, right-click **Dependencies** and select **Manage NuGet Packages**. In **NuGet Package Manager**, search for ‘DS.Documents.Excel’, select [**<span>DS.Documents.Excel</span>**](https://www.nuget.org/packages/DS.Documents.Excel "https://www.nuget.org/packages/DS.Documents.Excel"), and click **Install**. [See our _Quick Start_ documentation for more information on getting started](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/quick-start.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/quick-start.html").

![Excel Headers/Footers C#](//cdn.mescius.io/umb/media/eq5kgmta/04-excelheaders.png?rmode=max&width=752&height=376)

### <a id="open-excel"></a>Open the Excel File in the .NET Excel API Workbook

Next, in the “Program.cs” file, create a new DsExcel .NET workbook using the [**<span>Workbook</span>** class](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Workbook.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Workbook.html") and add a new worksheet. **Note**: ‘Document Solutions for Excel’was previously named _‘_GrapeCity Documents for Excel_’;_ at this time, the older product name remains in areas of our API.

```
    using GrapeCity.Documents.Excel;

    // Create DsExcel workbook
    Workbook workbook = new Workbook();
```

Open the existing “MonthlyClass6thAttendance.xlsx_”_ file in the .NET Excel workbook using the [**Open** method](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Workbook~Open.html).

```
    using GrapeCity.Documents.Excel;

    // Create DsExcel workbook
    Workbook workbook = new Workbook();

    // Open an Excel file
    workbook.Open("MonthlyClass6thAttendance.xlsx");
```

### <a id="set"></a>Set PrintTailColumns & PrintTialRows Properties

Fetch the worksheet, and set the appropriate [**<span>PrintTailColumns</span>**](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup~PrintTailColumns.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup~PrintTailColumns.html")& [**PrintTailRows** ](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup~PrintTailRows.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup~PrintTailRows.html")properties along with some additional properties.

```
    using GrapeCity.Documents.Excel;

    // Create DsExcel workbook
    Workbook workbook = new Workbook();

    // Open an Excel file
    workbook.Open("MonthlyClass6thAttendance.xlsx");

    // Fetch default worksheet 
    IWorksheet worksheet = workbook.Worksheets[0];

    // Columns to be repeated on the left side of each page, while saving pdf
    worksheet.PageSetup.PrintTitleColumns = "$A:$B";
    // Rows to be repeated on the top of each page, while saving pdf
    worksheet.PageSetup.PrintTitleRows = "$1:$6";
    // Columns to be repeated on the right side of each page, while saving pdf
    worksheet.PageSetup.PrintTailColumns = "$AH:$AK";
    // Rows to be repeated on the bottom of each page, while saving pdf
    worksheet.PageSetup.PrintTailRows = "$47:$47";
```

### <a id="export"></a>Export Excel Report to PDF

Save the DsExcel workbook as a PDF to observe the repeating headers and footers on the multi-paged file.

```
    // Saving workbook to pdf
    workbook.Save(@"MonthyClass6thAttendance.pdf");
```

![Excel Headers/Footers C#](//cdn.mescius.io/umb/media/q0eplcm1/05-excelheaders.png?rmode=max&width=752&height=376)

[Download the sample app](https://cdn.mescius.io/assets/developer/blogs/gcdocuments/20240206-How-To-Add-Repeated-Headers-and-Footers-to-Excel/Excel_Repeating_Rows_Columns.zip) for this example and try this for yourself!

<div class="MsoNormal">

* * *

</div>

**Learn More About this .NET Excel API**

This article only scratches the surface of the full capabilities of [<span>Document Solutions for Excel (DsExcel), .NET Edition</span>](https://developer.mescius.com/document-solutions/dot-net-excel-api "https://developer.mescius.com/document-solutions/dot-net-excel-api"), our server-side .NET/C# Excel API. Review our [documentation](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html") to see the many available features, and check out our [online demo explorer](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/ "https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/") to see the features in action and interact with the sample code. To learn more about DsExcel .NET and the new features added in the latest release, check out our [releases page](https://developer.mescius.com/document-solutions/dot-net-excel-api/releases "https://developer.mescius.com/document-solutions/dot-net-excel-api/releases").