Adatta automaticamente righe e colonne

Adattamento automatico

Aspose.Cells per Python via .NET fornisce una classe Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una collezione worksheets che consente l’accesso a ciascun foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una vasta gamma di proprietà e metodi per la gestione di un foglio di lavoro. Questo articolo esamina l’utilizzo della classe Worksheet per adattare automaticamente righe o colonne.

Adatta automaticamente la riga - Semplice

L’approccio più diretto per ridimensionare automaticamente larghezza e altezza di una riga è chiamare il metodo auto_fit_row della classe Worksheet. Il metodo auto_fit_row richiede un indice di riga (della riga da ridimensionare) come parametro.

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(".")
InputPath = dataDir + "Book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Auto-fitting the 3rd row of the worksheet
worksheet.auto_fit_row(1)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()

Come adattare automaticamente la riga in un intervallo di celle

Una riga è composta da molte colonne. Aspose.Cells per Python via .NET consente agli sviluppatori di adattare automaticamente una riga in base al contenuto in un intervallo di celle all’interno della riga chiamando una versione sovraccarica del metodo auto_fit_row. Richiede i seguenti parametri:

  • Indice riga, l’indice della riga da adattare automaticamente.
  • Primo indice colonna, l’indice della prima colonna della riga.
  • Ultimo indice colonna, l’indice dell’ultima colonna della riga.

Il metodo auto_fit_row controlla i contenuti di tutte le colonne nella riga e quindi adatta automaticamente la 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(".")
InputPath = dataDir + "Book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Auto-fitting the 3rd row of the worksheet
worksheet.auto_fit_row(1, 0, 5)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()

Come adattare automaticamente la colonna in un intervallo di celle

Una colonna è composta da molte righe. È possibile adattare automaticamente una colonna in base al contenuto in un intervallo di celle nella colonna chiamando una versione sovraccarica del metodo auto_fit_column che richiede i seguenti parametri:

  • Indice colonna, l’indice della colonna da adattare automaticamente.
  • Primo indice riga, l’indice della prima riga della colonna.
  • Ultimo indice di riga, l’indice dell’ultima riga della colonna.

Il metodo auto_fit_column controlla il contenuto di tutte le righe nella colonna e quindi adatta automaticamente la larghezza della colonna.

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(".")
InputPath = dataDir + "Book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Auto-fitting the Column of the worksheet
worksheet.auto_fit_column(4, 4, 6)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()

Come adattare automaticamente le righe per le celle unite

Con Aspose.Cells for Python via .NET è possibile adattare automaticamente le righe anche per le celle unite utilizzando l’API AutoFitterOptions. La classe AutoFitterOptions fornisce la proprietà auto_fit_merged_cells_type che può essere utilizzata per adattare automaticamente le righe per le celle unite. auto_fit_merged_cells_type accetta l’enumerazione AutoFitMergedCellsType che ha i seguenti membri.

  • NONE: Ignora le celle unite.
  • FIRST_LINE: Espande solo l’altezza della prima riga.
  • LAST_LINE: Espande solo l’altezza dell’ultima riga.
  • EACH_LINE: Espande solo l’altezza di ogni riga.
from aspose.cells import AutoFitMergedCellsType, AutoFitterOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiate a new Workbook
wb = Workbook()
# Get the first (default) worksheet
_worksheet = wb.worksheets[0]
# Create a range A1:B1
range = _worksheet.cells.create_range(0, 0, 1, 2)
# Merge the cells
range.merge()
# Insert value to the merged cell A1
_worksheet.cells.get(0, 0).value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end"
# Create a style object
style = _worksheet.cells.get(0, 0).get_style()
# Set wrapping text on
style.is_text_wrapped = True
# Apply the style to the cell
_worksheet.cells.get(0, 0).set_style(style)
# Create an object for AutoFitterOptions
options = AutoFitterOptions()
# Set auto-fit for merged cells
options.auto_fit_merged_cells_type = AutoFitMergedCellsType.EACH_LINE
# Autofit rows in the sheet(including the merged cells)
_worksheet.auto_fit_rows(options)
# Save the Excel file
wb.save(outputDir + "AutofitRowsforMergedCells.xlsx")

Importante sapere

Argomenti avanzati