データの追加と取得

セルへのデータの追加

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

Aspose.Cellsを使用すると、開発者はCellクラスのPutValueメソッドを呼び出すことで、ワークシート内のセルにデータを追加できます。 Aspose.Cellsは、PutValueメソッドのオーバーロードされたバージョンを提供しており、開発者はこれらのオーバーロードされたバージョンのPutValueメソッドを使用して、セルに異なる種類のデータ(ブール値、文字列、倍精度浮動小数点数、整数、日付/時刻など)を追加することができます。

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 sampleData = dirPath + u"sampleData.xlsx";
//Path of output excel file
U16String outputData = outPath + u"outputData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the second worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(1);
//Adding a string value to the cell
worksheet.GetCells().Get(u"A1").PutValue(u"Hello World");
//Adding a double value to the cell
worksheet.GetCells().Get(u"A2").PutValue(20.5);
//Adding an integer value to the cell
worksheet.GetCells().Get(u"A3").PutValue(15);
//Adding a boolean value to the cell
worksheet.GetCells().Get(u"A4").PutValue(true);
//Setting the display format of the date
Cell cell = worksheet.GetCells().Get(u"A5");
Style style = cell.GetStyle();
style.SetNumber(15);
cell.SetStyle(style);
//Save the workbook
workbook.Save(outputData);
Aspose::Cells::Cleanup();

効率の向上

ワークシートに大量のデータを追加する場合は、まず行ごとに値を追加し、次に列ごとに追加する必要があります。このアプローチにより、アプリケーションの効率が大幅に改善されます。

セルからデータを取得

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

Cellクラスには、そのデータ型に応じてセルから値を取得するためのいくつかのメソッドが提供されています。これらのメソッドには次のものがあります:

フィールドが入力されていない場合、GetDoubleValueまたはGetFloatValueは例外をスローします。

セルに含まれるデータの型は、CellクラスのGetTypeメソッドを使用しても確認できます。実際、CellクラスのGetTypeメソッドは、CellValueType列挙体に基づいており、その事前定義値が以下にリストされています:

セル値の種類 説明
CellValueType_IsBool セルの値がブール型であることを指定します。
CellValueType_IsDateTime セルの値が日付/時刻型であることを指定します。
CellValueType_IsNull 空白のセルを表します。
CellValueType_IsNumeric セルの値が数値型であることを指定します。
CellValueType_IsString セルの値が文字列型であることを指定します。
CellValueType_IsUnknown セルの値が不明な型であることを指定します。
上記で定義されたセルの事前定義値を使用して、各セルに存在するデータの型と比較することもできます。
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 sampleData = dirPath + u"sampleData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the third worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(2);
//Get cells from sheet
Cells cells = worksheet.GetCells();
//Variable declarations
U16String strVal;
Date dateVal;
double dblVal;
bool boolVal;
Enumerator<Cell> enCell = cells.GetEnumerator();
while (enCell.MoveNext())
{
Cell cell = enCell.GetCurrent();
switch (cell.GetType())
{
//Evaluating the data type of the cell data for string value
case CellValueType::IsString:
std::cout <<"Cell Value Type Is String." << std::endl;
strVal = cell.GetStringValue();
break;
//Evaluating the data type of the cell data for double value
case CellValueType::IsNumeric:
std::cout <<"Cell Value Type Is Numeric." << std::endl;
dblVal = cell.GetDoubleValue();
break;
//Evaluating the data type of the cell data for boolean value
case CellValueType::IsBool:
std::cout <<"Cell Value Type Is Bool." << std::endl;
boolVal = cell.GetBoolValue();
break;
//Evaluating the data type of the cell data for date/time value
case CellValueType::IsDateTime:
std::cout <<"Cell Value Type Is DateTime." << std::endl;
dateVal = cell.GetDateTimeValue();
break;
//Evaluating the unknown data type of the cell data
case CellValueType::IsUnknown:
cell.GetStringValue();
break;
default:
break;
}
}
Aspose::Cells::Cleanup();