Infoga och ta bort rader och kolumner i Excelfil

Introduktion

Oavsett om du skapar en ny arbetsbok från grunden eller arbetar med en befintlig arbetsbok kan vi behöva lägga till extra rader eller kolumner för att rymma mer data. Å andra sidan kan det också hända att vi behöver ta bort rader eller kolumner från angivna positioner i arbetsboken. För att uppfylla dessa krav tillhandahåller Aspose.Cells for Python via .NET en mycket enkel uppsättning av klasser och metoder, som diskuteras nedan.

Hantera rader och kolumner

Aspose.Cells for Python via .NET tillhandahåller en klass Workbook, som representerar en Microsoft Excel-fil. Klassen Workbook innehåller en worksheets samling som möjliggör åtkomst till varje arbetsblad i en Excel-fil. Ett arbetsblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller en cells samling som representerar alla celler i arbetsbladet.

Samlingen cells tillhandahåller flera metoder för att hantera rader och kolumner i ett arbetsblad. Några av dessa diskuteras nedan.

Infoga rader och kolumner

Hur man infogar en rad

Infoga en rad i arbetsbladet på valfri plats genom att anropa insert_row-metoden i cells-samlingen. Metoden insert_row tar indexet för raden där den nya raden ska infogas.

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]
# Inserting a row into the worksheet at 3rd position
worksheet.cells.insert_row(2)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Hur man infogar flera rader

För att infoga flera rader i ett arbetsblad, ring insert_rows-metoden i cells-samlingen. Metoden insert_rows tar två parametrar:

  • Radindex, index för raden från vilken de nya raderna ska infogas.
  • Antal rader, det totala antalet rader som ska infogas.
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]
# Inserting 10 rows into the worksheet starting from 3rd row
worksheet.cells.insert_rows(2, 10)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Hur man infogar en rad med formatering

För att infoga en rad med formateringsalternativ, använd insert_rows-överbelastningen som tar InsertOptions som parameter. Ange copy_format_type-egenskapen i InsertOptions-klassen med CopyFormatType-uppräkningen. CopyFormatType-uppräkningen har tre medlemmar som listas nedan.

  • SAMMA_SOM_OVAN: Formaterar raden på samma sätt som ovanstående rad.
  • SAMMA_SOM_NEDAN: Formaterar raden på samma sätt som nedanstående rad.
  • RENSA: Rensar formateringen.
from aspose.cells import CopyFormatType, InsertOptions, 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 Formatting options
insertOptions = InsertOptions()
insertOptions.copy_format_type = CopyFormatType.SAME_AS_ABOVE
# Inserting a row into the worksheet at 3rd position
worksheet.cells.insert_rows(2, 1, insertOptions)
# Saving the modified Excel file
workbook.save(dataDir + "InsertingARowWithFormatting.out.xls")
# Closing the file stream to free all resources
fstream.close()

Hur man infogar en kolumn

Utvecklare kan också infoga en kolumn i arbetsbladet på valfri plats genom att anropa insert_column-metoden i cells-samlingen. Metoden insert_column tar indexet för kolumnen där den nya kolumnen ska infogas.

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]
# Inserting a column into the worksheet at 2nd position
worksheet.cells.insert_column(1)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Ta bort rader och kolumner

Hur man tar bort flera rader

För att ta bort flera rader från ett arbetsblad, ring delete_rows-metoden i cells-samlingen. Metoden delete_rows tar två parametrar:

  • Radindex, index för raden från vilken raderna ska tas bort.
  • Antal rader, det totala antalet rader som ska tas bort.
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.xlsx", "wb")
# 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]
# Deleting 10 rows from the worksheet starting from 3rd row
worksheet.cells.delete_rows(2, 10)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()

Hur man tar bort en kolumn

För att ta bort en kolumn från arbetsbladet på valfri plats, ring delete_column-metoden i Cells-samlingen. Metoden delete_column tar indexet för kolumnen som ska tas bort.

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.xlsx", "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]
# Deleting a column from the worksheet at 5th position
worksheet.cells.delete_column(4)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()