Formattare celle
Introduzione
Come formattare le celle utilizzando i metodi GetStyle e SetStyle
Applicare diversi tipi di stili di formattazione alle celle per impostare colori di sfondo o primo piano, bordi, caratteri, allineamenti orizzontali e verticali, livello di rientro, direzione del testo, angolo di rotazione e molto altro.
Come utilizzare i metodi GetStyle e SetStyle
Se gli sviluppatori devono applicare stili di formattazione diversi a celle diverse, è meglio ottenere il Style della cella utilizzando il metodo Cell.get_style, specificare gli attributi dello stile e quindi applicare la formattazione utilizzando il metodo Cell.set_style. Di seguito è riportato un esempio per dimostrare questo approccio per applicare varie formattazioni a una cella.
from aspose.cells import BorderType, CellBorderType, TextAlignmentType, 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() | |
# Obtaining the reference of the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Get the style from A1 cell | |
style = cell.get_style() | |
# Setting the vertical alignment | |
style.vertical_alignment = TextAlignmentType.CENTER | |
# Setting the horizontal alignment | |
style.horizontal_alignment = TextAlignmentType.CENTER | |
# Setting the font color of the text | |
style.font.color = Color.green | |
# Setting to shrink according to the text contained in it | |
style.shrink_to_fit = True | |
# Setting the bottom border color to red | |
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red | |
# Setting the bottom border type to medium | |
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM | |
# Applying the style to A1 cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
Come utilizzare l’oggetto stile per formattare celle diverse
Se gli sviluppatori hanno bisogno di applicare lo stesso stile di formattazione a diverse celle, possono utilizzare l’oggetto Style. Seguire i passaggi seguenti per utilizzare l’oggetto Style:
- Aggiungi un oggetto Style richiamando il metodo create_style della classe Workbook
- Accedere al nuovo oggetto Style aggiunto
- Imposta le proprietà/attributi desiderati dell’oggetto Style per applicare le impostazioni di formattazione desiderate
- Assegna l’oggetto Style configurato alle celle desiderate
Questo approccio può migliorare notevolmente l’efficienza delle tue applicazioni e risparmiare memoria anche.
from aspose.cells import BorderType, CellBorderType, TextAlignmentType, 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 Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the first 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!") | |
# Adding a new Style | |
style = workbook.create_style() | |
# Setting the vertical alignment of the text in the "A1" cell | |
style.vertical_alignment = TextAlignmentType.CENTER | |
# Setting the horizontal alignment of the text in the "A1" cell | |
style.horizontal_alignment = TextAlignmentType.CENTER | |
# Setting the font color of the text in the "A1" cell | |
style.font.color = Color.green | |
# Shrinking the text to fit in the cell | |
style.shrink_to_fit = True | |
# Setting the bottom border color of the cell to red | |
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red | |
# Setting the bottom border type of the cell to medium | |
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM | |
# Assigning the Style object to the "A1" cell | |
cell.set_style(style) | |
# Apply the same style to some other cells | |
worksheet.cells.get("B1").set_style(style) | |
worksheet.cells.get("C1").set_style(style) | |
worksheet.cells.get("D1").set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
Come utilizzare gli stili predefiniti di Microsoft Excel 2007
Se devi applicare stili di formattazione diversi per Microsoft Excel 2007, applica gli stili usando l’API di Aspose.Cells for Python via .NET. Un esempio viene fornito di seguito per dimostrare questo approccio nell’applicare uno stile predefinito su una cella.
from aspose.cells import Workbook | |
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) | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Create a style object . | |
style = workbook.create_style() | |
# Input a value to A1 cell. | |
workbook.worksheets[0].cells.get("A1").put_value("Test") | |
# Apply the style to the cell. | |
workbook.worksheets[0].cells.get("A1").set_style(style) | |
# Save the Excel 2007 file. | |
workbook.save(dataDir + "book1.out.xlsx") |
Come formattare i caratteri selezionati in una cella
Il trattamento delle impostazioni del carattere spiega come formattare il testo nelle celle, ma spiega solo come formattare tutto il contenuto della cella. E se si vuole formattare solo alcuni caratteri?
Aspose.Cells per Python via .NET supporta anche questa funzionalità. Questo argomento spiega come utilizzare efficacemente questa funzione.
Come formattare caratteri selezionati
Aspose.Cells per Python via .NET fornisce una classe, Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene la collezione worksheets che consente l’accesso a ogni foglio di lavoro in un 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.
La classe Cell fornisce il metodo characters che riceve i seguenti parametri per selezionare una gamma di caratteri all’interno di una cella:
- Indice di inizio, l’indice del carattere da cui inizia la selezione.
- Numero di caratteri, il numero di caratteri da selezionare.
Il metodo characters restituisce un’istanza della classe FontSetting che consente agli sviluppatori di formattare i caratteri allo stesso modo in cui farebbero con una cella come mostrato di seguito nell’esempio di codice. Nel file di output, nella cella A1, la parola ‘Visit’ verrà formattata con il carattere predefinito ma ‘Aspose!’ è in grassetto e blu.
from aspose.cells import 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() | |
# Obtaining the reference of the first(default) worksheet by passing its sheet index | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Visit Aspose!") | |
# Setting the font of selected characters to bold | |
cell.characters(6, 7).font.is_bold = True | |
# Setting the font color of selected characters to blue | |
cell.characters(6, 7).font.color = Color.blue | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
Come formattare righe e colonne
A volte i developer devono applicare la stessa formattazione su righe o colonne. Applicare la formattazione alle celle una per una richiede spesso più tempo e non è una buona soluzione. Per risolvere questo problema, Aspose.Cells per Python via .NET offre un modo semplice e veloce, discusso in dettaglio in questo articolo.
Formattare Righe e Colonne
Aspose.Cells per Python via .NET fornisce una classe, la Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una collezione worksheets che consente l’accesso a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una collezione cells. La collezione cells fornisce una collezione rows.
Come formattare una riga
Ogni elemento nella raccolta rows rappresenta un oggetto Row. L’oggetto Row offre il metodo apply_style utilizzato per impostare la formattazione della riga. Per applicare la stessa formattazione a una riga, utilizzare l’oggetto Style. I passaggi seguenti mostrano come utilizzarlo.
- Aggiungi un oggetto Style alla classe Workbook chiamando il suo metodo create_style.
- Imposta le proprietà dell’oggetto Style per applicare le impostazioni di formattazione.
- Attiva gli attributi rilevanti per l’oggetto StyleFlag.
- Assegna l’oggetto Style configurato all’oggetto Row.
from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, 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() | |
# Obtaining the reference of the first (default) worksheet by passing its sheet index | |
worksheet = workbook.worksheets[0] | |
# Adding a new Style to the styles | |
style = workbook.create_style() | |
# Setting the vertical alignment of the text in the "A1" cell | |
style.vertical_alignment = TextAlignmentType.CENTER | |
# Setting the horizontal alignment of the text in the "A1" cell | |
style.horizontal_alignment = TextAlignmentType.CENTER | |
# Setting the font color of the text in the "A1" cell | |
style.font.color = Color.green | |
# Shrinking the text to fit in the cell | |
style.shrink_to_fit = True | |
# Setting the bottom border color of the cell to red | |
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red | |
# Setting the bottom border type of the cell to medium | |
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM | |
# Creating StyleFlag | |
styleFlag = StyleFlag() | |
styleFlag.horizontal_alignment = True | |
styleFlag.vertical_alignment = True | |
styleFlag.shrink_to_fit = True | |
styleFlag.borders = True | |
styleFlag.font_color = True | |
# Accessing a row from the Rows collection | |
row = worksheet.cells.rows[0] | |
# Assigning the Style object to the Style property of the row | |
row.apply_style(style, styleFlag) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
Come formattare una colonna
Anche la raccolta cells fornisce una raccolta columns. Ciascun elemento nella raccolta columns rappresenta un oggetto Column. Similmente a un oggetto Row, l’oggetto Column offre anche il metodo apply_style per formattare una colonna.
from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, 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() | |
# Obtaining the reference of the first (default) worksheet by passing its sheet index | |
worksheet = workbook.worksheets[0] | |
# Adding a new Style to the styles | |
style = workbook.create_style() | |
# Setting the vertical alignment of the text in the "A1" cell | |
style.vertical_alignment = TextAlignmentType.CENTER | |
# Setting the horizontal alignment of the text in the "A1" cell | |
style.horizontal_alignment = TextAlignmentType.CENTER | |
# Setting the font color of the text in the "A1" cell | |
style.font.color = Color.green | |
# Shrinking the text to fit in the cell | |
style.shrink_to_fit = True | |
# Setting the bottom border color of the cell to red | |
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red | |
# Setting the bottom border type of the cell to medium | |
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM | |
# Creating StyleFlag | |
styleFlag = StyleFlag() | |
styleFlag.horizontal_alignment = True | |
styleFlag.vertical_alignment = True | |
styleFlag.shrink_to_fit = True | |
styleFlag.borders = True | |
styleFlag.font_color = True | |
# Accessing a column from the Columns collection | |
column = worksheet.cells.columns[0] | |
# Applying the style to the column | |
column.apply_style(style, styleFlag) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
Argomenti avanzati
- Impostazioni di Allineamento
- Impostazioni dei Bordi
- Imposta Formati Condizionali di file Excel e ODS.
- Temi e Colori di Excel
- Impostazioni di Riempimento
- Impostazioni dei Caratteri
- Formattare le Celle di un Foglio di Lavoro in un Documento di Lavoro
- Implementare il Sistema di Data 1904
- Unione e separazione di celle
- Impostazioni dei numeri
- Ottenere e impostare lo stile delle celle