Satır ve Sütunları Kopyalama

Giriş

Bazen, bir çalışma sayfasında tüm çalışma sayfasını kopyalamadan satır ve sütunları kopyalamanız gerekir. Aspose.Cells ile, çalışma kitapları arasında veya içinde satır ve sütunları kopyalamak mümkündür.

Bir satır (veya sütun) kopyalandığında, içindeki veriler, güncellenmiş referanslarla formülleri - ve değerleri, yorumları, biçimlendirmeyi, gizli hücreleri, görüntüleri ve diğer çizim nesnelerini içeren veriler de kopyalanır.

Microsoft Excel ile Satır ve Sütunları Kopyalama

  1. Kopyalamak istediğiniz satırı veya sütunu seçin.
  2. Satır veya sütunları kopyalamak için Standart araç çubuğunda Kopyala‘yı tıklayın veya CTRL+C‘ye basın.
  3. Kopyalamak istediğiniz seçimin altında veya sağındaki bir satır veya sütunu seçin.
  4. Satır veya sütunları kopyalarken, Ekle menüsünde Kopyalanan Hücreler‘i tıklayın.

Tek Satır Kopyalama

Aspose.Cells, Cells sınıfının copyRow metodunu sağlar. Bu metod, kaynak satırdan hedef satıra formüller, değerler, yorumlar, hücre biçimleri, gizli hücreler, resimler ve diğer çizim nesneleri dahil olmak üzere tüm veri türlerini kopyalar.

copyRow metodu şu parametreleri alır:

  • kaynak Cells nesnesi,
  • kaynak satır dizini, ve
  • hedef satır dizini.

Bu metodu bir sayfa içinde bir satırı kopyalamak veya başka bir sayfaya kopyalamak için kullanın. copyRow metodu Microsoft Excel’de kullanılan şekilde çalışır. Örneğin, hedef satırın yüksekliğini açıkça ayarlamak zorunda değilsiniz, bu değer de kopyalanır.

Aşağıdaki örnek, bir çalışsayfasında bir satır kopyalamayı gösterir. Bir şablon Microsoft Excel dosyası kullanır ve ikinci satırı (veri, biçimlendirme, yorumlar, resimler vb. ile birlikte) kopyalar ve aynı çalışsayfadaki 12. satıra yapıştırır.

// 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(CopyingRows.class) + "rows_cloumns/";
// Create a new Workbook.
Workbook excelWorkbook = new Workbook(dataDir + "book1.xls");
// Get the first worksheet in the workbook.
Worksheet wsTemplate = excelWorkbook.getWorksheets().get(0);
// Copy the second row with data, formating, images and drawing objects to the 12th row in the worksheet.
wsTemplate.getCells().copyRow(wsTemplate.getCells(), 2, 10);
// Save the excel file.
excelWorkbook.save(dataDir + "CopyingRows_out.xls");
// Print message
System.out.println("Row and Column copied successfully.");

Aşağıdaki kod çalıştırıldığında aşağıdaki çıktı üretilir.

Satır en yüksek hassasiyet ve doğrulukla kopyalanır

todo:image_alt_text

Birden Fazla Satır Kopyalama

Cells.copyRows yöntemini kullanarak bir tamsayı türünde ek bir parametre kullanarak yeni bir hedefe birden çok satır kopyalayabilirsiniz.

Aşağıda, 3 veri satırı içeren giriş elektronik tablosunun bir görüntüsü bulunmakta, aşağıdaki kod örneği tüm 3 satırı 7. satırdan başlayarak yeni bir konuma kopyalar.

todo:image_alt_text

// 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.getDataDir(CopyingMultipleRows.class);
// Create an instance of Workbook class by loading the existing spreadsheet
Workbook workbook = new Workbook(dataDir + "aspose-sample.xlsx");
// Get the cells collection of worksheet by name Rows
Cells cells = workbook.getWorksheets().get("Rows").getCells();
// Copy the first 3 rows to 7th row
cells.copyRows(cells, 0, 6, 3);
// Save the result on disc
workbook.save(dataDir + "output.xlsx");

