OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
Quick Start: Page Layout & Printing

Introduction

OpenXLSX provides comprehensive support for configuring how a worksheet should be printed. You can define page orientation, paper size, margins, headers and footers, print areas, repeated titles, and manual page breaks.

This tutorial covers:

  • Configuring basic Page Setup (Orientation, Paper Size, Scaling).
  • Adjusting Page Margins.
  • Setting up Print Options (Gridlines, Headings, Centering).
  • Creating custom Headers and Footers with dynamic Excel codes.
  • Defining Print Areas and repeating title rows/columns.
  • Inserting manual Page Breaks.

1. Page Setup Configuration

The XLPageSetup object handles the core physical properties of the printed page.

#include <OpenXLSX.hpp>
using namespace OpenXLSX;
int main() {
doc.create("PageLayoutDemo.xlsx", XLForceOverwrite);
auto wks = doc.workbook().worksheet("Sheet1");
auto ps = wks.pageSetup();
// Set to Landscape orientation
// Set paper size to A4 (Internal Excel ID for A4 is 9)
ps.setPaperSize(9);
// Force the printed output to fit within 1 page wide, but as many pages tall as needed
ps.setFitToHeight(0); // 0 indicates automatic scaling for height
// Define the order in which pages are numbered and printed
ps.setPageOrder("downThenOver");
// Start page numbering from 1
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:97
void create(std::string_view fileName, bool forceOverwrite=XLForceOverwrite)
Initialize a new .xlsx package from a built-in template.
Definition XLDocument.cpp:372
XLWorkbook workbook() const
Get the underlying workbook object, containing worksheets and global data state. [[nodiscard]] preven...
Definition XLDocument.cpp:828
XLPageSetup & setUseFirstPageNumber(bool value)
Definition XLPageSetup.cpp:157
XLPageSetup & setPageOrder(std::string_view value)
Definition XLPageSetup.cpp:149
XLPageSetup & setOrientation(XLPageOrientation value)
Definition XLPageSetup.cpp:112
XLPageSetup & setPaperSize(uint32_t value)
Definition XLPageSetup.cpp:97
XLPageSetup & setFitToHeight(uint32_t value)
Definition XLPageSetup.cpp:141
XLPageSetup & setFitToWidth(uint32_t value)
Definition XLPageSetup.cpp:133
XLPageSetup & setFirstPageNumber(uint32_t value)
Definition XLPageSetup.cpp:165
XLWorksheet worksheet(std::string_view sheetName)
Get the worksheet with the given name.
Definition XLWorkbook.cpp:205
XLPageSetup pageSetup() const
Definition XLWorksheet_View.cpp:54
Definition IXLCellProvider.hpp:24
constexpr const bool XLForceOverwrite
Definition XLDocument.hpp:60
@ Landscape
Definition XLPageSetup.hpp:24

2. Page Margins

The XLPageMargins object allows you to fine-tune the empty space around your content. Values are provided in inches.

auto pm = wks.pageMargins();
pm.setTop(1.5); // 1.5 inches from the top edge
pm.setBottom(1.0); // 1.0 inch from the bottom edge
pm.setLeft(0.75);
pm.setRight(0.75);
// Space allocated specifically for the header and footer text
pm.setHeader(0.5);
pm.setFooter(0.5);

3. Print Options

The XLPrintOptions object controls visual aids that appear on the printed page, such as gridlines and headings.

auto po = wks.printOptions();
po.setGridLines(true); // Print the cell gridlines
po.setHeadings(true); // Print the row/column headings (A, B, C... 1, 2, 3...)
po.setHorizontalCentered(true); // Center the content horizontally on the paper
po.setVerticalCentered(false); // Align content to the top vertically

4. Headers and Footers

Headers and footers can be heavily customized using the XLHeaderFooter object. Excel uses special formatting codes (e.g., &L, &C, &R) to define alignment, and placeholders (e.g., &P, &N) for dynamic data like page numbers.

auto hf = wks.headerFooter();
// Enable a unique header/footer for the very first printed page
hf.setDifferentFirst(true);
// First Page Header: Centered (&C), Arial font, Bold, Size 24
hf.setFirstHeader("&C&\"Arial,Bold\"&24 CONFIDENTIAL REPORT");
// First Page Footer: Centered (&C), Size 12
hf.setFirstFooter("&C&12 Restricted Distribution");
// Standard Header (used for page 2 onwards):
// Left: Tab Name (&A) | Center: Static Text | Right: Date (&D) and Time (&T)
hf.setOddHeader("&L&A &CHidden Corp &R&D &T");
// Standard Footer: Centered Page Number (&P) of Total Pages (&N)
hf.setOddFooter("&CPage &P of &N");

5. Print Titles and Print Area

You often want specific rows (like a table header) to repeat at the top of every printed page. You may also want to restrict printing to a specific range of cells.

// Repeat Row 1 at the top of every printed page
wks.setPrintTitleRows(1, 1);
// Repeat Columns A and B at the left of every printed page
// wks.setPrintTitleCols(1, 2);
// Restrict the printable area to columns A through F, rows 1 through 50
wks.setPrintArea("A1:F50");

6. Manual Page Breaks

If you need absolute control over where a page ends, you can insert manual row and column breaks.

// Force a horizontal page break immediately after row 25
wks.insertRowBreak(25);
// Force a vertical page break immediately after column 3 (Column C)
wks.insertColBreak(3);
doc.save();
doc.close();
return 0;
}
void close()
Close the document and release underlying ZIP resources, ensuring file handles are closed properly.
Definition XLDocument.cpp:412
void save()
Save changes to the original file path.
Definition XLDocument.cpp:460

By combining these layout properties, you can ensure that your generated Excel reports look exactly as intended when exported to PDF or sent to a physical printer.