aspose.cells Copy and Move Worksheets Within and Between Workbooks
aspose.cells Copy and Move Worksheets Within and Between Workbooks
关键词:aspose.cells,移动工作表、复制工作表、导出工作表到单独的文件,导出工作表到另外的文件,保存工作表到单独的文件,分割所有的工作表。
功能:将一个excel文件的多个工作表,分别复制或者移动到另外的excel
Link: https://docs.aspose.com/cells/net/copy-and-move-worksheets-within-and-between-workbooks/
Sometimes, you do need a number of worksheets with common formatting and data entry. For example, if you work with quarterly budgets, you might want to create a workbook with sheets that contain the same column headings, row headings, and formulas. There is a way to do this: by creating one sheet and then copying it three times.
Aspose.Cells supports copying or moving worksheets within or between workbooks. Worksheets including data, formatting, tables, matrices, charts, images and other objects are copied with the highest degree of precision.
Copying and Moving Worksheets
Copying a Worksheet within a Workbook
When executing the code, the worksheet named Copy is copied within FirstWorkbook.xlsx with the name Last Sheet.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
| |
// Open a file into the first book. | |
Workbook excelWorkbook1 = new Workbook(dataDir + @"FirstWorkbook.xlsx"); | |
| |
// Copy the first sheet of the first book with in the workbook | |
excelWorkbook1.Worksheets[2].Copy(excelWorkbook1.Worksheets["Copy"]); | |
| |
// Save the file. | |
excelWorkbook1.Save(dataDir + @"FirstWorkbookCopied_out.xlsx"); |
Moving a Worksheet within a Workbook
The code below shows how to move a worksheet from one position in a workbook to another. Executing the code moves the worksheet called Move from index 1 to index 2 in FirstWorkbook.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open a file into the first book. | |
Workbook excelWorkbook2 = new Workbook(dataDir + @"FirstWorkbook.xlsx"); | |
| |
// Move the sheet | |
excelWorkbook2.Worksheets["Move"].MoveTo(2); | |
| |
// Save the file. | |
excelWorkbook2.Save(dataDir + @"FirstWorkbookMoved_out.xlsx"); |
Copying a Worksheet between Workbooks
Executing the code copies the worksheet named Copy is to SecondWorkbook.xlsx with the name Sheet2.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open a file into the first book. | |
Workbook excelWorkbook3 = new Workbook(dataDir + @"FirstWorkbook.xlsx"); | |
| |
// Open a file into the second book. | |
Workbook excelWorkbook4 = new Workbook(dataDir + @"SecondWorkbook.xlsx"); | |
| |
// Add new worksheet into second Workbook | |
excelWorkbook4.Worksheets.Add(); | |
| |
// Copy the first sheet of the first book into second book. | |
excelWorkbook4.Worksheets[1].Copy(excelWorkbook3.Worksheets["Copy"]); | |
| |
// Save the file. | |
excelWorkbook4.Save(dataDir + @"CopyWorksheetsBetweenWorkbooks_out.xlsx"); |
Moving a Worksheet between Workbooks
Executing the code moves the worksheet named Move from FirstWorkbook.xlsx to SecondWorkbook.xlsx with the name Sheet3.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Open a file into the first book. | |
Workbook excelWorkbook5 = new Workbook(dataDir + @"FirstWorkbook.xlsx"); | |
| |
//Create another Workbook. Open a file into the Second book. | |
Workbook excelWorkbook6 = new Workbook(dataDir + @"SecondWorkbook.xlsx"); | |
| |
//Add New Worksheet | |
excelWorkbook6.Worksheets.Add(); | |
| |
//Copy the sheet from first book into second book. | |
excelWorkbook6.Worksheets[2].Copy(excelWorkbook5.Worksheets[2]); | |
| |
//Remove the copied worksheet from first workbook | |
excelWorkbook5.Worksheets.RemoveAt(2); | |
| |
//Save the file. | |
excelWorkbook5.Save(dataDir + @"FirstWorkbookWithMove_out.xlsx"); | |
| |
//Save the file. | |
excelWorkbook6.Save(dataDir + @"SecondWorkbookWithMove_out.xlsx"); |