Скрытие и отображение строк и столбцов

Управление видимостью строк и столбцов

Aspose.Cells предоставляет класс Workbook, который представляет файл Microsoft Excel. Класс Workbook содержит WorksheetCollection, позволяющий разработчикам обращаться к каждому листу в файле Excel. Лист представлен классом Worksheet. Класс Worksheet предоставляет коллекцию Cells, представляющую все ячейки на листе. Коллекция Cells предоставляет несколько методов для управления строками или столбцами на листе. Некоторые из них рассмотрены ниже.

Скрытие строк и столбцов

Разработчики могут скрыть строку или столбец, вызвав методы HideRow и HideColumn соответственно из коллекции Cells. Оба метода принимают индекс строки и столбца в качестве параметра для скрытия конкретной строки или столбца.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Hiding the 3rd row of the worksheet
worksheet.Cells.HideRow(2);
// Hiding the 2nd column of the worksheet
worksheet.Cells.HideColumn(1);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Показ строк и столбцов

Разработчики могут показать любую скрытую строку или столбец, вызвав методы UnhideRow и UnhideColumn из коллекции Cells. Оба метода принимают два параметра:

  • Индекс строки или столбца - индекс строки или столбца, который используется для отображения конкретной строки или столбца.
  • Высота строки или ширина столбца - высота строки или ширина столбца, назначенные строке или столбцу после отображения.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Unhiding the 3rd row and setting its height to 13.5
worksheet.Cells.UnhideRow(2, 13.5);
// Unhiding the 2nd column and setting its width to 8.5
worksheet.Cells.UnhideColumn(1, 8.5);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xls");
// Closing the file stream to free all resources
fstream.Close();

Скрытие нескольких строк и столбцов

Разработчики могут скрыть сразу несколько строк или столбцов, вызвав методы HideRows и HideColumns из коллекции Cells. Оба метода принимают начальный индекс строки или столбца и количество строк или столбцов, которые должны быть скрыты.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Hiding 3,4 and 5 rows in the worksheet
worksheet.Cells.HideRows(2, 3);
// Hiding 2 and 3 columns in the worksheet
worksheet.Cells.HideColumns(1, 2);
// Saving the modified Excel file
workbook.Save(dataDir + "outputxls");
// Closing the file stream to free all resources
fstream.Close();