Excel dosyasının Satırları ve Sütunları Eklemek ve Silmek
Giriş
Sıfırdan yeni bir çalışma sayfası oluştururken veya mevcut bir çalışma sayfası üzerinde çalışırken, daha fazla veri eklemek için ekstra satırlar veya sütunlar eklememiz gerekebilir. Tersine, çalışma sayfasının belirli pozisyonlarından satırları veya sütunları silebiliriz. Bu gereksinimleri karşılamak için Aspose.Cells for Python via .NET, aşağıda tartışılan çok basit bir sınıf ve yöntem seti sağlar.
Satırları ve Sütunları Yönetmek
Aspose.Cells for Python via .NET, bir Microsoft Excel dosyasını temsil eden bir sınıf Workbook sağlar. Workbook sınıfı, bir Excel dosyasındaki her çalışma sayfasına erişim sağlayan bir worksheets koleksiyonunu içerir. Bir çalışma sayfası, Worksheet sınıfı tarafından temsil edilir. Worksheet sınıfı, çalışma sayfasındaki tüm hücreleri temsil eden bir cells koleksiyonu sağlar.
cells koleksiyonu, bir çalışma sayfasındaki satırları ve sütunları yönetmek için birkaç yöntem sağlar. Bunlardan bazıları aşağıda tartışılmıştır.
Satırları ve Sütunları Eklemek
Bir Satır Nasıl Eklenir
Yeni bir satırı çalışma sayfasına herhangi bir konumda eklemek için insert_row koleksiyonunun cells yöntemini çağırarak yapılır. Yeni satırın ekleneceği satırın indeksini alan insert_row yöntemi çağrılır.
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() |
Birkaç Satır Nasıl Eklenir
Birden çok satırı çalışma sayfasına eklemek için insert_rows koleksiyonunun cells yöntemini çağırın. insert_rows yöntemi iki parametre alır:
- Satır indeksi, yeni satırların ekleneceği satırın indeksi.
- Satır sayısı, eklenmesi gereken toplam satır sayısı.
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() |
Biçimlendirme Seçenekleriyle Bir Satır Nasıl Eklenir
Biçimlendirme seçenekleriyle bir satır eklemek için, bir parametre olarak InsertOptions kullanan insert_rows aşırı yüklemesini kullanın. copy_format_type sınıfının InsertOptions özelliğini CopyFormatType Sıralama ile ayarlayın. CopyFormatType Sıralama’nın aşağıda listelenen üç üyesi bulunmaktadır.
- SAME_AS_ABOVE: Üstteki satırın biçimini aynı şekilde biçimlendirir.
- SAME_AS_BELOW: Altındaki satırın biçimini aynı şekilde biçimlendirir.
- TEMİZLE: Biçimlendirmeyi temizler.
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() |
Bir Sütun Nasıl Eklenir
Geliştiriciler, herhangi bir konumda çalışma sayfasına bir sütun ekleyebilirler, bunun için insert_column koleksiyonunun cells yöntemini çağırarak yapılır. insert_column yöntemi çağrılır.
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() |
Satırları ve Sütunları Silmek
Birden Fazla Satır Nasıl Silinir
Çalışma sayfasından birden fazla satır silmek için, cells koleksiyonunun delete_rows yöntemini çağırın. delete_rows yöntemi iki parametre alır:
- Satır endeksi, satırların silineceği başlangıç satırının endeksi.
- satır sayısı, silinmesi gereken toplam satır sayısı
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() |
Bir Sütunu Nasıl Silebilirsiniz
Herhangi bir konumda faaliyet sayfasından bir sütun silmek için, Cells koleksiyonunun delete_column yöntemini çağırın. delete_column yöntemi silinecek sütunun endeksini alır.
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() |