セルの値へのアクセスと変更

セルの値のアクセスおよび変更

文字列の値

セルの値にアクセスし変更する前に、セルへのアクセス方法について知る必要があります。セルへのアクセス方法の詳細については、ワークシートセルへのアクセス を参照してください。

各セルには、StringValueというプロパティがあります。セルにアクセスした後、開発者はStringValueプロパティを使用してセルの文字列値にアクセスできます。セルの値を変更するには、セルの文字列値を更新するための特別なPutValueメソッドが提供されており、これを使用してセルの文字列値を更新できます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Accessing "B1" cell of the worksheet
GridCell cell = sheet.Cells["B1"];
// Accessing the string value of "B1" cell
Label1.Text = cell.StringValue;
// Modifying the string value of "B1" cell
cell.PutValue("Hello Aspose.Grid");

すべての種類の値

セルのオブジェクトのPutValueメソッドには8つのオーバーロードがあり、セル内の任意のタイプの値(Boolean、int、double、DateTime、およびstring)を変更するために使用できます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Accessing "B3" cell of the worksheet
GridCell cell = sheet.Cells["B3"];
// Putting a value in "B3" cell
cell.PutValue(30);

PutValueメソッドのオーバーロードバージョンもあり、任意のkind of valueを文字列形式で受け取り、自動的に適切なデータ型に変換できます。これを実現するには、PutValueメソッドの別のパラメーターにtrueというBoolean値を渡します。以下の例に示すように。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Accessing "B5" cell of the worksheet
GridCell cell = sheet.Cells["B5"];
// Putting a numeric value as string in "B5" cell that will be converted to a suitable data type automatically
cell.PutValue("19.4", true);