Microsoft Excel dosyalarının çalışma sayfalarını yönetme.
Aspose.Cells, bir Excel dosyasını temsil eden bir Workbook sınıfını sağlar. Workbook sınıfı, Excel dosyasındaki her çalışma sayfasına erişim sağlayan bir worksheets koleksiyonuna sahiptir.
Bir çalışma sayfası, bir Worksheet sınıfı tarafından temsil edilir. Worksheet sınıfı, çalışma sayfalarını yönetmek için geniş bir yelpazede özellikler ve yöntemler sağlar.
Yeni bir Excel Dosyasına Çalışma Sayfaları Ekleme
Programlı olarak yeni bir Excel dosyası oluşturmak için:
- Workbook sınıfının bir nesnesini oluşturun.
- add methodunu WorksheetCollection sınıfın serisine çağırın. Boş bir çalışma sayfası otomatik olarak Excel dosyasına eklenir. Yeni çalışma sayfasının dizinini worksheets koleksiyonuna geçirerek referans alınabilir.
- Bir çalışma sayfası referansı edinin.
- Çalışma sayfalarında çalışma yapın.
- Yeni çalışma sayfaları ile Excel dosyasını Workbook sınıfının save methodunu çağırarak kaydedin.
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) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Setting the name of the newly added worksheet | |
worksheet.name = "My Worksheet" | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls") |
Tasarımcı Elektronik Tablo için Çalışma Sayfaları Ekleme
Tasarımcı bir e-tablosuna çalışma sayfaları eklemenin süreci, yeni bir çalışma sayfası eklemekle aynıdır. Tek fark, Excel dosyasının zaten mevcut olması ve çalışma sayfaları eklenmeden önce açılması gerektiği. Bir tasarımcı e-tablosu, Workbook sınıfı tarafından açılabilir.
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) | |
# Adding a new worksheet to the Workbook object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Setting the name of the newly added worksheet | |
worksheet.name = "My Worksheet" | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xlsx") |
Sayfa Adı kullanarak Çalışma Sayfalarına Erişme
Adını veya dizinini belirterek herhangi bir çalışma sayfasına erişin.
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") | |
# Instantiating a Workbook object | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing a worksheet using its sheet name | |
worksheet = workbook.worksheets.get("Sheet1") | |
cell = worksheet.cells.get("A1") | |
print(cell.value) |
Sayfa Adını Kullanarak Çalışma Sayfalarını Kaldırma
Dosyadan çalışma sayfalarını kaldırmak için, WorksheetCollection sınıfının remove_by_name methodunu çağırın. Belirli bir çalışma sayfasını kaldırmak için remove_by_name methoduna çalışma sayfasının adını geçirin.
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) | |
# Removing a worksheet using its sheet name | |
workbook.worksheets.remove_by_name("Sheet1") | |
# Save workbook | |
workbook.save(dataDir + "output.out.xls") |
Sayfa İndeksini Kullanarak Çalışma Sayfalarını Kaldırma
Çalışma sayfasının adı bilindiğinde çalışır. Sayfa adını bilmiyorsanız, sayfa adı yerine çalışma sayfasının sayfa indeksini alan remove_by_index yöntemini kullanın.
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) | |
# Removing a worksheet using its sheet index | |
workbook.worksheets.remove_by_index(0) | |
# Save workbook | |
workbook.save(dataDir + "output.out.xls") |
Sayfaları Etkinleştirme ve Çalışma Sayfasında Etkin Bir Hücre Yapma
Bazen, bir kullanıcı Excel’de bir Microsoft Excel dosyasını açtığında belirli bir çalışma sayfasının etkin ve görüntülenen olmasını istersiniz. Benzer şekilde, belirli bir hücreyi etkinleştirmek ve kaydırmalı çubukların etkin hücreyi göstermesini isteyebilirsiniz. Aspose.Cells, tüm bu görevleri yapabilir.
Bir etkin sayfa, üzerinde çalıştığınız bir sayfadır: sekmelerdeki etkin sayfanın adı varsayılan olarak kalın harfle yazılıdır.
Bir etkin hücre, seçilen hücredir, verinin başlatıldığı hücredir. Aynı zamanda yalnızca bir hücre etkindir. Etkin hücre, kalın bir kenarlıkla vurgulanır.
Sayfaları Etkinleştirme ve Bir Hücreyi Etkin Yapma
Aspose.Cells, bir sayfayı ve bir hücreyi aktifleştirmek için özel API çağrıları sağlar. Örneğin, Aspose.Cells.WorksheetCollection.active_sheet_index özelliği, bir çalışma kitabında etkin sayfayı ayarlamak için kullanışlıdır. Benzer şekilde, Aspose.Cells.Worksheet.active_cell özelliği, çalışma sayfasında etkin bir hücreyi ayarlamak ve almak için kullanılır.
Yatay veya dikey kaydırmalı çubukların belirli verileri göstermek istediğiniz satır ve sütun dizin konumuna geldiğinden emin olmak için, Aspose.Cells.Worksheet.first_visible_row ve Aspose.Cells.Worksheet.first_visible_column özelliklerini kullanın.
Aşağıdaki örnek, bir çalışma sayfasını etkinleştirmenin ve üzerinde etkin bir hücre oluşturmanın nasıl yapıldığını gösterir. Oluşturulan çıktıda, kaydırmalı çubuklar, ilk görünür satır ve sütun olarak 2. satır ve 2. sütunu yapmak için kaydı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(".") | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get the first worksheet in the workbook. | |
worksheet1 = workbook.worksheets[0] | |
# Get the cells in the worksheet. | |
cells = worksheet1.cells | |
# Input data into B2 cell. | |
cells.get(1, 1).put_value("Hello World!") | |
# Set the first sheet as an active sheet. | |
workbook.worksheets.active_sheet_index = 0 | |
# Set B2 cell as an active cell in the worksheet. | |
worksheet1.active_cell = "B2" | |
# Set the B column as the first visible column in the worksheet. | |
worksheet1.first_visible_column = 1 | |
# Set the 2nd row as the first visible row in the worksheet. | |
worksheet1.first_visible_row = 1 | |
# Save the excel file. | |
workbook.save(dataDir + "output.xls") |