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

复制和移动工作表

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

在工作簿中复制工作表

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

  1. 在 Microsoft Excel 中使用一些数据创建两个工作簿。出于本示例的目的,我们在 Microsoft Excel 中创建了两个新工作簿,并将一些数据输入到工作表中。
  • FirstWorkbook.xls(3 个工作表)

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

    FirstWorkbook.xls

待办事项:图片_替代_文本

第二工作簿.xls

待办事项:图片_替代_文本

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

执行代码将 FirstWorkbook.xls 中名为 Copy 的工作表移动到新名称 Last Sheet。

输出文件

待办事项:图片_替代_文本

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

在工作簿中移动工作表

下面是用于完成任务的代码。

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

输出文件

待办事项:图片_替代_文本

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

在工作簿之间复制工作表

执行代码将工作表 Copy 复制到 SecondWorkbook.xls 并使用新名称 Sheet2。

输出文件

待办事项:图片_替代_文本

// 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

待办事项:图片_替代_文本

输出 SecondWorkbook.xls

待办事项:图片_替代_文本

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

结论