Formatera rader och kolumner
Arbeta med rader
Hur man justerar radhöjden
Aspose.Cells for Python via .NET tillhandahåller en klass, Workbook, som representerar en Microsoft Excel-fil. Klassen Workbook innehåller en WorksheetCollection som ger åtkomst till varje kalkylblad i Excel-filen. Ett kalkylblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller en Cells som representerar alla celler i kalkylbladet.
Cells samlingen tillhandahåller flera metoder för att hantera rader eller kolumner i ett kalkylblad. Några av dessa diskuteras nedan mer detaljerat.
Hur man ställer in höjden på en rad
Det är möjligt att ställa in höjden på en enskild rad genom att anropa set_row_height metoden på Cells samlingen. Metoden set_row_height tar följande parametrar som följer:
- rad, indexet för den rad du ändrar höjden på.
- höjd, radhöjden att tillämpa på raden.
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() |
Hur man ställer in höjden på alla rader i ett kalkylblad
Om utvecklare behöver ange samma radhöjd för alla rader i kalkylbladet kan de göra det genom att använda Cells egenskapen för standard_height samlingen.
Exempel:
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() |
Arbeta med kolumner
Hur man ställer in bredden på en kolumn
Ställ in bredden på en kolumn genom att anropa set_column_width metoden på Cells samlingen. Metoden set_column_width tar följande parametrar:
- kolumn, index för den kolumn som du ändrar bredden på.
- bredd, önskad kolumnbredd.
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() |
Hur man ställer in kolumnbredd i pixlar
Ange kolumnbredden genom att ringa in samlingens Cells metoden set_column_width_pixel. Metoden set_column_width_pixel tar följande parametrar:
- kolumn, index för den kolumn som du ändrar bredden på.
- pixlar, önskad kolumnbredd i pixlar.
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") |
Hur man ställer in bredden på alla kolumner i en arbetsbok
För att ställa in samma kolumnbredd för alla kolumner i arbetsboken, använd samlingens Cells egenskap standard_width.
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() |
Fortsatta ämnen
- AutoAnpassa rader och kolumner
- Konvertera Text till Kolumner med Aspose.Cells
- Kopiera rader och kolumner
- Ta bort tomma rader och kolumner i en arbetsbok
- Gruppering och avgruppering av rader och kolumner
- Dölja och visa rader och kolumner
- Infoga eller ta bort rader i en Excel-arbetsbok
- Infoga och ta bort rader och kolumner i Excel-fil
- Ta bort dubblettrader i ett kalkylblad
- Uppdatera referenser i andra arbetsblad samtidigt som tomma kolumner och rader tas bort i ett arbetsblad