Más información Cuadernos Xeed para .NET
En el artículo anterior, aprendimos los conceptos básicos de la creación de un documento xlsx utilizando la función Cree, Carga, Ahorra y Guardar como así como la forma de añadir y recuperar Hojas de trabajo.
Esta vez aprenderemos los conceptos básicos de cómo añadir y modificar el contenido de un archivo Hoja de trabajo.
¿Qué es exactamente una hoja de cálculo?
En pocas palabras, un Hoja de trabajo es una colección de celdas. A Celda representa un rectángulo en una hoja de cálculo; cada celda es la intersección de una celda Fila y un Columna.
Note:
A Worksheet can have a maximum of 16,384 columns and 1,048,576 rows.
Acceso a las celdas de una hoja de cálculo
Para poder modificar el contenido de una Hoja de Cálculo, necesitaremos poder acceder a la(s) Celda(s) a modificar.
Hay tres (3) maneras de acceder a una celda determinada:
1. A través de la Células Colección:
By using an address. This is a combination of a letter (for the column) and a number (for the row). The first column starts at "A" and the first row starts at "1", meaning the top left cell is located at the "A1" address.
By using coordinates. This is done by specifying the Row Id first and then the Column Id. In both cases the indexes start at 0, meaning the top left cell is located at the 0,0 coordinates.
2. A través de la Células de una columna determinada en el Columnas Colección:
By using the Row Id index. The index starts at 0, so the top-most cell in a column is located at index 0.
3. A través de la Células de una fila determinada del Filas Colección:
By using the Column Id index. The index starts at 0, so the left-most cell in a row is located at index 0.
// Accessing specific cells
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
// accessing cell A1 through the Cells collection
var cellA1_FromAddress = worksheet.Cells[ "A1" ];
var cellA1_FromCoordinates = worksheet.Cells[ 0, 0 ];
// accessing cell B2 through the Columns collection
var cellB2 = worksheet.Columns[ 1 ].Cells[ 1 ];
// accessing cell C3 through the Rows collection
var cellC3 = worksheet.Rows[ 2 ].Cells[ 2 ];
}
Notes:
The top left cell does not start at 1,1 like in Excel because for a developer, it is more intuitive to start the index at 0, and as such the top left cell is at coordinates 0,0
If performance is important, an access by coordinates is faster than an access by address.
Modificación de células
El contenido de una celda puede establecerse mediante los botones Valor o Fórmula propiedades.
A Valor es estático, mientras que a Fórmula puede ser dinámica, lo que significa que el valor mostrado puede cambiar en función de los elementos de la Fórmula (por ejemplo, si la Fórmula es la suma de un grupo de celdas, el valor mostrado cambiará si se cambia cualquier valor en esas otras celdas).
// Modifying the content of Cells
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
// modifying cell A1 through the Cells collection
worksheet.Cells[ "A1" ].Value = new DateTime( 2021, 7, 1, 10, 22, 33);
// modifying cell B2 through the Columns collection
worksheet.Columns[ 1 ].Cells[ 1 ].Value = 100;
// modifying cell C3 through the Rows collection
worksheet.Rows[ 2 ].Cells[ 2 ].Formula "=SUM(A2:A5)";
// save the modifications
document.Save();
}
Si Fórmula.celda está activado, el Celda.Valor se cubrirán en dos (2) momentos únicamente:
when MS Excel will be used to opened the saved document.
if the user calls the Worksheet.CalculateFormulas() method to calculate the formulas of a Worksheet, or Workbook.CalculateFormulas() to calculate the formulas of all the Worksheets within a Workbook.
Modificación de columnas
También podemos acceder al Columnas para modificar una columna específica. En el momento de redactar este documento, las propiedades que se pueden modificar en una columna son las siguientes BestFit y Anchura.
Para especificar qué Columna modificar, especificamos el índice (0 = Columna A).
// Modifying Columns
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
// modifying column A
worksheet.Columns[ 0 ].BestFit = true;
worksheet.Columns[ 0 ].Width = 65d;
// modifying column B
worksheet.Columns[ 1 ].BestFit = false;
worksheet.Columns[ 1 ].Width = 85d;
// save the modifications
document.Save();
}
Note:
The AutoFit() method on Column can be used to adjust the width of the column to the longest content. There are four (4) optional parameters: minimumWidth and maximumWidth (amount of characters under the default font), as well as startRowId and endRowId (to indicate the range of cells whose content will be evaluated to affect the AutoFit).
Modificación de filas
También podemos acceder al Filas para modificar una fila específica. En el momento de escribir esto, la única propiedad que se puede modificar en una fila es Altura.
Para especificar qué Fila modificar, especificamos el índice (0 = Fila 1).
// Modifying Rows
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
// modifying row 1
worksheet.Rows[ 0 ].Height = 85d;
// modifying row 2
worksheet.Rows[ 1 ].Height = 65d;
// save the modifications
document.Save();
}
Note:
An AutoFit() method on Row will be available in the next release of the product, and will function in a similar way to the AutoFit() method on Column.
Para más información, consulte el documentación.