显示和隐藏工作表和选项卡

显示和隐藏工作表

Excel文件可以包含一个或多个工作表。每当我们创建一个Excel文件时,都会向其中添加工作表。Excel文件中的每个工作表都是独立的,具有自己的数据和格式设置等。有时,开发人员可能需要将一些工作表隐藏,并使其他工作表对他们的兴趣可见。因此,Aspose.Cells允许开发人员控制其Excel文件中工作表的可见性。

Aspose.Cells 提供了代表 Excel 文件的一个类,WorkbookWorkbook 类包含一个 Worksheets 集合,允许访问 Excel 文件中的每个工作表。

工作表由 Worksheet 类表示。Worksheet 类提供了广泛的属性和方法来管理工作表。要控制工作表的可见性,请使用 Worksheet 类的 IsVisible 属性。IsVisible 是一个布尔属性,这意味着它只能存储 truefalse 值。

使工作表可见

通过将 Worksheet 类的 IsVisible 属性设置为 true 来使工作表可见

隐藏工作表

通过将Worksheet类的IsVisible属性设置为false来隐藏工作表。

// 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 with 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 first worksheet of the Excel file
worksheet.IsVisible = false;
// Shows first worksheet of the Excel file
//Worksheet.IsVisible = true;
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

显示和隐藏标签

如果您仔细查看Microsoft Excel文件的底部,您会看到许多控件。其中包括:

  • 工作表标签。
  • 选项卡滚动按钮。

工作表标签代表Excel文件中的工作表。单击任何选项卡以切换到该工作表。工作簿中有更多的工作表,也会有更多的工作表标签。如果Excel文件有大量工作表,您需要按钮来进行导航。因此,Microsoft Excel提供了选项卡滚动按钮来滚动工作表标签。

使用Aspose.Cells,开发人员可以控制Excel文件中工作表标签和选项卡滚动按钮的可见性。

Aspose.Cells提供了一个代表Excel文件的类WorkbookWorkbook类提供了各种属性和方法来管理Excel文件。要控制Excel文件中标签的可见性,开发人员可以使用Workbook类的WorkbookSettings.ShowTabs属性。WorkbookSettings.ShowTabs是一个布尔属性,这意味着它只能存储truefalse值。

使选项卡可见

使用Workbook类的WorkbookSettings.ShowTabs属性将标签设置为true来使标签可见。

隐藏选项卡

通过将Workbook类的WorkbookSettings.ShowTabs属性设置为false来隐藏Excel文件中的选项卡。

下面是一个完整的示例,打开一个Excel文件(book1.xls),隐藏其标签并将修改后的文件保存为output.xls。代码执行后,您将看到工作簿的标签被隐藏了。

// 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);
// Opening the Excel file
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Hiding the tabs of the Excel file
workbook.Settings.ShowTabs = false;
// Shows the tabs of the Excel file
//workbook.Settings.ShowTabs = true;
// Saving the modified Excel file
workbook.Save(dataDir + "output.xls");

控制选项卡栏宽度

// 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);
// Instantiating a Workbook object
// Opening the Excel file
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Hiding the tabs of the Excel file
workbook.Settings.ShowTabs = true;
// Adjusting the sheet tab bar width
workbook.Settings.SheetTabBarWidth = 800;
// Saving the modified Excel file
workbook.Save(dataDir + "output.xls");