行と列のコピー

紹介

時には、ワークシート全体をコピーせずに行や列をコピーする必要があります。Aspose.Cellsを使用すると、ワークブック内またはワークブック間で行や列をコピーすることができます。

行(または列)をコピーすると、それに含まれるデータ(更新された参照を含む数式、値、コメント、書式設定、非表示セル、画像、その他の図形オブジェクトなど)がコピーされます。

Microsoft Excelで行と列をコピーする

  1. コピーしたい行または列を選択します。
  2. 行または列をコピーする場合は、標準ツールバーのコピーをクリックするか、CTRL+Cを押します。
  3. コピーする選択範囲の下または右側に行または列を選択します。
  4. 行または列をコピーする際に、挿入メニューでコピーしたセルをクリックします。

単一行のコピー

Aspose.Cellsは、Cells クラスのcopyRow メソッドを提供しています。このメソッドは、ソース行から宛先行に数式、値、コメント、セルフォーマット、非表示セル、画像、および他の描画オブジェクトを含む、すべての種類のデータをコピーします。

copyRow メソッドは、次のパラメータを受け取ります:

  • ソースのCells オブジェクト,
  • ソースの行インデックス、および
  • 宛先の行インデックス。

このメソッドを使用して、ワークシート内であるいは他のワークシートに行をコピーすることが可能です。copyRow メソッドは Microsoft Excel と同様に機能します。例えば、宛先行の高さを明示的に設定する必要はありません。その値もコピーされます。

以下の例は、ワークシート内の行をコピーする方法を示しています。テンプレートのMicrosoft Excelファイルを使用し、2番目の行(データ、書式設定、コメント、画像などを含む)を12番目の行に貼り付けます。

// 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.");

以下は、以下のコードを実行した際の出力です。

最高度の精度と正確さで行がコピーされます

todo:image_alt_text

複数の行のコピー

追加の整数型のパラメータを指定するCells.copyRows メソッドを使用して、新しい宛先に複数の行をコピーすることもできます。

以下は、3行のデータを含む入力スプレッドシートのスナップショットですが、以下に提供されているコードスニペットは、すべての3行を7行から始まる新しい場所にコピーします。

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");

上記のコードスニペットを実行した後の結果のスプレッドシートビューは次のようになります。

todo:image_alt_text

単一列のコピー

Aspose.CellsはCellsクラスのcopyColumn メソッドを提供しており、このメソッドは、更新された参照を含む形式、値、コメント、セル形式、非表示セル、イメージなどの他の描画オブジェクトを含むあらゆる種類のデータを、ソースの列から宛先の列にコピーします。

copyColumn メソッドは以下のパラメータを取ります:

  • ソースのCells オブジェクト,
  • ソースの列インデックス、および
  • 宛先の列インデックス。

copyColumn メソッドを使用して、シート内または別のシートに列をコピーします。

この例では、ワークシートから列をコピーして別のブック内のワークシートに貼り付けます。

列を別のワークブックにコピーする

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.");

複数の列をコピー

Cells.copyRowsメソッドと同様に、Aspose.CellsのAPIは新しい位置に複数のソース列をコピーするためのCells.copyColumnsメソッドも提供しています。

// 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");

以下は、Excelでのソースと結果のスプレッドシートの見え方です。

todo:image_alt_text

todo:image_alt_text

貼り付けオプションを使用して行/列を貼り付ける

Aspose.Cellsは、関数CopyRowsCopyColumnsを使用する際にPasteOptionsを提供します。これにより、Excelと同様の適切な貼り付けオプションを設定できます。

// 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);