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>
int main() {
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);
pm.setBottom(1.0);
pm.setLeft(0.75);
pm.setRight(0.75);
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);
po.setHeadings(true);
po.setHorizontalCentered(true);
po.setVerticalCentered(false);
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();
hf.setDifferentFirst(true);
hf.setFirstHeader("&C&\"Arial,Bold\"&24 CONFIDENTIAL REPORT");
hf.setFirstFooter("&C&12 Restricted Distribution");
hf.setOddHeader("&L&A &CHidden Corp &R&D &T");
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.
wks.setPrintTitleRows(1, 1);
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.
wks.insertRowBreak(25);
wks.insertColBreak(3);
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.