ワークシートの管理

Aspose.Cells はクラスを提供しますIワークブックこれは Excel ファイルを表します。のIワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。

ワークシートは、Iワークシートクラス。のIワークシートクラスには、ワークシートを管理するためのさまざまなメソッドが用意されています。

新しい Excel ファイルへのワークシートの追加

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

  1. のオブジェクトを作成しますIワークシートクラス。
  2. 電話する追加の方法IWorksheetCollectionコレクション。空のワークシートが Excel ファイルに自動的に追加されます。新しいワークシートのシート インデックスをIWorksheetCollectionコレクション。
  3. ワークシート参照を取得します。
  4. ワークシートで作業を行います。
  5. を呼び出して、新しいワークシートを含む新しい Excel ファイルを保存します。Iワークブッククラスセーブ方法。
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Output directory path
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\");
//Path of output excel file
StringPtr outputManageWorksheets = outDir->StringAppend(new String("outputManageWorksheets.xlsx"));
//Create workbook
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook();
// Adding a new worksheet to the Workbook object
int i = workbook->GetIWorksheets()->Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(i);
// Setting the name of the newly added worksheet
worksheet->SetName(new String("My Worksheet"));
// Save the Excel file.
workbook->Save(outputManageWorksheets);
StringPtr msg = new String("New worksheet added successfully with in a workbook!");
Console::WriteLine(msg);

シート インデックスを使用したワークシートへのアクセス

次のサンプル コードは、インデックスを指定して任意のワークシートにアクセスまたは取得する方法を示しています。

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\");
//Path of input excel file
StringPtr sampleManageWorksheets = srcDir->StringAppend(new String("sampleManageWorksheets.xlsx"));
//Load the sample Excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleManageWorksheets);
//Accessing a worksheet using its index
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
//Access the cell by its name.
intrusive_ptr<ICell> cell = worksheet->GetICells()->GetObjectByIndex(new String("F7"));
//Print the value of cell F7
StringPtr val = cell->GetStringValue();
//Print the value on console.
Console::Write(new String("Value of cell F7: "));
Console::WriteLine(val);

シート インデックスを使用してワークシートを削除する

名前によるワークシートの削除は、ワークシートの名前がわかっている場合にうまく機能します。ワークシートの名前がわからない場合は、オーバーロードされたバージョンの削除場所シート名の代わりにワークシートのシート インデックスを取得するメソッド。

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\");
//Output directory path
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\");
//Path of input excel file
StringPtr sampleManageWorksheets = srcDir->StringAppend(new String("sampleManageWorksheets.xlsx"));
//Path of output excel file
StringPtr outputManageWorksheets = outDir->StringAppend(new String("outputManageWorksheets.xlsx"));
//Load the sample Excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleManageWorksheets);
//Removing a worksheet using its sheet index
workbook->GetIWorksheets()->RemoveAt(0);
//Save the Excel file.
workbook->Save(outputManageWorksheets);