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

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

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

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

Aspose.Cellsを使用してワークブック内でワークシートをコピーする

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

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "book1.xls";
// Open an existing Excel file.
Workbook wb = new Workbook(InputPath);
// Create a Worksheets object with reference to
// the sheets of the Workbook.
WorksheetCollection sheets = wb.Worksheets;
// 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は、指定のワークシートからデータおよび書式を別のワークブック内または間の別のワークシートにコピーするために使用されるメソッドAspose.Cells.Worksheet.Copy()を提供します。このメソッドは、ソースワークシートオブジェクトをパラメータとして受け取ります。

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "book1.xls";
// Create a Workbook.
// Open a file into the first book.
Workbook excelWorkbook0 = new Workbook(InputPath);
// Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
// Copy the first sheet of the first book into second book.
excelWorkbook1.Worksheets[0].Copy(excelWorkbook0.Worksheets[0]);
// Save the file.
excelWorkbook1.Save(dataDir + "CopyWorksheetsBetweenWorkbooks_out.xls");

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create a new Workbook.
Workbook excelWorkbook0 = new Workbook();
// Get the first worksheet in the book.
Worksheet ws0 = excelWorkbook0.Worksheets[0];
// Put some data into header rows (A1:A4)
for (int i = 0; i < 5; i++)
{
ws0.Cells[i, 0].PutValue(string.Format("Header Row {0}", i));
}
// Put some detail data (A5:A999)
for (int i = 5; i < 1000; i++)
{
ws0.Cells[i, 0].PutValue(string.Format("Detail Row {0}", i));
}
// Define a pagesetup object based on the first worksheet.
PageSetup pagesetup = ws0.PageSetup;
// The first five rows are repeated in each page...
// It can be seen in print preview.
pagesetup.PrintTitleRows = "$1:$5";
// Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
// Get the first worksheet in the book.
Worksheet ws1 = excelWorkbook1.Worksheets[0];
// Name the worksheet.
ws1.Name = "MySheet";
// 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");

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

Aspose.Cellsは、スプレッドシート内の別の場所にワークシートを移動するために使用されるメソッドAspose.Cells.Worksheet.MoveTo()を提供します。このメソッドは、移動先のワークシートインデックスをパラメータとして受け取ります。

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "book1.xls";
// Open an existing excel file.
Workbook wb = new Workbook(InputPath);
// Create a Worksheets object with reference to
// the sheets of the Workbook.
WorksheetCollection sheets = wb.Worksheets;
// Get the first worksheet.
Worksheet worksheet = sheets[0];
// Move the first sheet to the third position in the workbook.
worksheet.MoveTo(2);
// Save the excel file.
wb.Save(dataDir + "MoveWorksheet_out.xls");