在工作簿内和工作簿之间复制和移动工作表
Contents
[
Hide
]
有时,您确实需要许多具有通用格式和数据输入的工作表。例如,如果您处理季度预算,您可能希望创建一个工作簿,其中的工作表包含相同的列标题、行标题和公式。有一种方法可以做到这一点:创建一张纸,然后将其复制三次。
Aspose.Cells 支持在工作簿内或工作簿之间复制或移动工作表。包括数据、格式、表格、矩阵、图表、图像和其他对象在内的工作表以最高精度复制。
复制和移动工作表
本文介绍了如何使用 Aspose.Cells 来:
在工作簿中复制工作表
所有示例的初始步骤都相同。
- 在 Microsoft Excel 中使用一些数据创建两个工作簿。出于本示例的目的,我们在 Microsoft Excel 中创建了两个新工作簿,并将一些数据输入到工作表中。
-
FirstWorkbook.xls(3 个工作表)
-
SecondWorkbook.xls(1 个工作表)。
FirstWorkbook.xls
第二工作簿.xls
- 下载并安装 Aspose.Cells:
- 在您的开发计算机上解压它。 全部Aspose组件在安装后以评估模式工作。评估模式没有时间限制,它只在生成的文档中注入水印。
- 创建一个项目:
- 使用 Java 编辑器(如 Eclipse)创建项目或使用文本编辑器创建简单程序。
- 添加类路径:
- 从 Aspose.Cells.zip 中提取 Aspose.Cells.jar 和 dom4j_1.6.1.jar。 1、在Eclipse中设置项目的类路径:
- 在 Eclipse 中选择您的项目并单击菜单项目, 然后特性. 1.选择Java 构建路径在对话框的左侧,然后选择库选项卡, 1.点击添加 JAR要么添加外部 JAR选择 Aspose.Cells.jar 和 dom4j_1.6.1.jar 并将它们添加到构建路径中。
或者您可以在 Windows 上的 DOS 提示符下在运行时设置类路径。 例如:
javac -classpath %classpath%;e:\Aspose.Cells.jar; ClassName .javajava -classpath %classpath%;e:\Aspose.Cells.jar; ClassName
- 在工作簿中复制工作表: 下面是用于完成任务的代码。它复制 FirstWorkbook.xls 中的工作表 Copy。
执行代码将 FirstWorkbook.xls 中名为 Copy 的工作表移动到新名称 Last Sheet。
输出文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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。
输出文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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。
输出文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
结论
本文介绍如何使用 Aspose.Cells 在工作簿内和工作簿之间复制和移动工作表。
Aspose.Cells 得益于多年的研究、设计和精心调整。我们欢迎您的查询、意见和建议Aspose.Cells论坛.我们保证及时回复。