ワークシートのコピーと移動

Microsoft Excelでシートを移動またはコピーする

ブック内またはブック間でのワークシートのコピーおよび移動の手順は次のとおりです。

  1. 別のワークブックにシートを移動またはコピーするには、シートを受け取るワークブックを開きます。
  2. 移動またはコピーしたいシートを含むワークブックに切り替え、そのシートを選択します。
  3. 編集メニューでシートの移動またはコピーをクリックします。
  4. 別のブックのボックスで、シートを受け取るブックをクリックしてください。
  5. 選択したシートを新しいブックに移動またはコピーするには、新しいブックをクリックしてください。
  6. 前のシートボックスで、移動またはコピーされたシートが挿入される前のシートをクリックします。
  7. 移動ではなくシートをコピーする場合は、コピーを作成チェックボックスを選択します。

ブック内でのワークシートのコピー

Aspose.CellsはWorksheetCollection.addCopy()というオーバーロードされたメソッドを提供し、これはコレクションにワークシートを追加し、既存のワークシートからデータをコピーするために使用されます。メソッドの1つのバージョンは、ソースワークシートのインデックスをパラメーターとして取ります。もう1つのバージョンは、ソースワークシートの名前をパラメーターとして取ります。

次の例は、ブック内で既存のワークシートをコピーする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/";
// Create a new Workbook by excel file path
Workbook wb = new Workbook(dataDir + "book1.xls");
// Create a Worksheets object with reference to the sheets of the Workbook.
WorksheetCollection sheets = wb.getWorksheets();
// Copy data to a new sheet from an existing sheet within the Workbook.
sheets.addCopy("Sheet1");
// Save the excel file.
wb.save(dataDir + "CopyWithinWorkbook_out.xls");

ブック間でのワークシートのコピー

Aspose.CellsはWorksheet.copy()というメソッドを提供し、これはソースのワークシートオブジェクトをパラメーターとして使用して、ブック内またはブック間で別のワークシートにデータと書式をコピーするために使用されます。

次の例は、ワークブックから別のワークブックにワークシートをコピーする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/";
// Create a Workbook.
Workbook excelWorkbook0 = new Workbook(dataDir + "book1.xls");
// Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
// Copy the first sheet of the first book into second book.
excelWorkbook1.getWorksheets().get(0).copy(excelWorkbook0.getWorksheets().get(0));
// Save the file.
excelWorkbook1.save(dataDir + "CWBetweenWorkbooks_out.xls", FileFormatType.EXCEL_97_TO_2003);

次の例は、ワークブックから別のワークブックにワークシートをコピーする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/";
// Create a new Workbook.
Workbook excelWorkbook0 = new Workbook();
// Get the first worksheet in the book.
Worksheet ws0 = excelWorkbook0.getWorksheets().get(0);
// Put some data into header rows (A1:A4)
for (int i = 0; i < 5; i++) {
ws0.getCells().get(i, 0).setValue("Header Row " + i);
}
// Put some detail data (A5:A999)
for (int i = 5; i < 1000; i++) {
ws0.getCells().get(i, 0).setValue("Detail Row " + i);
}
// Define a pagesetup object based on the first worksheet.
PageSetup pagesetup = ws0.getPageSetup();
// The first five rows are repeated in each page... It can be seen in print preview.
pagesetup.setPrintTitleRows("$1:$5");
// Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
// Get the first worksheet in the book.
Worksheet ws1 = excelWorkbook1.getWorksheets().get(0);
// Name the worksheet.
ws1.setName("Sheet1");
// Copy data from the first worksheet of the first workbook into the first worksheet of the second workbook.
ws1.copy(ws0);
// Save the excel file.
excelWorkbook1.save(dataDir + "CopyWorksheetFromWorkbookToOther_out.xls", FileFormatType.EXCEL_97_TO_2003);

ワークブック内でのワークシートの移動

Aspose.CellsはWorksheet.moveTo()というメソッドを提供し、これは同じスプレッドシート内でワークシートを別の場所に移動するために使用されます。

次の例は、ワークブック内でワークシートを別の場所に移動する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(MoveWorksheet.class) + "worksheets/";
// Create a new Workbook.
Workbook wb = new Workbook(dataDir + "BkFinance.xls");
// Get the first worksheet in the book.
Worksheet sheet = wb.getWorksheets().get(0);
// Move the first sheet to the third position in the workbook.
sheet.moveTo(2);
// Save the Excel file.
wb.save(dataDir + "MoveWorksheet_out.xls");