Excel dosyasına Satır ve Sütun Ekleme ve Silme

Giriş

Sıfırdan yeni bir çalışma sayfası oluştururken veya mevcut bir çalışma sayfası üzerinde çalışırken, daha fazla veriyi barındırmak için fazladan satır veya sütun eklememiz gerekebilir. Tersine, çalışma sayfasında belirtilen konumlardaki satırları veya sütunları da silmemiz gerekebilir. Bu gereklilikleri yerine getirmek için Aspose.Cells, aşağıda tartışılan çok basit bir sınıflar ve yöntemler seti sağlar.

Satırları ve Sütunları Yönet

Aspose.Cells bir sınıf sağlarÇalışma kitabı , bu bir Microsoft Excel dosyasını temsil eder. buÇalışma kitabı sınıf bir içerirçalışma sayfaları bir Excel dosyasındaki her çalışma sayfasına erişim sağlayan koleksiyon. Bir çalışma sayfası şununla temsil edilir:Çalışma kağıdı sınıf. buÇalışma kağıdı sınıf bir sağlarCellsçalışma sayfasındaki tüm hücreleri temsil eden koleksiyon.

buCellskoleksiyon, bir çalışma sayfasındaki satırları ve sütunları yönetmek için çeşitli yöntemler sağlar. Bunlardan bazıları aşağıda tartışılmaktadır.

Satır ve Sütun Ekleme

Satır Ekle

çağırarak çalışma sayfasına herhangi bir konumda bir satır ekleyin.Satır Ekle yöntemiCells Toplamak. buSatır Eklemethod yeni satırın ekleneceği satırın indeksini alı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);
// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting a row into the worksheet at 3rd position
worksheet.Cells.InsertRow(2);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Birden Fazla Satır Ekle

Bir çalışma sayfasına birden çok satır eklemek içinSatır Ekle yöntemiCells Toplamak. buSatır Ekleyöntem iki parametre alır:

  • Satır dizini, yeni satırların ekleneceği satırın dizini.
  • Satır sayısı, eklenmesi gereken toplam satır sayısı.
// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting 10 rows into the worksheet starting from 3rd row
worksheet.Cells.InsertRows(2, 10);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Biçimlendirmeyle Satır Ekleme

Biçimlendirme seçenekleriyle bir satır eklemek içinSatır Ekleaşırı yüklenmeEkleme Seçenekleri parametre olarak. Yı kurKopya BiçimiTürü mülkiyetEkleme Seçenekleri ile sınıfKopya BiçimiTürü Numaralandırma. buKopya BiçimiTürüNumaralandırmanın aşağıda listelendiği gibi üç üyesi vardır.

  • SameAsAbove: Satırı yukarıdaki satırla aynı şekilde biçimlendirir.
  • SameAsBelow: Satırı aşağıdaki satırla aynı şekilde biçimlendirir.
  • Temizle: Biçimlendirmeyi temizler.
// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting Formatting options
InsertOptions insertOptions = new InsertOptions();
insertOptions.CopyFormatType = CopyFormatType.SameAsAbove;
// Inserting a row into the worksheet at 3rd position
worksheet.Cells.InsertRows(2, 1, insertOptions);
// Saving the modified Excel file
workbook.Save(dataDir + "InsertingARowWithFormatting.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Sütun Ekle

Geliştiriciler ayrıca, çalışma sayfasına herhangi bir konumda şunu çağırarak bir sütun ekleyebilir:Sütun Ekle yöntemiCellsToplamak. buSütun Eklemethod yeni kolonun ekleneceği kolonun indeksini alı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);
// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Inserting a column into the worksheet at 2nd position
worksheet.Cells.InsertColumn(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ı Sil

Birden Çok Satırı Sil

Bir çalışma sayfasından birden çok satırı silmek içinSatırları Sil yöntemiCells Toplamak. buSatırları Silyöntem iki parametre alır:

  • Satır dizini, satırların silineceği satırın dizini.
  • Satır sayısı, silinmesi gereken toplam satır sayısı.
// 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.xlsx", FileMode.OpenOrCreate);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Deleting 10 rows from the worksheet starting from 3rd row
worksheet.Cells.DeleteRows(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 Sil

Herhangi bir konumdaki çalışma sayfasından bir sütunu silmek içinSütunu Sil yöntemiCells Toplamak. buSütunu SilYöntem, silinecek sütunun dizinini alı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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Deleting a column from the worksheet at 5th position
worksheet.Cells.DeleteColumn(4);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();