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

Microsoft Excel を使用したシートの移動またはコピー

以下は、ワークブック内またはワークブック間でワークシートをコピーおよび移動するための手順です。

  1. シートを別のブックに移動またはコピーするには、シートを受け取るブックを開きます。
  2. 移動またはコピーするシートを含むブックに切り替えてから、シートを選択します。
  3. 上で編集メニュー、クリックシートの移動またはコピー.
  4. [To book] ボックスで、ワークブックをクリックしてシートを受け取ります。
  5. 選択したシートを新しいワークブックに移動またはコピーするには、新しい本.
  6. の中にシート前ボックスで、移動またはコピーしたシートを挿入する前のシートをクリックします。
  7. シートを移動する代わりにコピーするには、コピーを作成するチェックボックス。

ワークブック内でワークシートをコピーする

Aspose.Cells はオーバーロードされたメソッドを提供し、WorksheetCollection.addCopy()、ワークシートをコレクションに追加し、既存のワークシートからデータをコピーするために使用されます。メソッドの 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");