Impostazioni di riempimento

Colori e motivi di sfondo

Microsoft Excel può impostare i colori del primo piano (contorno) e lo sfondo (riempimento) delle celle e i motivi di sfondo.

Aspose.Cells supporta anche queste funzionalità in modo flessibile. In questo argomento, impariamo ad utilizzare queste funzionalità utilizzando Aspose.Cells.

Impostazione di colori e motivi di sfondo

Aspose.Cells fornisce una classe, Workbook che rappresenta un file di Microsoft Excel. La classe Workbook contiene una raccolta Worksheets che consente l’accesso a ogni foglio di lavoro nel file di Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells. Ciascun elemento della raccolta Cells rappresenta un oggetto della classe Cell.

Il Cell ha i metodi GetStyle e SetStyle che vengono utilizzati per ottenere e impostare la formattazione di una cella. La classe Style fornisce proprietà per impostare i colori del primo piano e dello sfondo delle celle. Aspose.Cells fornisce un’enumerazione BackgroundType che contiene un insieme di tipi predefiniti di motivi di sfondo che sono di seguito elencati.

Motivi di sfondo Descrizione
DiagonalCrosshatch Rappresenta un motivo a croce diagonale
DiagonalStripe Rappresenta un motivo a strisce diagonali
Gray6 Rappresenta un motivo grigio al 6.25%
Gray12 Rappresenta un motivo grigio al 12.5%
Gray25 Rappresenta un motivo grigio al 25%
Gray50 Rappresenta 50% modello grigio
Gray75 Rappresenta 75% modello grigio
HorizontalStripe Rappresenta modello a strisce orizzontali
None Rappresenta nessuno sfondo
ReverseDiagonalStripe Rappresenta modello a strisce diagonali invertite
Solid Rappresenta modello solido
ThickDiagonalCrosshatch Rappresenta modello spesso di incroci diagonali
ThinDiagonalCrosshatch Rappresenta modello sottile di incroci diagonali
ThinDiagonalStripe Rappresenta modello sottile a strisce diagonali
ThinHorizontalCrosshatch Rappresenta modello sottile di incroci orizzontali
ThinHorizontalStripe Rappresenta modello sottile a strisce orizzontali
ThinReverseDiagonalStripe Rappresenta modello sottile a strisce diagonali invertite
ThinVerticalStripe Rappresenta modello sottile a strisce verticali
VerticalStripe Rappresenta modello a strisce verticali

Nell’esempio seguente, il colore dell’oggetto A1 è impostato ma A2 è configurato per avere sia colori di primo piano sia di sfondo con un modello di sfondo a strisce verticali.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Define a Style and get the A1 cell style
Style style = worksheet.Cells["A1"].GetStyle();
// Setting the foreground color to yellow
style.ForegroundColor = Color.Yellow;
// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;
// Apply the style to A1 cell
worksheet.Cells["A1"].SetStyle(style);
// Get the A2 cell style
style = worksheet.Cells["A2"].GetStyle();
// Setting the foreground color to blue
style.ForegroundColor = Color.Blue;
// Setting the background color to yellow
style.BackgroundColor = Color.Yellow;
// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;
// Apply the style to A2 cell
worksheet.Cells["A2"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Importante sapere

Applicazione degli effetti di riempimento a sfumatura

Per applicare i vostri desiderati effetti di riempimento a sfumatura alla cella, utilizzate il metodo SetTwoColorGradient dell’oggetto Style di conseguenza.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet (default) in the workbook
Worksheet worksheet = workbook.Worksheets[0];
// Input a value into B3 cell
worksheet.Cells[2, 1].PutValue("test");
// Get the Style of the cell
Style style = worksheet.Cells["B3"].GetStyle();
// Set Gradient pattern on
style.IsGradient = true;
// Specify two color gradient fill effects
style.SetTwoColorGradient(Color.FromArgb(255, 255, 255), Color.FromArgb(79, 129, 189), GradientStyleType.Horizontal, 1);
// Set the color of the text in the cell
style.Font.Color = Color.Red;
// Specify horizontal and vertical alignment settings
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
// Apply the style to the cell
worksheet.Cells["B3"].SetStyle(style);
// Set the third row height in pixels
worksheet.Cells.SetRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.Cells.Merge(2, 1, 1, 2);
// Save the Excel file
workbook.Save(dataDir + "output.xlsx");

Colori e Palette

Una palette è il numero di colori disponibili per creare un’immagine. L’uso di una palette standardizzata in una presentazione consente all’utente di creare un aspetto costante. Ogni file di Microsoft Excel (97-2003) ha una palette di 56 colori che possono essere applicati a celle, caratteri, linee guida, oggetti grafici, riempimenti e linee in un grafico.

Con Aspose.Cells è possibile utilizzare non solo i colori esistenti nella palette ma anche colori personalizzati. Prima di utilizzare un colore personalizzato, aggiungilo prima alla palette.

Questo argomento tratta come aggiungere colori personalizzati alla palette.

Aggiunta colori personalizzati alla palette

Aspose.Cells supporta la palette a 56 colori di Microsoft Excel. Per utilizzare un colore personalizzato non definito nella palette, aggiungi il colore alla palette.

Aspose.Cells fornisce una classe, Workbook, che rappresenta un file di Microsoft Excel. La classe Workbook fornisce un metodo ChangePalette che richiede i seguenti parametri per aggiungere un colore personalizzato alla modifica della palette:

  • Colore personalizzato, il colore personalizzato da aggiungere.
  • Indice, l’indice del colore nella palette che il colore personalizzato sostituirà. Dovrebbe essere compreso tra 0 e 55.

Nell’esempio seguente viene aggiunto un colore personalizzato (Orchidea) alla palette prima di applicarlo a un carattere.

// Instantiating an Workbook object
Workbook workbook = new Workbook();
//Checks if a color is in the palette for the spreadsheet.
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// Adding Orchid color to the palette at 55th index
workbook.ChangePalette(Color.Orchid, 55);
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining new Style object
Style styleObject = workbook.CreateStyle();
// Setting the Orchid (custom) color to the font
styleObject.Font.Color = workbook.Colors[55];
// Applying the style to the cell
cell.SetStyle(styleObject);
// Saving the Excel file
workbook.Save("out.xlsx");