Automatisk anpassning av rader och kolumner

Autostorlek

Aspose.Cells för Python via .NET tillhandahåller en Workbook-klass som representerar en Microsoft Excel-fil. Workbook-klassen innehåller en worksheets-samling som tillåter åtkomst till varje kalkylblad i en Excel-fil. Ett kalkylblad representeras av klassen Worksheet. Worksheet-klassen tillhandahåller ett brett utbud av egenskaper och metoder för att hantera ett kalkylblad. Denna artikel tittar på att använda Worksheet-klassen för att automatiskt justera rader eller kolumner.

AutoFit Row - Enkelt

Det mest raka tillvägagångssättet för att automatiskt justera bredd och höjd för en rad är att anropa klassen Worksheet metoden auto_fit_row. Metoden auto_fit_row tar en radindex (av raden som ska ändras) som parameter.

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()

Hur man Autofitrad i ett område av celler

En rad består av många kolumner. Aspose.Cells för Python via .NET tillåter utvecklare att automatiskt anpassa en rad baserad på innehållet i en cellräckvidd inom raden genom att anropa en överbelastad version av auto_fit_row metoden. Den tar följande parametrar:

  • Radindex, index för raden som ska autofit.
  • Första kolumnindex, index för radens första kolumn.
  • Sista kolumnindex, index för radens sista kolumn.

Metoden auto_fit_row kontrollerar innehållet i alla kolumner i raden och anpassar sedan raden automatiskt.

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()

Hur man Autofittar kolumn i ett område av celler

En kolumn består av många rader. Det är möjligt att automatiskt justera en kolumn baserad på innehållet i ett område av celler i kolumnen genom att anropa en överlagrad version av metoden auto_fit_column som tar följande parametrar:

  • Kolumnindex, index för kolumnen som ska autofit.
  • Första radindex, index för kolumnens första rad.
  • Sista radindex, index för kolumnens sista rad.

Metoden auto_fit_column kontrollerar innehållet i alla rader i kolumnen och anpassar sedan kolumnen automatiskt.

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()

Hur man Autofittar rader för sammanfogade celler

Med Aspose.Cells for Python via .NET är det möjligt att autofit-rader även för celler som har sammanfogats med hjälp av AutoFitterOptions API. AutoFitterOptions klass tillhandahåller auto_fit_merged_cells_type egenskap som kan användas för att autofit-rader för sammanfogade celler. auto_fit_merged_cells_type accepterar AutoFitMergedCellsType uppräkningsbar som har följande medlemmar.

  • INGEN: Ignorera sammanfogade celler.
  • FÖRSTA_RADEN: Utvidgar bara höjden på första raden.
  • SISTA_RADEN: Utvidgar bara höjden på sista raden.
  • VARJE_RAD: Utvidgar bara höjden på varje rad.
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")

Viktigt att veta

Fortsatta ämnen