ワークシートの管理
Aspose.Cellsは、Excelファイルを表すWorkbook クラスを提供しており、Workbook クラスにはExcelファイル内の各ワークシートにアクセスできるWorksheets コレクションが含まれています。
ワークシートは、Worksheet クラスで表されます。Worksheet クラスには、ワークシートを管理するための幅広いメソッドが提供されています。
新しいExcelファイルにワークシートを追加する
プログラムで新しいExcelファイルを作成するには:
- Worksheet クラスのオブジェクトを作成します。
- WorksheetCollection コレクションのAdd メソッドを呼び出します。空のワークシートがExcelファイルに自動的に追加されます。新しいワークシートのシートインデックスをWorksheetCollection コレクションに渡すことで参照できます。
- ワークシートの参照を取得します。
- ワークシートで作業を実行します。
- 新しいワークシートを含む新しいExcelファイルをWorkbook クラスの Save メソッドを呼び出して保存します。
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
//Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
//Path of output excel file | |
U16String outputManageWorksheets = outDir + u"outputManageWorksheets.xlsx"; | |
//Create workbook | |
Workbook workbook; | |
// Adding a new worksheet to the Workbook object | |
int i = workbook.GetWorksheets().Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.GetWorksheets().Get(i); | |
// Setting the name of the newly added worksheet | |
worksheet.SetName(u"My Worksheet"); | |
// Save the Excel file. | |
workbook.Save(outputManageWorksheets); | |
std::cout <<"New worksheet added successfully with in a workbook!" << std::endl; | |
Aspose::Cells::Cleanup(); |
Sheet Indexを使用してワークシートにアクセスする
次のサンプルコードは、インデックスを指定して任意のワークシートにアクセスまたは取得する方法を示しています。
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
//Source directory path | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
//Path of input excel file | |
U16String sampleManageWorksheets = srcDir + u"sampleManageWorksheets.xlsx"; | |
//Load the sample Excel file | |
Workbook workbook(sampleManageWorksheets); | |
//Accessing a worksheet using its index | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Access the cell by its name. | |
Cell cell = worksheet.GetCells().Get(u"F7"); | |
//Print the value of cell F7 | |
U16String val = cell.GetStringValue(); | |
//Print the value on console. | |
std::cout << "Value of cell F7: " << val.ToUtf8() << std::endl; | |
Aspose::Cells::Cleanup(); |
Sheet Indexを使用してワークシートを削除する
ワークシートの名前がわかっている場合は、ワークシートの名前を使用してワークシートを削除することができます。ワークシートの名前がわからない場合は、ワークシートの名前の代わりにワークシートのシートインデックスを取るRemoveAt メソッドのオーバーロードバージョンを使用します。
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
//Source directory path | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
//Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
//Path of input excel file | |
U16String sampleManageWorksheets = srcDir + u"sampleManageWorksheets.xlsx"; | |
//Path of output excel file | |
U16String outputManageWorksheets = outDir + u"outputManageWorksheets.xlsx"; | |
//Load the sample Excel file | |
Workbook workbook(sampleManageWorksheets); | |
//Removing a worksheet using its sheet index | |
workbook.GetWorksheets().RemoveAt(0); | |
//Save the Excel file. | |
workbook.Save(outputManageWorksheets); | |
Aspose::Cells::Cleanup(); |