Download includes report.xlsx.
dotnet add package RecursiveAI.OneDocument.SpreadsheetsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Open("report.xlsx"); using var split = book.SplitBySheet(); int i = 1; foreach (var piece in split.Pieces) piece.Workbook.SaveAs($"sheet-{i++}.xlsx");
Writes sheet-1.xlsx (Sales) and sheet-2.xlsx (Summary); prints nothing.
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
The full program
In report.xlsx, Sales has 120 in A1 and Summary has =Sales!A1 in A1. This version reads each worksheet’s report. Summary’s formula still depends on the source workbook; splitting doesn’t turn it into a value. Charts, pictures and pivot tables aren’t copied.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Open("report.xlsx"); using var split = book.SplitBySheet(); for (int i = 0; i < split.Pieces.Count; i++) { var piece = split.Pieces[i]; string file = $"sheet-{i + 1}.xlsx"; piece.Workbook.SaveAs(file); Console.WriteLine($"{file}: {piece.SheetName}"); foreach (var resolution in piece.Report.Resolutions) Console.WriteLine($" Resolution: {resolution.Code}"); foreach (var problem in piece.Report.Problems) Console.WriteLine($" Problem: {problem.Code}"); foreach (var loss in piece.Report.Loss.Items) Console.WriteLine($" Loss: {loss.Code}"); }
sheet-1.xlsx: Sales sheet-2.xlsx: Summary Loss: merge.external-references
Summary’s =Sales!A1 now points to the Sales sheet in report.xlsx.
A save can succeed even when something wasn’t copied: check Report.Problems and Report.Loss.Items for each worksheet, not just whether a file was written.
Variations
Set the link target if the source will be renamed. This changes the link stored in Summary; it doesn’t rename or copy report.xlsx.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Open("report.xlsx"); using var split = book.SplitBySheet("renamed.xlsx"); split.Pieces[1].Workbook.SaveAs("summary.xlsx");
Related recipes
The short answer
Open the workbook, call SplitBySheet(), and save each piece’s Workbook to a different .xlsx file. Each piece has one worksheet and its own report. The original workbook doesn’t change.
How do I save each Excel sheet in a separate file?
Save the pieces in the order they’re returned, which is the original worksheet order. The example uses numbered filenames and prints the original SheetName beside each. That gives you a way to match the outputs to the input without using arbitrary worksheet names as filesystem paths. If you use the names in filenames instead, handle invalid characters and collisions before saving. Chart, dialog and macro sheets are skipped; the split is for worksheets, not every kind of Excel tab.
How do I copy an Excel sheet to a separate workbook without breaking its formulas?
A formula that uses cells on the same worksheet can keep referring to those cells. A formula that needs another worksheet can’t use an in-workbook reference after the split. In this example, Sales!A1 becomes an external reference to Sales in report.xlsx. OneDocument records that change as merge.external-references; the Summary output isn’t independent of the source. Keep the source available at the link target if others need to refresh those references. If they need stand-alone files with live cross-sheet formulas, keep the dependent sheets together instead of splitting them.
The worksheet copy rules also explain what travels with a worksheet. Cells are copied with their cached values and formulas, along with styles, merged cells, print settings, conditional formatting and data validation. The split doesn’t recalculate those formulas. Drawings, charts, pictures, comments, pivot tables and slicers don’t carry over. Review the report before handing off the individual files, especially if the original uses more than cells and formulas.