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, aşağıda tartışılan çok basit bir sınıf ve yöntem seti sağlar.
Satır/Sütun Yönetimi
Aspose.Cells, bir Microsoft Excel dosyasını temsil eden Workbook sınıfını sağlar. Workbook sınıfı, bir Excel dosyasındaki her bir çalışma sayfasına erişime izin veren bir WorksheetCollection 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 açıklanmıştır.
Bir Satır Nasıl Eklenir
Yeni bir satır eklemek için insertRows yöntemini çağırın. Bu yöntem, yeni satırın ekleneceği satırın indeksini ilk argüman olarak ve eklenecek satırların sayısını ikinci argüman olarak alır.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(InsertingARow.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.getCells().insertRows(2, 1); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "InsertingARow_out.xls"); |
Birkaç Satır Nasıl Eklenir
Çoklu satır eklemek için insertRows yöntemini çağırın. Bu yöntem iki parametre alır:
- Satır endeksi: Yeni satırların ekleneceği satırın endeksi.
- 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-Java | |
String dataDir = Utils.getSharedDataDir(InsertingMultipleRows.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet.getCells().insertRows(2, 10); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "InsertingMultipleRows_out.xls"); |
Biçimlendirme Seçenekleriyle Bir Satır Nasıl Eklenir
Biçimlendirme seçenekleriyle birlikte satır eklemek için, insertRows aşırı yüklemesini kullanın ve parametre olarak InsertOptions verin. InsertOptions sınıfının CopyFormatType özelliğini CopyFormatType Enum’ı ile ayarlayın. CopyFormatType Enum’ında aşağıda listelenen üç üye vardır.
- SAME_AS_ABOVE: Satırı yukarıdaki satırla aynı biçimde biçimlendirir.
- SAME_AS_BELOW: Satırı aşağıdaki satırla aynı biçimde biçimlendirir.
- TEMİZLE: Biçimlendirmeyi temizler.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(InsertingARowWithFormatting.class) + "RowsAndColumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Setting Formatting options | |
InsertOptions insertOptions = new InsertOptions(); | |
insertOptions.setCopyFormatType(CopyFormatType.SAME_AS_ABOVE); | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.getCells().insertRows(2, 1, insertOptions); | |
// Saving the modified Excel file | |
workbook.save(dataDir + "InsertingARowWithFormatting_out.xlsx"); |
Bir Satır Nasıl Silinir
Herhangi bir konumda satırı silmek için, deleteRows yöntemini çağırın. Bu yöntem iki parametre alır:
- Satır endeksi: Silinecek satırların endeksi.
- 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(DeleteARow.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Deleting 3rd row from the worksheet | |
worksheet.getCells().deleteRows(2, 1, true); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "DeleteARow_out.xls"); |
Birden Fazla Satır Nasıl Silinir
Bir çalışma sayfasından çoklu satır silmek için deleteRows yöntemini çağırın. Bu yöntem iki parametre alır:
- Satır endeksi: Silinecek satırların endeksi.
- 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(DeleteMultipleRows.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet.getCells().deleteRows(2, 10, true); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "DeleteMultipleRows_out.xls"); |
Bir veya Birden Fazla Sütunun Nasıl Eklenir
Geliştiriciler, herhangi bir konuma sütun eklemek için insertColumns yöntemini çağırabilir. Bu yöntem iki parametre alır:
- Sütun endeksi, sütunun ekleneceği sütunun endeksi
- Sütun sayısı, eklenmek istenen toplam sütun sayısı
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(InsertingAColumn.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Inserting a column into the worksheet at 2nd position | |
worksheet.getCells().insertColumns(1, 1); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "InsertingAColumn_out.xls"); |
Bir Sütunu Nasıl Silebilirsiniz
Herhangi bir konumda çalışma sayfasından sütunu silmek için, deleteColumns yöntemini çağırın. Bu yöntem aşağıdaki parametreleri alır:
- Sütun endeksi: Sütunun silineceği sütunun endeksi.
- Sütun sayısı: Silinmesi gereken toplam sütun sayısı.
- Referans Güncelleme: Başka çalışma sayfalarındaki referansları güncellemek için bir Boolean parametresi.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(DeleteAColumn.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Deleting a column from the worksheet at 2nd position | |
worksheet.getCells().deleteColumns(1, 1, true); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "DeleteAColumn_out.xls"); |