隐藏和显示行和列

介绍

有时,用户可能需要隐藏工作表的某些行或列,然后稍后再显示它们。Microsoft Excel提供了这个功能,Aspose.Cells也提供了。

控制行和列的可见性

Aspose.Cells提供了一个类,Workbook,表示Microsoft Excel文件。Workbook类包含一个WorksheetCollection允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了一个Cells集合,表示工作表中的所有单元格。Cells集合提供了几种管理工作表中的行或列的方法,以下是其中的一些。

隐藏行或列

开发人员可以通过调用Cells集合的HideRowHideColumn方法来隐藏行或列。这两种方法分别接受行/列索引作为参数,以隐藏特定的行或列。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(HidingRowsandColumns.class) + "rows_cloumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Hiding the 3rd row of the worksheet
cells.hideRow(2);
// Hiding the 2nd column of the worksheet
cells.hideColumn(1);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "HidingRowsandColumns_out.xls");
// Print message
System.out.println("Rows and Columns hidden successfully.");

显示行和列

开发人员可以通过调用Cells集合的UnhideRowUnhideColumn方法,来取消隐藏任何隐藏的行或列。这两种方法各自接受两个参数:

  • 行或列索引 - 用于显示特定行或列的索引。
  • 行高或列宽 - 在显示后分配给行或列的行高或列宽。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(UnhidingRowsandColumns.class) + "rows_cloumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Unhiding the 3rd row and setting its height to 13.5
cells.unhideRow(2, 13.5);
// Unhiding the 2nd column and setting its width to 8.5
cells.unhideColumn(1, 8.5);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "UnhidingRowsandColumns_out.xls");
// Print message
System.out.println("Rows and Columns unhidden successfully.");