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ışsayfalar Ekleme

Programlı olarak yeni bir Excel dosyası oluşturmak için:

  1. Workbook sınıfının bir nesnesini oluşturun.
  2. 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.
  3. Bir çalışma sayfası referansı edinin.
  4. Çalışma sayfalarında çalışma yapın.
  5. Yeni çalışma sayfaları ile Excel dosyasını Workbook sınıfının Save methodunu çağırarak kaydedin.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet 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ı Çalışsayfalara Çalış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.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet 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ını Kullanarak Çalışsayfalara Erişme

Adını veya dizinini belirterek herhangi bir çalışma sayfasına erişin.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing a worksheet using its sheet name
Worksheet worksheet = workbook.Worksheets["Sheet1"];
Cell cell = worksheet.Cells["A1"];
Console.WriteLine(cell.Value);

Sayfa Adını Kullanarak Çalışsayfaları Kaldırma

Dosyadan çalışma sayfalarını kaldırmak için, WorksheetCollection sınıfının RemoveAt methodunu çağırın. Belirli bir çalışma sayfasını kaldırmak için RemoveAt methoduna çalışma sayfasının adını geçirin.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Removing a worksheet using its sheet name
workbook.Worksheets.RemoveAt("Sheet1");
// Save workbook
workbook.Save(dataDir + "output.out.xls");

Sayfa Dizinini Kullanarak Çalışma Sayfalarını Kaldırma

Çalışma sayfalarını adı bilindiğinde adıyla kaldırmak iyi çalışır. Eğer çalışma sayfasının adını bilmiyorsanız, çalışma sayfasının adı yerine dizinini alan RemoveAt methodunun aşırı yüklenmiş bir sürümünü kullanın.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Removing a worksheet using its sheet index
workbook.Worksheets.RemoveAt(0);
// Save workbook
workbook.Save(dataDir + "output.out.xls");

Sayfaları Etkinleştirme ve Çalışma Sayfasında Aktif 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ı Aktifleş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.ActiveSheetIndex özelliği, bir çalışma kitabında etkin sayfayı ayarlamak için kullanışlıdır. Benzer şekilde, Aspose.Cells.Worksheet.ActiveCell ö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.FirstVisibleRow ve Aspose.Cells.Worksheet.FirstVisibleColumn ö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.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.Worksheets[0];
// Get the cells in the worksheet.
Cells cells = worksheet1.Cells;
// Input data into B2 cell.
cells[1, 1].PutValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.Worksheets.ActiveSheetIndex = 0;
// Set B2 cell as an active cell in the worksheet.
worksheet1.ActiveCell = "B2";
// Set the B column as the first visible column in the worksheet.
worksheet1.FirstVisibleColumn = 1;
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.FirstVisibleRow = 1;
// Save the excel file.
workbook.Save(dataDir + "output.xls");

Gelişmiş Konular