C++でExcelファイルの行と列を挿入・削除する
紹介
ワークシートをゼロから作成するか、既存のワークシートで作業する場合、さらなるデータを収容するために追加の行や列を必要とする場合があります。逆に、ワークシート内の特定の位置から行や列を削除する必要がある場合もあります。 これらの要件を満たすために、Aspose.Cellsは以下の非常にシンプルなクラスとメソッドのセットを提供しています。
行と列の管理
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetsコレクションが含まれています。ワークシートはWorksheetクラスによって表されます。Worksheetクラスは、ワークシート内のすべてのセルを表すGetCells()コレクションを提供します。
GetCells()コレクションは、ワークシート内の行と列を管理するためのいくつかのメソッドを提供します。いくつかの例を以下に示します。
行と列の挿入
行の挿入方法
任意の位置に行を挿入するには、GetCells()コレクションのInsertRowメソッドを呼び出します。InsertRowメソッドは、新しい行を挿入する位置のインデックスを取ります。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
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 inputFilePath = srcDir + u"book1.xls";
// Instantiating a Workbook object
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Inserting a row into the worksheet at 3rd position
worksheet.GetCells().InsertRow(2);
// Path of output excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Saving the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Row inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
複数の行を挿入する方法
ワークシートに複数の行を挿入するには、GetCells()コレクションのInsertRowsメソッドを呼び出します。InsertRowsメソッドは2つのパラメータを取ります:
- 行インデックス、新しい行が挿入される行のインデックス。
- 行数、挿入する必要がある行の合計数。
#include <iostream>
#include <fstream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Create workbook from file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Insert 10 rows into the worksheet starting from 3rd row
worksheet.GetCells().InsertRows(2, 10);
// Path of output excel file
U16String outputFilePath = srcDir + u"output.out.xls";
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Rows inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
書式付きで行を挿入する方法
書式設定オプション付きの行を挿入するには、引数にInsertOptionsを取るInsertRowsのオーバーロードを使用します。InsertOptionsクラスのCopyFormatTypeプロパティをCopyFormatType列挙型で設定します。CopyFormatType列挙型には以下の3つのメンバーがあります。
- SameAsAbove: 上の行と同じフォーマット。
- SameAsBelow: 下の行と同じフォーマット。
- Clear: 書式をクリアします。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
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 inputFilePath = srcDir + u"book1.xls";
// Path of output Excel file
U16String outputFilePath = outDir + u"InsertingARowWithFormatting_out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Setting Formatting options
InsertOptions insertOptions;
insertOptions.SetCopyFormatType(CopyFormatType::SameAsAbove);
// Inserting a row into the worksheet at 3rd position
worksheet.GetCells().InsertRows(2, 1, insertOptions);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Row inserted successfully with formatting!" << std::endl;
Aspose::Cells::Cleanup();
}
列の挿入方法
開発者は、任意の場所に列を挿入することもできます。GetCells()コレクションのInsertColumnメソッドを呼び出します。InsertColumnメソッドは、挿入する列のインデックスを受け取ります。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
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 inputFilePath = srcDir + u"book1.xls";
// Path of output Excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Insert a column into the worksheet at 2nd position
worksheet.GetCells().InsertColumn(1);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Column inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
行と列の削除
複数の行を削除する方法
複数行を削除するには、GetCells()コレクションのDeleteRowsメソッドを呼び出します。DeleteRowsメソッドは2つのパラメータを取ります:
- 行インデックス、削除される行のインデックス。
- 行数、削除する必要がある行の合計数。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Delete 10 rows from the worksheet starting from 3rd row
worksheet.GetCells().DeleteRows(2, 10);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xlsx";
workbook.Save(outputFilePath);
std::cout << "Rows deleted successfully and file saved!" << std::endl;
Aspose::Cells::Cleanup();
}
列を削除する方法
任意の場所で列を削除するには、GetCells()コレクションのDeleteColumnメソッドを呼び出します。DeleteColumnメソッドは削除する列のインデックスを受け取ります。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook from file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Delete a column from the worksheet at 5th position (index 4)
worksheet.GetCells().DeleteColumn(4);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xlsx";
workbook.Save(outputFilePath);
std::cout << "Column deleted successfully and file saved!" << std::endl;
Aspose::Cells::Cleanup();
}