Excelファイルのデータ管理。

Cells にデータを追加する

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

Aspose.Cells を使用すると、開発者は、Cellクラス'プットバリュー方法。 Aspose.Cells は、プットバリュー開発者がさまざまな種類のデータをセルに追加できるようにするメソッド。これらのオーバーロードされたバージョンのプットバリューメソッドを使用すると、ブール値、文字列、倍精度、整数、日付/時刻などの値をセルに追加できます。

// 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");

効率の向上

使用する場合プットバリューメソッドを使用して大量のデータをワークシートに配置するには、最初に行ごと、次に列ごとにセルに値を追加する必要があります。このアプローチにより、アプリケーションの効率が大幅に向上します。

Cells からのデータの取得

Aspose.Cells はクラスを提供し、ワークブック Microsoft Excel ファイルを表します。のワークブッククラスにはワークシートファイル内のワークシートへのアクセスを許可するコレクション。ワークシートは、ワークシートクラス。のワークシートクラスはCellsコレクション。の各項目Cellsコレクションはのオブジェクトを表しますCellクラス。

Cellクラスには、開発者がデータ型に従ってセルから値を取得できるいくつかのプロパティが用意されています。これらのプロパティは次のとおりです。

フィールドが入力されていない場合、DoubleValueまた浮動小数点値例外をスローします。

セルに含まれるデータの種類は、Cellクラス'タイプ財産。実際、Cellクラス'タイププロパティはに基づいていますCellValueType定義済みの値が以下にリストされている列挙型:

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

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

// 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;
}
}

先行トピック