访问和修改单元格值

访问和修改单元格的值

字符串值

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

每个单元格都有一个名为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);