复制和移动工作表

使用 Microsoft Excel 移动或复制工作表

以下是在 Microsoft Excel 中的工作簿内或工作簿之间复制和移动工作表所涉及的步骤。

  1. 要将工作表移动或复制到另一个工作簿,请打开将接收工作表的工作簿。
  2. 切换到包含要移动或复制的工作表的工作簿,然后选择工作表。
  3. 编辑菜单,点击移动或复制工作表.
  4. 在里面预订对话框中,单击工作簿以接收工作表。
  5. 要将所选工作表移动或复制到新工作簿,请单击新书.
  6. 在里面片前框,单击要在其前插入移动或复制的工作表的工作表。
  7. 要复制工作表而不是移动它们,请选择创建副本复选框。

使用 Aspose.Cells 在工作簿中复制工作表

Aspose.Cells提供重载方法,Aspose.Cells.WorksheetCollection.AddCopy(), 用于将工作表添加到集合并从现有工作表复制数据。该方法的一个版本将源工作表的索引作为参数。另一个版本采用源工作表的名称。

以下示例显示如何复制工作簿中的现有工作表。

// 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");