Formattare righe e colonne

Lavorare con le righe

Come regolare l’altezza della riga

Aspose.Cells per Python via .NET fornisce una classe, Workbook, che rappresenta un file di Microsoft Excel. La classe Workbook contiene un WorksheetCollection che consente l’accesso a ciascun foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells che rappresenta tutte le celle nel foglio di lavoro.

La raccolta Cells fornisce diversi metodi per gestire righe o colonne in un foglio di lavoro. Alcuni di questi sono discussi in seguito in maggior dettaglio.

Come impostare l’altezza di una riga

È possibile impostare l’altezza di una singola riga chiamando il metodo set_row_height della raccolta Cells. Il metodo set_row_height prende i seguenti parametri come segue:

  • riga, l’indice della riga di cui stai modificando l’altezza.
  • altezza, l’altezza della riga da applicare alla riga.
from aspose.cells import Workbook
# 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(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the height of the second row to 13
worksheet.cells.set_row_height(1, 13)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Come impostare l’altezza di tutte le righe in un foglio di lavoro

Se gli sviluppatori hanno bisogno di impostare la stessa altezza della riga per tutte le righe nel foglio di lavoro, possono farlo utilizzando la proprietà standard_height della raccolta Cells.

Esempio:

from aspose.cells import Workbook
# 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(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the height of all rows in the worksheet to 15
worksheet.cells.standard_height = 15.0
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Lavorare con colonne

Come impostare la larghezza di una colonna

Imposta la larghezza di una colonna chiamando il metodo set_column_width della raccolta Cells. Il metodo set_column_width prende i seguenti parametri:

  • colonna, l’indice della colonna di cui stai cambiando la larghezza.
  • larghezza, la larghezza di colonna desiderata.
from aspose.cells import Workbook
# 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(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the width of the second column to 17.5
worksheet.cells.set_column_width(1, 17.5)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Come impostare la larghezza della colonna in pixel

Imposta la larghezza di una colonna chiamando il metodo set_column_width_pixel della raccolta Cells. Il metodo set_column_width_pixel prende i seguenti parametri:

  • colonna, l’indice della colonna di cui stai cambiando la larghezza.
  • pixel, la larghezza di colonna desiderata in pixel.
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
outDir = RunExamples.Get_OutputDirectory()
# Load source Excel file
workbook = Workbook(sourceDir + "Book1.xlsx")
# Access first worksheet
worksheet = workbook.worksheets[0]
# Set the width of the column in pixels
worksheet.cells.set_column_width_pixel(7, 200)
workbook.save(outDir + "SetColumnWidthInPixels_Out.xlsx")

Come impostare la larghezza di tutte le colonne in un foglio di lavoro

Per impostare la stessa larghezza della colonna per tutte le colonne nel foglio di lavoro, utilizzare la proprietà standard_width della raccolta Cells.

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)
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the width of all columns in the worksheet to 20.5
worksheet.cells.standard_width = 20.5
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Argomenti avanzati