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 per Python via .NET supporta anche queste funzionalità in modo flessibile. In questo argomento, impareremo a usare queste funzionalità con Aspose.Cells.

Impostazione di colori e motivi di sfondo

Aspose.Cells per Python via .NET fornisce una classe, Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una collezione worksheets che permette di accedere a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una collezione Cells. Ogni elemento nella collezione Cells rappresenta un oggetto della classe Cell.

Il Cell ha i metodi get_style e set_style utilizzati per ottenere e impostare la formattazione di una cella. La classe Style fornisce proprietà per impostare i colori di primo piano e di sfondo delle celle. Aspose.Cells per Python via .NET fornisce un’enumerazione BackgroundType che contiene un insieme di tipi predefiniti di motivi di sfondo.

Motivi di sfondo Descrizione
DIAGONAL_CROSSHATCH Rappresenta un motivo incrociato diagonale
DIAGONAL_STRIPE Rappresenta un motivo a strisce diagonali
GRAY6 Rappresenta un motivo grigio del 6,25%
GRAY12 Rappresenta un motivo grigio del 12,5%
GRAY25 Rappresenta un motivo grigio del 25%
GRAY50 Rappresenta il motivo grigio al 50%
GRAY75 Rappresenta il motivo grigio al 75%
HORIZONTAL_STRIPE Rappresenta il motivo a strisce orizzontali
NONE Rappresenta nessuno sfondo
REVERSE_DIAGONAL_STRIPE Rappresenta il motivo a strisce diagonali inverse
SOLID Rappresenta un motivo solido
THICK_DIAGONAL_CROSSHATCH Rappresenta il motivo a croce diagonale spesso
THIN_DIAGONAL_CROSSHATCH Rappresenta il motivo a croce diagonale sottile
THIN_DIAGONAL_STRIPE Rappresenta il motivo a strisce diagonali sottili
THIN_HORIZONTAL_CROSSHATCH Rappresenta il motivo a croce orizzontale sottile
THIN_HORIZONTAL_STRIPE Rappresenta il motivo a strisce orizzontali sottili
THIN_REVERSE_DIAGONAL_STRIPE Rappresenta il motivo a strisce diagonali inverse sottili
THIN_VERTICAL_STRIPE Rappresenta il motivo a strisce verticali sottili
VERTICAL_STRIPE Rappresenta il motivo 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.

from aspose.cells import BackgroundType, SaveFormat, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Define a Style and get the A1 cell style
style = worksheet.cells.get("A1").get_style()
# Setting the foreground color to yellow
style.foreground_color = Color.yellow
# Setting the background pattern to vertical stripe
style.pattern = BackgroundType.VERTICAL_STRIPE
# Apply the style to A1 cell
worksheet.cells.get("A1").set_style(style)
# Get the A2 cell style
style = worksheet.cells.get("A2").get_style()
# Setting the foreground color to blue
style.foreground_color = Color.blue
# Setting the background color to yellow
style.background_color = Color.yellow
# Setting the background pattern to vertical stripe
style.pattern = BackgroundType.VERTICAL_STRIPE
# Apply the style to A2 cell
worksheet.cells.get("A2").set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

Importante sapere

Applicazione degli effetti di riempimento a sfumatura

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

from aspose.cells import TextAlignmentType, Workbook
from aspose.cells.drawing import GradientStyleType
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate a new Workbook
workbook = Workbook()
# Get the first worksheet (default) in the workbook
worksheet = workbook.worksheets[0]
# Input a value into B3 cell
worksheet.cells.get(2, 1).put_value("test")
# Get the Style of the cell
style = worksheet.cells.get("B3").get_style()
# Set Gradient pattern on
style.is_gradient = True
# Specify two color gradient fill effects
style.set_two_color_gradient(Color.from_argb(255, 255, 255), Color.from_argb(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.horizontal_alignment = TextAlignmentType.CENTER
style.vertical_alignment = TextAlignmentType.CENTER
# Apply the style to the cell
worksheet.cells.get("B3").set_style(style)
# Set the third row height in pixels
worksheet.cells.set_row_height_pixel(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 per Python via .NET è possibile non solo usare i colori esistenti della palette, ma anche colori personalizzati. Prima di usare un colore personalizzato, aggiungilo prima alla palette.

Questo argomento tratta come aggiungere colori personalizzati alla palette.

Aggiunta colori personalizzati alla palette

Aspose.Cells per Python via .NET supporta la palette di 56 colori di Microsoft Excel. Per usare un colore personalizzato non definito nella palette, aggiungi il colore alla palette.

Aspose.Cells per Python via .NET fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook fornisce un metodo change_palette che accetta i seguenti parametri per aggiungere un colore personalizzato e modificare la 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.

from aspose.cells import SaveFormat, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating an Workbook object
workbook = Workbook()
# Adding Orchid color to the palette at 55th index
workbook.change_palette(Color.orchid, 55)
# Adding a new worksheet to the Excel object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Defining new Style object
styleObject = workbook.create_style()
# Setting the Orchid (custom) color to the font
styleObject.font.color = Color.orchid
# Applying the style to the cell
cell.set_style(styleObject)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.AUTO)