Yukarıdaki kod örneği yürütüldükten sonra elde edilen elektronik tablo görünümü aşağıda verilmektedir.

todo:image_alt_text

Tek Sütun Kopyalama

Aspose.Cells, Cells sınıfının copyColumn metodunu sağlar, bu metod kaynak sütundan hedef sütuna formüller (güncellenmiş referanslarla) ve değerler, yorumlar, hücre biçimleri, gizli hücreler, resimler ve diğer çizim nesneleri dahil olmak üzere tüm veri türlerini kopyalar.

copyColumn metodu şu parametreleri alır:

  • kaynak Cells nesnesi,
  • kaynak sütun indeksi ve
  • hedef sütun indeksi.

copyColumn metodunu bir sayfa içinde bir sütunu kopyalamak veya başka bir sayfaya kopyalamak için kullanın.

Bu örnek, bir çalışma sayfasından bir sütunu kopyalar ve başka bir iş kitabındaki bir çalışma sayfasına yapıştırır.

Bir çalışma kitabından bir sütun diğerine kopyalanır

todo:image_alt_text

// 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(CopyingColumns.class) + "rows_cloumns/";
// Create a new Workbook.
Workbook excelWorkbook = new Workbook(dataDir + "book1.xls");
// Get the first worksheet in the workbook.
Worksheet wsTemplate = excelWorkbook.getWorksheets().get(0);
// Copy the first column from the first worksheet of the first workbook into the first worksheet of the second workbook.
wsTemplate.getCells().copyColumn(wsTemplate.getCells(), 1, 4);
// Save the excel file.
excelWorkbook.save(dataDir + "CopyingColumns_out.xls");
// Print message
System.out.println("Row and Column copied successfully.");

Birden Çok Sütunun Kopyalanması

Cells.copyRows yöntemine benzer şekilde, Aspose.Cells API’leri ayrıca birden çok kaynak sütunu yeni bir konuma kopyalamak için Cells.copyColumns yöntemini sağlar.

// 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.getDataDir(CopyingMultipleColumns.class);
// Create an instance of Workbook class by loading the existing spreadsheet
Workbook workbook = new Workbook(dataDir + "aspose-sample.xlsx");
// Get the cells collection of worksheet by name Columns
Cells cells = workbook.getWorksheets().get("Columns").getCells();
// Copy the first 3 columns 7th column
cells.copyColumns(cells, 0, 6, 3);
// Save the result on disc
workbook.save(dataDir + "output.xlsx");

İşte Excel’de kaynak ve sonuç çalışma kitapları nasıl görünür:

todo:image_alt_text

todo:image_alt_text

Yapıştırma Seçenekleri ile Satır/Sütunları Yapıştırma

Aspose.Cells artık Yapıştırma Seçenekleri sağlar ve aynı zamanda CopyRows ve CopyColumns fonksiyonlarını kullanırken uygun yapıştırma seçeneklerinin Excel’e benzer şekilde ayarlanmasına izin verir.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Load some excel file
Workbook wb = new Workbook("book1.xlsx");
// Access the first sheet which contains chart
Worksheet source = wb.getWorksheets().get(0);
// Add another sheet named DestSheet
Worksheet destination = wb.getWorksheets().add("DestSheet");
// Set CopyOptions.ReferToDestinationSheet to true
CopyOptions options = new CopyOptions();
options.setReferToDestinationSheet(true);
// Set PasteOptions
PasteOptions pasteOptions = new PasteOptions();
pasteOptions.setPasteType(PasteType.VALUES);
pasteOptions.setOnlyVisibleCells(true);
// Copy all the rows of source worksheet to destination worksheet which includes chart as well
// The chart data source will now refer to DestSheet
destination.getCells().copyRows(source.getCells(), 0, 0, source.getCells().getMaxDisplayRange().getRowCount(), options, pasteOptions);
// Save workbook in xlsx format
wb.save("destination.xlsx", SaveFormat.XLSX);