Microsoft Excelファイルのワークシートを管理します。

Aspose.Cellsは、Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイルの各ワークシートにアクセスできるWorksheetsコレクションが含まれています。

ワークシートはWorksheetクラスで表されます。Worksheetクラスには、ワークシートを管理するための多くのプロパティやメソッドが用意されています。

新しいExcelファイルにワークシートを追加する

プログラムで新しいExcelファイルを作成するには:

  1. Workbookクラスのオブジェクトを作成します。
  2. WorksheetCollectionクラスのAddメソッドを呼び出します。空のワークシートがExcelファイルに自動的に追加されます。新しいワークシートのシートインデックスをWorksheetsコレクションに渡すことで参照できます。
  3. ワークシートの参照を取得します。
  4. ワークシートで作業を実行します。
  5. WorkbookクラスのSaveメソッドを呼び出して、新しいワークシートで新しいExcelファイルを保存します。
// 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);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Setting the name of the newly added worksheet
worksheet.Name = "My Worksheet";
// Saving the Excel file
workbook.Save(dataDir + "output.out.xls");

デザイナースプレッドシートにワークシートを追加する

デザイナースプレッドシートにワークシートを追加するプロセスは、新しいワークシートを追加するプロセスと同じですが、既存のExcelファイルがあるため、ワークシートを追加する前に開く必要があります。デザイナースプレッドシートは、Workbookクラスによって開くことができます。

// 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);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Setting the name of the newly added worksheet
worksheet.Name = "My Worksheet";
// Saving the Excel file
workbook.Save(dataDir + "output.xlsx");

シート名を使用してワークシートにアクセスする

名前またはインデックスを指定して任意のワークシートにアクセスできます。

// 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);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing a worksheet using its sheet name
Worksheet worksheet = workbook.Worksheets["Sheet1"];
Cell cell = worksheet.Cells["A1"];
Console.WriteLine(cell.Value);

シート名を使用してワークシートを削除する

ファイルからワークシートを削除するには、WorksheetCollectionクラスのRemoveAtメソッドを呼び出します。特定のワークシートを削除するには、RemoveAtメソッドにシート名を渡します。

// 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);
// Removing a worksheet using its sheet name
workbook.Worksheets.RemoveAt("Sheet1");
// Save workbook
workbook.Save(dataDir + "output.out.xls");

Sheet Indexを使用してワークシートを削除する

ワークシートの名前がわかっている場合は、ワークシートの名前を使用して削除する方法が適しています。ワークシートの名前を知らない場合は、シート名の代わりにシートのインデックスを取るRemoveAtメソッドのオーバーロードバージョンを使用してください。

// 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);
// Removing a worksheet using its sheet index
workbook.Worksheets.RemoveAt(0);
// Save workbook
workbook.Save(dataDir + "output.out.xls");

シートのアクティブ化およびワークシート内のアクティブセルを作成します

時々、ユーザーがMicrosoft ExcelファイルをExcelで開いたときに特定のワークシートをアクティブで表示する必要があります。同様に、特定のセルをアクティブにし、スクロールバーをアクティブなセルを表示するように設定することがあります。 Aspose.Cellsはこれらのすべてのタスクを実行できます。

アクティブなシートとは、作業中のシートのことです。タブ上のアクティブなシートの名前は、デフォルトで太字になります。

アクティブなセルは選択されたセルであり、タイプを始めるとデータが入力されるセルです。1度に1つのセルがアクティブです。アクティブなセルは太い枠で強調表示されます。

シートのアクティブ化とセルをアクティブにする

Aspose.Cellsには、シートとセルをアクティブにするための特定のAPI呼び出しが用意されています。たとえば、ブック内でアクティブなシートを設定するのにAspose.Cells.WorksheetCollection.ActiveSheetIndexプロパティが役立ちます。 同様に、Aspose.Cells.Worksheet.ActiveCellプロパティはワークシート内のアクティブなセルを設定および取得するために使用されます。

水平または垂直のスクロールバーが特定のデータを表示するために行および列の索引位置にあることを確認するには、Aspose.Cells.Worksheet.FirstVisibleRowおよびAspose.Cells.Worksheet.FirstVisibleColumnプロパティを使用します。

次の例は、ワークシートをアクティブ化し、その中のアクティブなセルにします。生成された出力では、スクロールバーは、2行と2列を最初に表示されるようにスクロールします。

// 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);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.Worksheets[0];
// Get the cells in the worksheet.
Cells cells = worksheet1.Cells;
// Input data into B2 cell.
cells[1, 1].PutValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.Worksheets.ActiveSheetIndex = 0;
// Set B2 cell as an active cell in the worksheet.
worksheet1.ActiveCell = "B2";
// Set the B column as the first visible column in the worksheet.
worksheet1.FirstVisibleColumn = 1;
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.FirstVisibleRow = 1;
// Save the excel file.
workbook.Save(dataDir + "output.xls");

高度なトピック