Hiding and Showing Rows and Columns with C++
Controlling the Visibility of Rows and Columns
Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection that allows developers to access each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection that represents all cells in the worksheet. The Cells collection provides several methods for managing rows or columns in a worksheet. A few of these are discussed below.
Hiding Rows and Columns
Developers can hide a row or column by calling the HideRow and HideColumn methods of the Cells collection respectively. Both methods take the row and column index as a parameter to hide the specific row or column.
#include <iostream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Hide the 3rd row of the worksheet
worksheet.GetCells().HideRow(2);
// Hide the 2nd column of the worksheet
worksheet.GetCells().HideColumn(1);
// Save the modified Excel file
U16String outputFilePath = outDir + u"output.out.xls";
workbook.Save(outputFilePath);
std::cout << "Rows and columns hidden successfully. File saved to: " << outputFilePath.ToUtf8() << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
Showing Rows and Columns
Developers can show any hidden row or column by calling the UnhideRow and UnhideColumn methods of the Cells collection respectively. Both methods take two parameters:
- Row or column index - the index of a row or column that is used to show the specific row or column.
- Row height or column width - the row height or column width assigned to the row or column after unhiding.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Create workbook from file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Unhide the 3rd row and set its height to 13.5
worksheet.GetCells().UnhideRow(2, 13.5);
// Unhide the 2nd column and set its width to 8.5
worksheet.GetCells().UnhideColumn(1, 8.5);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xls";
workbook.Save(outputFilePath);
Aspose::Cells::Cleanup();
std::cout << "File modified and saved successfully!" << std::endl;
return 0;
}
worksheet.Cells.UnhideColumn(5, -1)
Hiding Multiple Rows and Columns
Developers can hide multiple rows or columns at once by calling the HideRows and HideColumns methods of the Cells collection respectively. Both methods take the starting row or column index and the number of rows or columns that should be hidden as parameters.
#include <iostream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Hide 3, 4, and 5 rows in the worksheet (zero-based index)
worksheet.GetCells().HideRows(2, 3);
// Hide 2 and 3 columns in the worksheet (zero-based index)
worksheet.GetCells().HideColumns(1, 2);
// Save the modified Excel file
workbook.Save(outDir + u"outputxls");
std::cout << "Rows and columns hidden successfully!" << std::endl;
Aspose::Cells::Cleanup();
}