行と列の挿入、削除

序章

新しいワークシートをゼロから作成する場合でも、既存のワークシートで作業する場合でも、より多くのデータを収容するために余分な行または列を追加する必要がある場合があります。逆に、ワークシートの指定された位置から行または列を削除する必要がある場合もあります。これらの要件を満たすために、Aspose.Cells は、以下で説明する非常に単純なクラスとメソッドのセットを提供します。

行と列の管理

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

Iセルコレクションには、ワークシートの行と列を管理するいくつかのメソッドが用意されています。これらのいくつかを以下で説明します。

行を挿入する

を呼び出して、ワークシートの任意の場所に行を挿入します。行の挿入の方法Iセルコレクション。の行の挿入メソッドは、新しい行が挿入される行のインデックスを取ります。

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input excel file
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx"));
//Path of output excel file
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx"));
//Read input excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
//Inserting a row into the worksheet at 3rd position
worksheet->GetICells()->InsertRow(2);
//Save the Excel file.
workbook->Save(outputInsertingDeletingRowsAndColumns);

複数行の挿入

複数の行をワークシートに挿入するには、InsertRowsの方法Iセルコレクション。のInsertRowsメソッドは次の 2 つのパラメーターを取ります。

  • 行インデックス。新しい行が挿入される行のインデックス。
  • 行数、挿入する必要がある行の総数。
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input excel file
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx"));
//Path of output excel file
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx"));
//Read input excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
//Inserting 10 rows into the worksheet starting from 3rd row
worksheet->GetICells()->InsertRows(2, 10);
//Save the Excel file.
workbook->Save(outputInsertingDeletingRowsAndColumns);

複数行の削除

ワークシートから複数の行を削除するには、行の削除の方法Iセルコレクション。の行の削除メソッドは次の 2 つのパラメーターを取ります。

  • 行インデックス。行が削除される行のインデックス。
  • 行数、削除する必要がある行の総数。
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input excel file
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx"));
//Path of output excel file
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx"));
//Read input excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
//Deleting 10 rows from the worksheet starting from 3rd row
worksheet->GetICells()->DeleteRows(2, 10);
//Save the Excel file.
workbook->Save(outputInsertingDeletingRowsAndColumns);

列を挿入する

開発者は、を呼び出して、ワークシートの任意の場所に列を挿入することもできます挿入列の方法Iセルコレクション。挿入列メソッドは、新しい列が挿入される列のインデックスを取得します。

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input excel file
StringPtr sampleInsertingDeletingRowsAndColumns = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx"));
//Path of output excel file
StringPtr outputInsertingDeletingRowsAndColumns = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx"));
//Read input excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
//Inserting a column into the worksheet at 2nd position
worksheet->GetICells()->InsertColumn(1);
//Save the Excel file.
workbook->Save(outputInsertingDeletingRowsAndColumns);

列を削除する

ワークシートの任意の場所から列を削除するには、列の削除の方法Iセルコレクション。の列の削除メソッドは、削除する列のインデックスを取得します。

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input excel file
StringPtr sampleDeleteColumn = dirPath->StringAppend(new String("sampleInsertingDeletingRowsAndColumns.xlsx"));
//Path of output excel file
StringPtr outputDeleteColumn = outPath->StringAppend(new String("outputInsertingDeletingRowsAndColumns.xlsx"));
//Read input excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleDeleteColumn);
//Accessing the first worksheet in the Excel file
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
//Deleting a column from the worksheet at 2nd position
worksheet->GetICells()->DeleteColumn(4);
//Save the Excel file.
workbook->Save(outputDeleteColumn);