VSTO および Aspose.Cells でブック内のワークシートを非表示および再表示する

この記事では、C# または Visual Basic を使用して VSTO でワークシートを非表示および再表示することと、C# または Visual Basic を使用して Aspose.Cells で同じタスクを実行することを比較します。 Aspose.Cells では、Microsoft Excel がインストールされていなくても作業できます。

ワークシートを非表示にする手順は次のとおりです。

  1. ファイルを開きます。
  2. ワークシートを取得します。
  3. ワークシートを非表示にします。
  4. ファイルを保存します。 ワークシートを再度表示するには、非表示のシートの表示をオンに切り替えます。

以下のコード サンプルは、最初にワークシートを非表示にする方法を示しています。最初のサンプルは、C# を使用した Aspose.Cells と比較して、C# を使用した VSTO のプロセスを示しています。

コード サンプルの 2 番目のセットは、VSTO または Aspose.Cells でワークシートを再表示するために使用される行を示しています。

ワークシートを非表示にする

以下は、ブック内のワークシートを非表示にする方法を示す VSTO および Aspose.Cells のコード サンプルです。

VSTO

 //Instantiate the Application object.

Excel.Application excelApp = Application;

//Specify the template Excel file path.

string myPath = "Book1.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

 //Instantiate a new Workbook.

Workbook workbook = new Workbook();

//Specify the template Excel file path.

string myPath = "Book1.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("Book1.xls");

ワークシートの再表示

以下は、VSTO と Aspose.Cells のコード サンプルで、ブック内のワークシートを再表示する方法を示しています。

VSTO

 //Unhide the worksheet.

	objSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;

Aspose.Cells

 //Unhide the worksheet.

objSheet.IsVisible = true;

サンプルコードをダウンロード