C++: Hide or Show Rows and Columns in Excel

Sometimes, certain rows and columns of an Excel file may contain sensitive or confidential information. Before presenting the file to others, it is critical to hide these specific rows and columns to prevent unauthorized people from viewing this information. After the representation, you can show the hidden rows and columns to redisplay the information as needed. In this article, we will demonstrate how to hide or show rows and columns in Excel in C++ using Spire.XLS for 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

Hide Specific Rows and Columns in Excel in C++

You can hide specific rows and columns in an Excel worksheet by using the XlsWorksheet->HideRow(int rowIndex) and XlsWorksheet->HideColumn(int columnIndex) methods. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get(int index) method.
  • Hide specific rows in the worksheet using XlsWorksheet->HideRow(int rowIndex) method.
  • Hide Specific columns in the worksheet using XlsWorksheet->HideColumn(int columnIndex) method.
  • Save the result file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the workbook class
	Workbook* workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"Input.xlsx");

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Hide the 3rd and the 7th rows
	sheet->HideRow(3);
	sheet->HideRow(7);

	//Hide the 3rd and the 6th columns
	sheet->HideColumn(3);
	sheet->HideColumn(6);	

	//Save the result file
	workbook->SaveToFile(L"HideRowsAndColumns.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
	delete workbook;
}

C++: Hide or Show Rows and Columns in Excel

Show Specific Hidden Rows and Columns in Excel in C++

To show specific hidden rows and columns in an Excel worksheet, you can use the XlsWorksheet->ShowRow(int rowIndex) and XlsWorksheet->ShowColumn(int columnIndex) methods. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get(int index) method.
  • Show specific hidden rows in the worksheet using XlsWorksheet->ShowRow(int rowIndex) method.
  • Show specific hidden columns in the worksheet using XlsWorksheet->ShowColumn(int columnIndex) method.
  • Save the result file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the workbook class
	Workbook* workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"HideRowsAndColumns.xlsx");

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Show the 3rd and the 7th rows
	sheet->ShowRow(3);
	sheet->ShowRow(7);

	//Show the 3rd and the 6th columns
	sheet->ShowColumn(3);
	sheet->ShowColumn(6);

	//Save the result file
	workbook->SaveToFile(L"ShowRowsAndColumns.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
	delete workbook;
}

C++: Hide or Show Rows and Columns in Excel

Hide Multiple Rows and Columns at Once in Excel in C++

You can hide multiple adjacent rows and columns at once by using the XlsWorksheet->HideRows(int rowIndex, int rowCount) and XlsWorksheet->HideColumns(int columnIndex, int columnCount) methods. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get(int index) method.
  • Hide multiple adjacent rows in the worksheet at once using XlsWorksheet->HideRows(int rowIndex, int rowCount) method.
  • Hide multiple adjacent columns in the worksheet at once using XlsWorksheet->HideColumns(int columnIndex, int columnCount) method.
  • Save the result file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the workbook class
	Workbook* workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"Input.xlsx");

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Hide the 3rd, 4th and 5th rows
	sheet->HideRows(3, 3);

	//Hide the 5th, 6th and 7th columns
	sheet->HideColumns(5, 3);

	//Save the result file
	workbook->SaveToFile(L"HideMultipleRowsAndColumns.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
	delete workbook;
}

C++: Hide or Show Rows and Columns in Excel

Show All Hidden Rows and Columns in Excel in C++

To show all the hidden rows and columns in an Excel worksheet, you need to iterate through all the rows and columns in the worksheet, then find the hidden rows and columns and call the XlsWorksheet->ShowRow(int rowIndex) and XlsWorksheet->ShowColumn(int columnIndex) methods to redisplay them. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get(int index) method.
  • Iterate through all the rows in the worksheet, then find the hidden rows using XlsWorksheet->GetRowIsHide(int rowIndex) method.
  • Show the hidden rows using XlsWorksheet->ShowRow(int rowIndex) method.
  • Iterate through all the columns in the worksheet, then find the hidden columns using XlsWorksheet->GetColumnIsHide(int columnIndex) method.
  • Show the hidden columns using XlsWorksheet->ShowColumn(int columnIndex) method.
  • Save the result file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the workbook class
	Workbook* workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"HideMultipleRowsAndColumns.xlsx");

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Iterate through all the rows in the worksheet
	for (int i = 1; i <= sheet->GetLastRow(); i++)
	{
		//Check if the current row is hidden
		if (sheet->GetRowIsHide(i))
		{
			sheet->ShowRow(i);
		}
	}

	//Iterate through all the columns in the worksheet
	for (int j = 1; j <= sheet->GetLastColumn(); j++)
	{
		//Check if the current column is hidden
		if (sheet->GetColumnIsHide(j))
		{
			//Show the hidden column
			sheet->ShowColumn(j);
		}
	}

	//Save the result file
	workbook->SaveToFile(L"ShowAllHiddenRowsAndColumns.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
	delete workbook;
}

C++: Hide or Show Rows and Columns in Excel

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.