访问和修改 Cell 值

访问和修改 Cell 的值

字符串值

在访问和修改单元格的值之前,您需要知道如何访问单元格。有关访问单元格的不同方法的详细信息,请参阅访问工作表 Cells.

每个单元格都有一个名为 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 个可用重载,可用于修改单元格中的任何类型的值(布尔值、整数、双精度值、日期时间和字符串)。

// 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 方法的重载版本,它可以采用字符串格式的任何类型的值并自动将其转换为适当的数据类型。为此,将布尔值 true 传递给 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 "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);