---
title: "Save each Excel sheet to a separate workbook in C#"
url: https://recursiveai.org/onedocument/cookbook/split-excel-workbook-by-worksheet/
description: "Save each Excel worksheet to a separate XLSX workbook in C# with OneDocument. Check the report for external formula links and features that weren’t copied."
version: "1.0.0"
---

# Save each Excel sheet to a separate workbook in C#

**OneDocument** is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDocument.Spreadsheets) · [GitHub](https://github.com/recursive-ai-oss/onedocument-csharp) · [Docs](https://recursiveai.org/onedocument/docs/) · [Download example (.zip)](https://recursiveai.org/onedocument/cookbook/split-excel-workbook-by-worksheet/split-excel-workbook-by-worksheet.zip)

Download includes `report.xlsx`.

```shell
dotnet add package RecursiveAI.OneDocument.Spreadsheets
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onedocument/cookbook/split-excel-workbook-by-worksheet/split-excel-workbook-by-worksheet.zip) 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.

```csharp
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}");
}
```

```text
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`.

```csharp
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

- [Copy an Excel worksheet to another workbook](https://recursiveai.org/onedocument/cookbook/copy-excel-worksheet/)
- [Convert an XLSX worksheet to CSV](https://recursiveai.org/onedocument/cookbook/convert-xlsx-to-csv/)

## 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](https://recursiveai.org/onedocument/docs/operations-merge/) 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.
