复制和移动工作表
有时,您确实需要一些具有共同格式和数据的工作表。例如,如果您在季度预算上工作,您可能希望创建一个包含具有相同列标题、行标题和公式的工作表的工作簿。有一种方法可以做到这一点:先创建一个工作表,然后进行复制。
Aspose.Cells支持在工作簿内部或之间复制和移动工作表。工作表、数据、格式、表格、矩阵、图表、图像和其他对象都可以以最高程度的精度复制。
使用Microsoft Excel移动或复制工作表
以下是在Microsoft Excel中复制和移动工作表的步骤。
- 要将工作表移动或复制到另一个工作簿中,请打开将要接收工作表的工作簿。
- 切换到包含您想要移动或复制的工作表的工作簿,然后选择这些工作表。
- 在“编辑”菜单上,单击“移动或复制工作表”
- 在“选择工作簿”对话框中,单击要接收工作表的工作簿。
- 要将所选工作表移动或复制到新工作簿中,请单击“新建工作簿”
- 在“工作表之前”框中,单击要在其之前插入移动或复制的工作表。
- 要复制工作表而不是移动它们,请选择“创建副本”复选框。
在Aspose.Cells中复制工作表
Aspose.Cells提供了一个重载的方法AddCopy(),用于将工作表添加到集合并从现有工作表中复制数据。该方法的一个版本将源工作表的索引作为参数。另一个版本将源工作表的名称作为参数。以下示例显示了如何在工作簿内部复制现有工作表。
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
//Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
//Path of input excel file | |
U16String sampleCopyingAndMovingWorksheets = srcDir + u"sampleCopyingAndMovingWorksheets.xlsx"; | |
//Path of output excel file | |
U16String outputCopyingAndMovingWorksheets = outDir + u"outputCopyingAndMovingWorksheets.xlsx"; | |
//Create workbook | |
Workbook workbook(sampleCopyingAndMovingWorksheets); | |
//Create worksheets object with reference to the sheets of the workbook. | |
WorksheetCollection sheets = workbook.GetWorksheets(); | |
//Copy data to a new sheet from an existing sheet within the workbook. | |
sheets.AddCopy(u"Sheet1"); | |
//Save the Excel file. | |
workbook.Save(outputCopyingAndMovingWorksheets); | |
std::cout << "Worksheet copied successfully with in a workbook!" << std::endl; | |
Aspose::Cells::Cleanup(); |
在工作簿内部移动工作表
Aspose.Cells提供了一个MoveTo()方法,用于将工作表移动到同一电子表格中的另一个位置。该方法将目标工作表的索引作为参数。以下示例显示了如何将工作表移动到工作簿内的另一个位置。
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
//Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
//Path of input excel file | |
U16String sampleCopyingAndMovingWorksheets = srcDir + u"sampleCopyingAndMovingWorksheets.xlsx"; | |
//Path of output excel file | |
U16String outputCopyingAndMovingWorksheets = outDir + u"outputCopyingAndMovingWorksheets.xlsx"; | |
//Create workbook | |
Workbook workbook(sampleCopyingAndMovingWorksheets); | |
//Create worksheets object with reference to the sheets of the workbook. | |
WorksheetCollection sheets = workbook.GetWorksheets(); | |
//Access the first sheet | |
Worksheet sheet = sheets.Get(0); | |
//Move the first sheet to the third position in the workbook. | |
sheet.MoveTo(2); | |
//Save the Excel file. | |
workbook.Save(outputCopyingAndMovingWorksheets); | |
std::cout << "Worksheet moved successfully with in a workbook!" << std::endl; | |
Aspose::Cells::Cleanup(); |