复制和移动工作表在工作簿内和工作簿之间

复制和移动工作表

在工作簿内复制工作表

所有示例的初始步骤都是相同的。

  1. 在Microsoft Excel中创建两个带有一些数据的工作簿。对于本示例,我们在Microsoft Excel中创建了两个新工作簿,并为工作表输入了一些数据。
  • FirstWorkbook.xlsx(3 个工作表)。
  • SecondWorkbook.xlsx(1 个工作表)。
  1. 下载并安装 Aspose.Cells:
    1. 下载 Aspose.Cells for .NET
    2. 在您的开发计算机上安装它。 所有 Aspose 组件在安装后都是以评估模式运行。 评估模式没有时间限制,只会在生成的文档中添加水印。
  2. 创建一个项目:
    1. 启动 Visual Studio.Net。
    2. 创建新的控制台应用程序。
  3. 添加引用:
    1. 向项目添加对 Aspose.Cells 的引用。 例如,向 …\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll 添加引用。
  4. 在工作簿内复制工作表 第一个示例将 FirstWorkbook.xlsx 内的第一个工作表(Copy)复制。

执行代码后,名为 Copy 的工作表将在 FirstWorkbook.xlsx 中复制并命名为 Last Sheet。

// 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);
// Open a file into the first book.
Workbook excelWorkbook1 = new Workbook(dataDir + @"FirstWorkbook.xlsx");
// Copy the first sheet of the first book with in the workbook
excelWorkbook1.Worksheets[2].Copy(excelWorkbook1.Worksheets["Copy"]);
// Save the file.
excelWorkbook1.Save(dataDir + @"FirstWorkbookCopied_out.xlsx");

工作簿内移动工作表

以下代码显示了如何将工作簿内的工作表从一个位置移动到另一个位置。执行代码后,Move 工作表从 FirstWorkbook.xlsx 的索引 1 移动到索引 2。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open a file into the first book.
Workbook excelWorkbook2 = new Workbook(dataDir + @"FirstWorkbook.xlsx");
// Move the sheet
excelWorkbook2.Worksheets["Move"].MoveTo(2);
// Save the file.
excelWorkbook2.Save(dataDir + @"FirstWorkbookMoved_out.xlsx");

在工作簿之间复制工作表

执行代码后,工作表名为 Copy 被复制到 SecondWorkbook.xlsx,并命名为 Sheet2。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open a file into the first book.
Workbook excelWorkbook3 = new Workbook(dataDir + @"FirstWorkbook.xlsx");
// Open a file into the second book.
Workbook excelWorkbook4 = new Workbook(dataDir + @"SecondWorkbook.xlsx");
// Add new worksheet into second Workbook
excelWorkbook4.Worksheets.Add();
// Copy the first sheet of the first book into second book.
excelWorkbook4.Worksheets[1].Copy(excelWorkbook3.Worksheets["Copy"]);
// Save the file.
excelWorkbook4.Save(dataDir + @"CopyWorksheetsBetweenWorkbooks_out.xlsx");

在工作簿之间移动工作表

执行代码后,工作表名为 Move 从 FirstWorkbook.xlsx 移动到 SecondWorkbook.xlsx,并命名为 Sheet3。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Open a file into the first book.
Workbook excelWorkbook5 = new Workbook(dataDir + @"FirstWorkbook.xlsx");
//Create another Workbook. Open a file into the Second book.
Workbook excelWorkbook6 = new Workbook(dataDir + @"SecondWorkbook.xlsx");
//Add New Worksheet
excelWorkbook6.Worksheets.Add();
//Copy the sheet from first book into second book.
excelWorkbook6.Worksheets[2].Copy(excelWorkbook5.Worksheets[2]);
//Remove the copied worksheet from first workbook
excelWorkbook5.Worksheets.RemoveAt(2);
//Save the file.
excelWorkbook5.Save(dataDir + @"FirstWorkbookWithMove_out.xlsx");
//Save the file.
excelWorkbook6.Save(dataDir + @"SecondWorkbookWithMove_out.xlsx");