When viewing data in a large excel worksheet, you may often lose track of the header rows or columns when scrolling to another part of the worksheet. Under the circumstances, MS Excel provides the "Freeze Panes" function to help you lock the necessary rows or/and columns to keep them visible all the time. In this article, you will learn how to programmatically freeze rows or/and columns in an Excel worksheet using Spire.XLS for C++.
Spire.XLS for C++ provides the Worksheet->FreezePanes(int rowIndex, int columnIndex) method to freeze all rows and columns above and left of the selected cell which is specified by the rowIndex and the columnIndex.
This tutorial provides the code examples for the following cases:
- Freeze the Top Row in Excel in C++
- Freeze the First Column in Excel in C++
- Freeze the First Row and First Column in Excel in C++
Install Spire.XLS for C++
There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.XLS for C++ in a C++ Application
Freeze the Top Row in Excel in C++
To freeze the top row, the selected cell should be the cell (2, 1) – "A2". The following are the detailed steps.
- Create a Workbook object.
- Load an Excel document using Workbook->LoadFromFile() method.
- Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
- Freeze the top row using Worksheet->FreezePanes(2, 1) method.
- Save the workbook to another Excel file using Workbook->SaveToFile() method.
- C++
#include "Spire.Xls.o.h"; using namespace Spire::Xls; int main() { //Specify input file path and name std::wstring data_path = L"Data\\"; std::wstring inputFile = data_path + L"sample.xlsx"; //Specify output file path and name std::wstring outputPath = L"Output\\"; std::wstring outputFile = outputPath + L"FreezeFirstRowAndColumn.xlsx"; //Create a Workbook object Workbook* workbook = new Workbook(); //Load the Excel document from disk workbook->LoadFromFile(inputFile.c_str()); //Get the first worksheet Worksheet* sheet = workbook->GetWorksheets()->Get(0); //Freeze top row sheet->FreezePanes(2, 1); //Save to file workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013); workbook->Dispose(); }
Freeze the First Column in Excel in C++
To freeze the first column, the selected cell should be the cell (1, 2) – "B1". The following are the steps to freeze the first column in an Excel worksheet.
- Create a Workbook object.
- Load an Excel document using Workbook->LoadFromFile() method.
- Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
- Freeze the first column using Worksheet->FreezePanes(1, 2) method.
- Save the workbook to another Excel file using Workbook->SaveToFile() method.
- C++
#include "Spire.Xls.o.h"; using namespace Spire::Xls; int main() { //Specify input file path and name std::wstring data_path = L"Data\\"; std::wstring inputFile = data_path + L"sample.xlsx"; //Specify output file path and name std::wstring outputPath = L"Output\\"; std::wstring outputFile = outputPath + L"FreezeFirstRowAndColumn.xlsx"; //Create a workbook Workbook* workbook = new Workbook(); //Load the Excel document from disk workbook->LoadFromFile(inputFile.c_str()); //Get the first worksheet Worksheet* sheet = workbook->GetWorksheets()->Get(0); //Freeze first column sheet->FreezePanes(1, 2); //Save to file workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013); workbook->Dispose(); }
Freeze the First Row and First Column in Excel in C++
If you want to freeze the top row and the first column at the same time, the selected cell should be the cell (2, 2) – "B2". The following are the detailed steps.
- Create a Workbook object.
- Load an Excel document using Workbook->LoadFromFile() method.
- Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
- Freeze the top row and first column using Worksheet->FreezePanes(1, 2) method.
- Save the workbook to another Excel file using Workbook->SaveToFile() method.
- C++
#include "Spire.Xls.o.h"; using namespace Spire::Xls; int main() { //Specify input file path and name std::wstring data_path = L"Data\\"; std::wstring inputFile = data_path + L"sample.xlsx"; //Specify output file path and name std::wstring outputPath = L"Output\\"; std::wstring outputFile = outputPath + L"FreezeFirstRowAndColumn.xlsx"; //Create a workbook Workbook* workbook = new Workbook(); //Load the Excel document from disk workbook->LoadFromFile(inputFile.c_str()); //Get the first worksheet Worksheet* sheet = workbook->GetWorksheets()->Get(0); //Freeze top row and first column sheet->FreezePanes(2, 2); //Save to file workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013); workbook->Dispose(); }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.