En savoir plus sur Xeed Workbooks pour .NET
Maintenant que nous avons couvert les bases de la création d'un document xlsx et que nous avons appris à ajouter et à modifier le contenu d'un document xlsx, nous sommes en mesure de le faire. Feuille de travail y compris Cellules et TableauxNous allons donc voir comment le rendre attrayant en tirant parti des Styles et des SheetViews !
Note:
At the time of this writing, more options have been added to the Style and SheetView classes and will be included in a future release of Xceed Workbooks for .NET.
We will cover these new options in a part 2 article once they are available.
Qu'est-ce qu'un style ?
En d'autres termes, un Style est un groupe de paramètres permettant de modifier l'apparence des cellules. Cellule, les cellules d'un Colonneou les cellules d'un Rangée.
Plus précisément, ces paramètres sont actuellement les suivants
Font: the Font to use for the current Style.
Alignment: the horizontal and vertical alignments used to align the content of a cell.
CustomFormat: the format used to display the content of a Cell (null by default, setting this property will overwrite the PredefinedNumberFormatId property)
PredefinedNumberFormatId: the predefined number format with an Id (0 by default, setting this property will overwrite the CustomFormat property)
L'utilisation des styles est un excellent moyen d'assurer une mise en forme cohérente dans vos classeurs et feuilles de calcul.
Application d'un style
Un style peut être spécifié soit sur un Cellule, les cellules d'un Colonneou des cellules d'un Rangée.
Cela soulève toutefois la question suivante : que se passe-t-il si une cellule est affectée par plus d'un style ? Par exemple, nous avons un style sur la colonne B et également un style sur la 5ème ligne, dans cette situation, quel style serait utilisé pour la cellule "B5" ? La priorité est déterminée dans l'ordre suivant : Cellule, Ligne, Colonne. Cela signifie que dans notre exemple, si aucun style n'est appliqué directement à la cellule "B5", c'est le style au niveau de la ligne qui sera utilisé.
Les exemples ci-dessous supposent la disposition initiale suivante du code :
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
// Styling a specific Cell using its address
worksheet.Cells[ "B2" ].Style. [...]
// Styling a specific Cell using its row and column index
worksheet.Cells[ 1, 1 ].Style. [...]
// Styling the Cells of a Column
worksheet.Columns[ 2 ].Style. [...]
// Styling the Cells of a Row
worksheet.Rows[ 3 ].Style. [...]
document.Save();
}
Police (propriétés disponibles) ici)
// Specific Cell
worksheet.Cells[ "B1" ].Style.Font = new Font() { Bold = true, Size = 15.5d };
// Column
worksheet.Columns[ 2 ].Style.Font = new Font() { Name = "Lucida Fax", Italic = true, Underline = true, UnderlineType = UnderlineType.Double };
// Row
worksheet.Rows[ 3 ].Style.Font = new Font() { Strikethrough = true, Size = 18d };
Alignement (valeurs disponibles pour Alignement horizontal et Alignement vertical)
// Specific Cell
worksheet.Cells[ "B1" ].Style.Alignment.Horizontal = HorizontalAlignment.Center;
worksheet.Cells[ "B1" ].Style.Alignment.Vertical = VerticalAlignment.Center;
// Column
worksheet.Columns[ 2 ].Style.Alignment = new Alignment( HorizontalAlignment.Justify, VerticalAlignment.Center);
// Row
worksheet.Rows[ 3 ].Style.Alignment.Horizontal = HorizontalAlignment.CenterAcrossSelection;
Format personnalisé
// Cell B2 contains 25.6 displayed as "$25.6000"
worksheet.Cells[ 1, 1 ].Value = 25.6;
worksheet.Cells[ 1, 1 ].Style.CustomFormat = "$0.0000";
// Cell C3 contains a date formatted to display as "10:22 AM"
worksheet.Cells[ 2, 2 ].Value = new DateTime( 2021, 7, 1, 10, 22, 33 );
worksheet.Cells[ 2, 2 ].Style.CustomFormat = "h:mm AM/PM";
// Column
worksheet.Columns[ 2 ].Style.CustomFormat = "h:mm AM/PM";
// Row
worksheet.Rows[ 3 ].Style.CustomFormat = "$0.0000";
PredefinedNumberFormatId (valeurs disponibles) ici)
// Cell D4 contains 33.5 set to format "0.00" to display the value as "33.50"
worksheet.Cells[ "D4" ].Value = 33.5;
worksheet.Cells[ "D4" ].Style.PredefinedNumberFormatId = 2;
// Column with dates formatted as mm-dd-yy
worksheet.Columns[ 2 ].Style.PredefinedNumberFormatId = 14;
// Row with time formatted as [h]:mm:ss
worksheet.Rows[ 3 ].Style.PredefinedNumberFormatId = 46;
Qu'est-ce qu'un SheetView ?
Un SheetView est un moyen de créer des vues personnalisées dans une base de données. Feuille de travail.
La classe SheetView représente une vue d'une seule feuille à partir d'un fichier Feuille de travail.
Note:
At the time of writing, only one SheetView per Worksheet is supported.
Utilisation d'un SheetView
La classe SheetView prend actuellement en charge les propriétés suivantes :
ActiveCellAddress: the active Cell in the Worksheet, which is the one with focus.
TopLeftCellAddress: the Cell to be displayed as the top left position in the Worksheet view.
// Setting a SheetView
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = doc.Worksheets[ 0 ];
worksheet.SheetView.ActiveCellAddress = "C2";
worksheet.SheetView.TopLeftCellAddress = "B1";
document.Save();
}
Pour plus d'informations, veuillez vous référer à la la documentation.