行と列の挿入、削除

紹介

新規ワークシートを作成したり、既存のワークシートで作業したりする際に、データの追加のために行や列を追加する必要があるかもしれません。逆に、ワークシート内の特定の位置から行または列を削除する必要があるかもしれません。これらの要件を満たすために、Aspose.Cellsは、以下で説明する非常にシンプルなクラスとメソッドを提供しています。

行と列の管理

Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供しています。Workbookクラスには、Excelファイル内の各ワークシートにアクセスを許可するWorksheetsコレクションが含まれています。ワークシートはWorksheetクラスによって表されます。Worksheetクラスは、ワークシート内のすべてのセルを表すCellsコレクションを提供します。

Cellsコレクションには、ワークシート内の行や列を管理するためのいくつかのメソッドが提供されています。以下ではその一部を詳しく説明します。

行の挿入

ワークシートに新しい行をどこにでも挿入するには、CellsコレクションのInsertRowメソッドを呼び出します。InsertRowメソッドは、新しい行が挿入される行のインデックスを取ります。

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Inserting a column into the worksheet at 2nd position
worksheet.GetCells().InsertColumn(1);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();

複数の行の挿入

ワークシートに複数の行を挿入するには、CellsコレクションのInsertRowsメソッドを呼び出します。InsertRowsメソッドは2つのパラメータを取ります:

  • 行インデックス、新しい行が挿入される行のインデックス。
  • 行数、挿入する必要がある行の合計数。
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Inserting 10 rows into the worksheet starting from 3rd row
worksheet.GetCells().InsertRows(2, 10);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();

複数の行の削除

ワークシートから複数の行を削除するには、CellsコレクションのDeleteRowsメソッドを呼び出します。DeleteRowsメソッドは2つのパラメータを取ります:

  • 行インデックス、削除される行のインデックス。
  • 行数、削除する必要がある行の合計数。
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Deleting 10 rows from the worksheet starting from 3rd row
worksheet.GetCells().DeleteRows(2, 10);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();

列の挿入

開発者は、任意の位置にワークシートに列を挿入するために、CellsコレクションのInsertColumnメソッドを呼び出すこともできます。InsertColumnメソッドは、新しい列が挿入される列のインデックスを取ります。

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Inserting a column into the worksheet at 2nd position
worksheet.GetCells().InsertColumn(1);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();

列の削除

任意の位置にワークシートから列を削除するには、CellsコレクションのDeleteColumnメソッドを呼び出します。DeleteColumnメソッドは、削除する列のインデックスを取ります。

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx";
//Path of output excel file
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx";
//Read input excel file
Workbook workbook(sampleInsertingDeletingRowsAndColumns);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Deleting a column from the worksheet at 2nd position
worksheet.GetCells().DeleteColumn(1);
//Save the Excel file.
workbook.Save(outputInsertingDeletingRowsAndColumns);
Aspose::Cells::Cleanup();