Copy worksheet cells, formulas and styles between workbooks. The zip supplies source.xlsx and destination.xlsx; drawings, charts and pivot tables are not copied.
dotnet add package RecursiveAI.OneDocument.SpreadsheetsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument.Spreadsheets; using var source = Workbook.Open("source.xlsx"); using var target = Workbook.Open("destination.xlsx"); source.Sheets["Sales"].CopyTo(target); target.SaveAs("copied.xlsx");
source.xlsx must contain a sheet named “Sales”; replace "Sales" with your own sheet name if needed. Writes copied.xlsx; the snippet prints nothing.
Unzip the example, then dotnet run.
The full program
The source’s Sales sheet contains these cells:
| Region | Sales |
|---|---|
| East | 100 |
| West | 200 |
The destination already has a Sales sheet with “Existing destination” in A1. The full program prints the copy’s new name and every reported resolution, problem and loss.
using RecursiveAI.OneDocument.Spreadsheets; using var source = Workbook.Open("source.xlsx"); using var target = Workbook.Open("destination.xlsx"); var result = source.Sheets["Sales"].CopyTo(target); target.SaveAs("copied.xlsx"); Console.WriteLine($"Copied: {result.Sheet.Name}"); foreach (var item in result.Report.Resolutions) Console.WriteLine(item.Code); foreach (var item in result.Report.Problems) Console.WriteLine(item.Code); foreach (var item in result.Report.Loss.Items) Console.WriteLine(item.Code);
dotnet run prints:
Copied: Sales (2) merge.sheet-renamed
Copying can succeed with losses or conflicts; inspect Report.Loss, Report.Problems and Report.Resolutions before accepting the output.
Variations
Duplicate a worksheet within its workbook.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Open("source.xlsx"); var result = book.Sheets["Sales"].CopyTo(book); book.SaveAs("duplicate.xlsx"); Console.WriteLine(result.Sheet.Name);
Writes duplicate.xlsx and prints Sales (2).
Related recipes
The short answer
Open both workbooks, call source.Sheets["Sales"].CopyTo(target), and save the target workbook. The return value contains the new sheet and a merge report. Copying is not an all-content clone: review the report before using the result for a workbook with related objects.
Can I copy a worksheet from one workbook to another?
Yes. Worksheet.CopyTo copies cells with their cached values, formulas, styles, row and column settings, merged cells, tables, conditional formats, data validations, print settings, sheet-scoped names and hyperlinks. It does not recalculate formulas, and it does not set the destination’s fullCalcOnLoad flag. Call Recalculate explicitly when your workflow needs fresh results rather than the stored results from the source.
The source and destination are separate open workbook objects. Saving the destination writes the copy; the operation does not require saving changes back to the source. In the supplied files, both workbooks already have a Sales sheet. OneDocument keeps the existing destination sheet and calls the imported sheet “Sales (2)”. The full program prints merge.sheet-renamed so that the naming change is visible.
Styles travel by value rather than by blindly reusing the source’s style indexes. When the workbooks use different date systems, date serials are adjusted by 1462 days. These rules are documented in worksheet copying; they matter when copying between independently authored workbooks rather than two files built from the same template.
How can you insert a copy of an existing worksheet within the same workbook?
Pass the source workbook itself to CopyTo, as in the variation. The existing Sales sheet remains, and the copy receives an available name. References to the copied sheet itself follow the new name. This is a duplicate operation, not a move: the original sheet is still present after saving.
The returned Sheet is the imported sheet. Use it when your next step edits the copy, rather than looking it up by a name that might have been changed to avoid a collision. For a destructive move workflow, the library also documents MoveTo, but references from sheets left behind are not automatically re-pointed.
Why can’t I copy everything from one Excel sheet to another?
A worksheet’s visible content may depend on other package parts. OneDocument’s copy operation does not copy drawings, charts, pictures, comments, pivot tables or slicers. It reports omitted related parts with merge.sheet-part-not-copied, and omitted sheet extensions as a loss. Table totals rows and calculated-column formulas are also not carried. A successful save therefore does not mean every feature arrived.
Formulas need particular care. When a copied formula refers to another sheet in the source workbook, the cross-workbook copy turns that reference into an external link to the source. It does not silently import the other sheet. A workbook-level name already defined differently in the destination is kept, and copied formulas use that destination definition; the report records merge.name-conflict. Review these cases before recalculating or distributing the result. The sample avoids those dependencies so the name collision is the only resolution it prints.