Excelファイルのデータ管理

セルにデータを追加する方法

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

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Adding a string value to the cell
worksheet.Cells["A1"].PutValue("Hello World");
// Adding a double value to the cell
worksheet.Cells["A2"].PutValue(20.5);
// Adding an integer value to the cell
worksheet.Cells["A3"].PutValue(15);
// Adding a boolean value to the cell
worksheet.Cells["A4"].PutValue(true);
// Adding a date/time value to the cell
worksheet.Cells["A5"].PutValue(DateTime.Now);
// Setting the display format of the date
Style style = worksheet.Cells["A5"].GetStyle();
style.Number = 15;
worksheet.Cells["A5"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "output.out.xls");

効率を向上させる方法

PutValueメソッドを使用してワークシートに大量のデータを追加する場合は、まず行ごとに、次に列ごとに値をセルに追加するべきです。このアプローチにより、アプリケーションの効率が大幅に向上します。

セルからデータを取得する方法

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

Cellクラスには、それぞれのデータ型に応じてセルから値を取得するためのいくつかのプロパティが提供されています。

  • StringValue:セルの文字列値を返します。
  • DoubleValue:セルの倍精度浮動小数点数値を返します。
  • BoolValue:セルのブール値を返します。
  • DateTimeValue:セルの日付/時刻値を返します。
  • FloatValue:セルの浮動小数点数値を返します。
  • IntValue:セルの整数値を返します。

フィールドが埋められていない場合、DoubleValueまたはFloatValueのセルは例外をスローします。

また、CellクラスのTypeプロパティを使用してセルに含まれるデータの型を確認することもできます。実際、CellクラスのTypeプロパティは、その前に定義された値がリストされたCellValueType列挙型に基づいています。

セル値の種類 説明
IsBool セルの値がブール型であることを指定します。
IsDateTime セルの値が日付/時刻であることを指定します。
IsNull 空白セルを表します。
IsNumeric セルの値が数値であることを指定します。
IsString セルの値が文字列であることを指定します。
IsUnknown セルの値が不明であることを指定します。

上記の事前定義されたセル値タイプを使用して、各セルに存在するデータのTypeと比較することもできます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Opening an existing workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing first worksheet
Worksheet worksheet = workbook.Worksheets[0];
foreach (Cell cell1 in worksheet.Cells)
{
// Variables to store values of different data types
string stringValue;
double doubleValue;
bool boolValue;
DateTime dateTimeValue;
// Passing the type of the data contained in the cell for evaluation
switch (cell1.Type)
{
// Evaluating the data type of the cell data for string value
case CellValueType.IsString:
stringValue = cell1.StringValue;
Console.WriteLine("String Value: " + stringValue);
break;
// Evaluating the data type of the cell data for double value
case CellValueType.IsNumeric:
doubleValue = cell1.DoubleValue;
Console.WriteLine("Double Value: " + doubleValue);
break;
// Evaluating the data type of the cell data for boolean value
case CellValueType.IsBool:
boolValue = cell1.BoolValue;
Console.WriteLine("Bool Value: " + boolValue);
break;
// Evaluating the data type of the cell data for date/time value
case CellValueType.IsDateTime:
dateTimeValue = cell1.DateTimeValue;
Console.WriteLine("DateTime Value: " + dateTimeValue);
break;
// Evaluating the unknown data type of the cell data
case CellValueType.IsUnknown:
stringValue = cell1.StringValue;
Console.WriteLine("Unknown Value: " + stringValue);
break;
// Terminating the type checking of type of the cell data is null
case CellValueType.IsNull:
break;
}
}

高度なトピック