Åtkomst och Modifiera cellvärde

Komma åt och ändra ett cells värde

Strängvärden

Innan man får tillgång till och modifierar värdet på en cell måste man veta hur man får tillgång till celler. För detaljer om de olika tillvägagångssätten för att få tillgång till celler, hänvisa till Tillgång till Arbetsbladsceller.

Varje cell har en egenskap som heter StringValue. När en cell är åtkomst, kan utvecklare använda egenskapen StringValue för att få tillgång till cellernas strängvärde. För att modifiera cellvärden tillhandahålls en särskild metod PutValue, som kan användas för att uppdatera cellens strängvärde.

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

Alla typer av värden

PutValue-metoden för ett cellsobjekt har 8 överbelastningar tillgängliga som kan användas för att modifiera vilken typ av värde som helst (Boolean, int, double, DateTime och sträng) i en cell.

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

Det finns också en överbelastad version av PutValue-metoden som kan ta vilken typ av värde som helst i strängformat och automatiskt konvertera det till en korrekt datatyp. För att detta ska ske, skicka det booleska värdet true till en annan parameter av PutValue-metoden som visas nedan i exemplet.

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