隐藏和取消隐藏工作簿中的工作表

隐藏和取消隐藏工作表

本文比较隐藏取消隐藏工作表与VSTO 使用 C# 或 Visual Basic 来执行相同的任务Aspose.Cells再次使用 C# 或 Visual Basic。 Aspose.Cells 让您无需安装 Microsoft Excel 即可工作。

隐藏工作表的步骤是:

  1. 打开一个文件。
  2. 获取工作表。
  3. 隐藏工作表。
  4. 保存文件。

取消隐藏再次打开工作表,只需打开隐藏工作表的可见性即可。

下面的代码示例首先展示了如何隐藏工作表。第一个样本显示了这个过程VSTO,与使用 C# 或 Visual Basic 相比Aspose.Cells再次使用 C# 或 Visual Basics。

第二组代码示例显示了用于取消隐藏工作表的行VSTO要么Aspose.Cells.

隐藏工作表

下面是 VSTO 和 Aspose.Cells 的代码示例,说明如何在工作簿中隐藏工作表。

使用 VSTO 隐藏工作表

C#

.......



using Microsoft.VisualStudio.Tools.Applications.Runtime;

using Excel = Microsoft.Office.Interop.Excel;

using Office = Microsoft.Office.Core;

using System.Reflection;

.......



//Instantiate the Application object.

Excel.Application excelApp = new Excel.ApplicationClass();



//Specify the template Excel file path.

string myPath=@"d:\test\MyBook.xls";



//Open the Excel file.

excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value,

Missing.Value, Missing.Value);



//Get the first sheet.

Excel.Worksheet objSheet = (Excel.Worksheet)excelApp.ActiveWorkbook.Sheets["Sheet1"];

//Hide the worksheet.

objSheet.Visible = Excel.XlSheetVisibility.xlSheetHidden

//Save As the Excel file.

excelApp.ActiveWorkbook.Save();

//Quit the Application.

excelApp.Quit();

使用 Aspose.Cells for .NET 隐藏工作表

C#

.......



using Aspose.Cells;



.......



//Instantiate a new Workbook.

Workbook workbook = new Workbook();

//Specify the template Excel file path.

string myPath = @"d:\test\MyBook.xls";

//Open the Excel file.

workbook.Open(myPath);

//Get the first sheet.

Aspose.Cells.Worksheet objSheet = workbook.Worksheets["Sheet1"];

//Hide the worksheet.

objSheet.IsVisible = false;

//Save As the Excel file.

workbook.Save(@"d:\test\MyBook.xls");

取消隐藏工作表

下面是 VSTO 和 Aspose.Cells 的代码示例,说明如何取消隐藏工作簿中的工作表。

使用 VSTO 取消隐藏工作表

C#

//Unhide the worksheet.

objSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;

使用 Aspose.Cells for .NET 取消隐藏工作表

C#

//Unhide the worksheet.

objSheet.IsVisible = true;