Introduction
When working with large datasets, it is often necessary to keep specific rows (like headers) or columns (like identifiers) visible while the user scrolls through the rest of the worksheet. OpenXLSX provides an elegant and straightforward API to handle this layout feature.
This tutorial covers:
- Freezing the top row or the first column.
- Freezing both rows and columns simultaneously.
- Splitting the window into independent scrollable panes.
- Removing all pane configurations from a worksheet.
1. Freezing the Top Row
To freeze the top row (Row 1), you specify the cell immediately below the row you want to freeze. Thus, you tell OpenXLSX to freeze panes starting at A2.
#include <OpenXLSX.hpp>
int main() {
wks.cell("B1").value() = "Sales";
wks.cell("A2").value() = "2026-01-01";
wks.cell("B2").value() = 1500;
wks.freezePanes("A2");
XLCellValueProxy & value()
Retrieves the mutable value proxy for the cell.
Definition XLCell.cpp:253
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
XLWorksheet worksheet(std::string_view sheetName)
Get the worksheet with the given name.
Definition XLWorkbook.cpp:205
XLCellAssignable cell(const std::string &ref) const
Definition XLWorksheet.cpp:168
Definition IXLCellProvider.hpp:24
constexpr const bool XLForceOverwrite
Definition XLDocument.hpp:60
2. Freezing the First Column
Similarly, to freeze a column, you specify the cell immediately to the right of the column you wish to freeze. To freeze Column A, you tell the worksheet to freeze starting at B1.
wksCol.cell("B1").value() = "Data 1";
wksCol.cell("C1").value() = "Data 2";
wksCol.freezePanes("B1");
void addWorksheet(std::string_view sheetName)
Add a new worksheet.
Definition XLWorkbook.cpp:258
3. Freezing Both Rows and Columns
You can freeze both rows and columns simultaneously by selecting a cell that is not in row 1 and not in column A.
For example, to freeze the first two rows (Row 1 and 2) AND the first two columns (Column A and B), you choose cell C3.
void freezePanes(uint16_t column, uint32_t row)
Definition XLWorksheet_View.cpp:126
4. Splitting Panes
While "Freezing" locks the headers in place, "Splitting" divides the Excel window into up to four independent, scrollable viewports.
To split panes, you use splitPanes(), providing the X and Y distance (in twips, where 1/20th of a point = 1 twip). You can optionally specify the cell that should appear at the top-left of the bottom-right pane, and which pane should be the active (selected) one.
void splitPanes(double xSplit, double ySplit, std::string_view topLeftCell="", XLPane activePane=XLPane::BottomRight)
Definition XLWorksheet_View.cpp:162
@ BottomRight
Definition XLSheetBase.hpp:45
5. Clearing Panes
If you need to programmatically remove any freezing or splitting from a worksheet, you can use the clearPanes() method.
Summary
Finally, save and close your document.
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
The resulting PanesDemo.xlsx file will correctly lock your headers or split your data windows exactly as defined, making large datasets much easier for your end-users to navigate!