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

复制和移动工作表

本文介绍如何使用Aspose.Cells:

在工作簿内复制工作表

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

  1. 在Microsoft Excel中创建两个带有一些数据的工作簿。对于本示例,我们在Microsoft Excel中创建了两个新工作簿,并为工作表输入了一些数据。
  • FirstWorkbook.xls(3个工作表)

  • SecondWorkbook.xls(1个工作表)。

    FirstWorkbook.xls

todo:image_alt_text

SecondWorkbook.xls

todo:image_alt_text

  1. 下载并安装 Aspose.Cells:
    1. 下载Aspose.Cells for Java.
    2. 在开发计算机上解压缩。 所有 Aspose 组件在安装后都是以评估模式运行。 评估模式没有时间限制,只会在生成的文档中添加水印。
  2. 创建一个项目:
    1. 使用Java编辑器(如Eclipse)创建项目或使用文本编辑器创建简单程序。
  3. 添加类路径:
    1. 从 Aspose.Cells.zip 中提取 Aspose.Cells.jar 和 dom4j_1.6.1.jar。
    2. 在 Eclipse 的项目中设置类路径:
      1. 在Eclipse中选择您的项目,单击“项目”,然后选择“属性”菜单。
      2. 在对话框左侧选择“Java构建路径”,然后选择“库”选项卡。
      3. 单击“添加JAR”或“添加外部JAR”来选择Aspose.Cells.jar和dom4j_1.6.1.jar并将它们添加到构建路径中。
  1. 在一个工作簿内复制工作表: 以下是用于完成该任务的代码。它复制了FirstWorkbook.xls中的工作表Copy。

执行代码将名为移动表的工作表从 FirstWorkbook.xls 移动到名为最后工作表的新位置。

输出文件

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(CopyWithinWorkbook.class);
// 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 + "mybook.xls");

在工作簿中移动工作表

以下是用来完成任务的代码。

执行代码将从 FirstWorkbook.xls 中的索引1移动工作表移动到索引2。

输出文件

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(MoveWorksheet.class);
// 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 + "BkFinance.xls");

在工作簿之间复制工作表

执行代码将工作表复制到 SecondWorkbook.xls,并将其命名为Sheet2。

输出文件

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(CopyWorksheetsBetweenWorkbooks.class);
// 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 + "FinalBook.xls", FileFormatType.EXCEL_97_TO_2003);

在工作簿之间移动工作表

执行代码将从 FirstWorkbook.xls 移动工作表移到 SecondWorkbook.xls,并将其命名为Sheet3。

输出 FirstWorkbook.xls 文件

todo:image_alt_text

输出 SecondWorkbook.xls 文件

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(MoveWorksheet.class);
// 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 + "BkFinance.xls");

结论