行と列のコピー

序章

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

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

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

  1. コピーする行または列を選択します。
  2. 行または列をコピーするには、コピー上で標準ツールバー、またはCTRL+C.
  3. 選択範囲をコピーする場所の下または右にある行または列を選択します。
  4. 行または列をコピーする場合は、Cellsをコピーしました上で入れるメニュー。

単一行のコピー

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

の[copyRow](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRow(com.aspose.cells.Cells,%20int,%20int)メソッドは、次のパラメーターを取ります。

  • 起源Cells物体、
  • ソース行インデックス、および
  • 宛先行インデックス。

このメソッドを使用して、シート内の行をコピーするか、別のシートにコピーします。の[copyRow](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRow(com.aspose.cells.Cells,%20int,%20int)メソッドは、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:画像_代替_文章

複数行のコピー

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

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

todo:画像_代替_文章

// 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:画像_代替_文章

単一列のコピー

Aspose.Cells はコピー列 の方法Cellsクラスの場合、このメソッドは、数式 (更新された参照を含む) および値、コメント、セル形式、非表示のセル、画像、およびその他の描画オブジェクトを含むすべての種類のデータをソース列から宛先列にコピーします。

の[コピー列](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyColumn(com.aspose.cells.Cells,%20int,%20int)メソッドは、次のパラメーターを取ります。

  • 起源Cells物体、
  • ソース列インデックス、および
  • 宛先列のインデックス。

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

次の使用例は、ワークシートから列をコピーし、別のブックのワークシートに貼り付けます。

あるワークブックから別のワークブックに列がコピーされる

todo:画像_代替_文章

// 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](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRow(com.aspose.cells.Cells,%20int,%20int) メソッド、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:画像_代替_文章

todo:画像_代替_文章

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

Aspose.Cells が提供するようになりました貼り付けオプション関数の使用中[行のコピー](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyRows(com.aspose.cells.Cells,%20int,%20int,%20int,%20com.aspose.cells.CopyOptions,%20com.aspose.cells.PasteOptions) ) と[コピー列](https://reference.aspose.com/cells/java/com.aspose.cells/cells#copyColumns(com.aspose.cells.Cells,%20int,%20int,%20int,%20com.aspose.cells.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);