Accesso e Modifica del Valore della Cella

Accesso e modifica del valore di una cella

Valori stringa

Prima di accedere e modificare il valore di una cella, è necessario sapere come accedere alle celle. Per ulteriori dettagli sui diversi approcci per accedere alle celle, fare riferimento all’articolo Accesso alle celle del foglio di lavoro.

Ogni cella ha una proprietà chiamata StringValue. Una volta che una cella viene acceduta, gli sviluppatori possono utilizzare la proprietà StringValue per accedere al valore di stringa delle celle. Per modificare i valori delle celle è fornito un metodo speciale PutValue, che può essere utilizzato per aggiornare il valore di stringa della cella.

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

Tutti i tipi di valori

Il metodo PutValue di un oggetto cella ha a disposizione 8 sovraccarichi che possono essere utilizzati per modificare qualsiasi tipo di valore (Boolean, int, double, DateTime e stringa) in una cella.

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

C’è anche una versione sovraccaricata del metodo PutValue che può prendere qualsiasi tipo di valore in formato stringa e convertirlo automaticamente in un tipo di dati appropriato. Per farlo accadere, passare il valore Booleano true a un altro parametro del metodo PutValue come mostrato di seguito nell’esempio.

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