复制和移动工作表
Contents
[
Hide
]
有时,您确实需要许多具有通用格式和数据的工作表。例如,如果您处理季度预算,您可能希望创建一个工作簿,其中的工作表包含相同的列标题、行标题和公式。有一种方法可以做到这一点:创建一张纸然后复制它。
Aspose.Cells 支持在工作簿内或工作簿之间复制和移动工作表。包含数据、格式、表格、矩阵、图表、图像和其他对象的工作表以最高精度复制。
使用 Microsoft Excel 移动或复制工作表
以下是在 Microsoft Excel 中的工作簿内或工作簿之间复制和移动工作表所涉及的步骤。
- 要将工作表移动或复制到另一个工作簿,请打开将接收工作表的工作簿。
- 切换到包含要移动或复制的工作表的工作簿,然后选择工作表。
- 在编辑菜单,点击移动或复制工作表.
- 在里面预订对话框中,单击工作簿以接收工作表。
- 要将所选工作表移动或复制到新工作簿,请单击新书.
- 在里面片前框,单击要在其前插入移动或复制的工作表的工作表。
- 要复制工作表而不是移动它们,请选择创建副本复选框。
使用 Aspose.Cells 在工作簿中复制工作表
Aspose.Cells 提供重载方法添加副本()用于将工作表添加到集合并从现有工作表复制数据。该方法的一个版本将源工作表的索引作为参数。另一个版本采用源工作表的名称。以下示例显示如何复制工作簿中的现有工作表。
This file contains 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-C | |
//Source directory path | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
//Output directory path | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
//Path of input excel file | |
StringPtr sampleCopyingAndMovingWorksheets = srcDir->StringAppend(new String("sampleCopyingAndMovingWorksheets.xlsx")); | |
//Path of output excel file | |
StringPtr outputCopyingAndMovingWorksheets = outDir->StringAppend(new String("outputCopyingAndMovingWorksheets.xlsx")); | |
//Create workbook | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleCopyingAndMovingWorksheets); | |
//Create worksheets object with reference to the sheets of the workbook. | |
intrusive_ptr<IWorksheetCollection> sheets = workbook->GetIWorksheets(); | |
//Copy data to a new sheet from an existing sheet within the workbook. | |
sheets->AddCopy(new String("Test1")); | |
//Save the Excel file. | |
workbook->Save(outputCopyingAndMovingWorksheets); | |
StringPtr msg = new String("Worksheet copied successfully with in a workbook!"); | |
Console::WriteLine(msg); |
在工作簿中移动工作表
Aspose.Cells提供方法搬去()用于将工作表移动到同一电子表格中的另一个位置。该方法将目标工作表索引作为参数。下面的示例演示如何将工作表移动到工作簿中的另一个位置。
This file contains 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-C | |
//Source directory path | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
//Output directory path | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
//Path of input excel file | |
StringPtr sampleCopyingAndMovingWorksheets = srcDir->StringAppend(new String("sampleCopyingAndMovingWorksheets.xlsx")); | |
//Path of output excel file | |
StringPtr outputCopyingAndMovingWorksheets = outDir->StringAppend(new String("outputCopyingAndMovingWorksheets.xlsx")); | |
//Create workbook | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleCopyingAndMovingWorksheets); | |
//Create worksheets object with reference to the sheets of the workbook. | |
intrusive_ptr<IWorksheetCollection> sheets = workbook->GetIWorksheets(); | |
//Access the first sheet | |
intrusive_ptr<IWorksheet> sheet = sheets->GetObjectByIndex(0); | |
//Move the first sheet to the third position in the workbook. | |
sheet->MoveTo(2); | |
//Save the Excel file. | |
workbook->Save(outputCopyingAndMovingWorksheets); | |
StringPtr msg = new String("Worksheet moved successfully with in a workbook!"); | |
Console::WriteLine(msg); |