Rename, delete, move and copy sheets
A sheet is renamed by setting Name, and deleted, moved and copied with Delete, MoveTo and CopyTo. CopySheetFrom brings a sheet from another workbook and Merge brings all of them. Everything that refers to a sheet follows, by Excel's own rules.
dotnet add package DocWright.Spreadsheet
Note
Checked against Excel. The same 23 operations were made in Excel and here, on a workbook with formulas, names that belong to the workbook and to sheets, print areas, a filter, a table, charts, a comment, a pivot table, a conditional format, a validation and a sparkline. The saved files agree place by place, apart from the differences listed at the end, and Excel opens all 144 workbooks written this way from the test files without repair.
Copy a sheet
using DocWright.Spreadsheet;
// A copy at a place in the list (counted from zero), under a name of its own.
XlsxDocumentSheet feb = jan.CopyTo(1, "Feb");
XlsxDocumentSheet mar = jan.CopyTo(2, "Mar");
feb.SetValue("B1", XlsxCellValue.Number(95m));
mar.SetValue("B1", XlsxCellValue.Number(140m));
// With no name the copy gets the name Excel gives one, and a formula
// that named the sheet copied names the copy.
feb.SetFormula("C1", "Feb!B1*2");
XlsxDocumentSheet again = feb.CopyTo(workbook.Sheets.Count);
Console.WriteLine("The copy is called " + again.Name + " and holds " + again.GetCell("C1").Formula);
A place is counted from zero: it is an index into Sheets. The copy is the sheet and everything that belongs to it, each as its own: cells and formats, charts, comments, tables (under a new name, because a workbook has one table of a name) and pivot tables, which go on reading the cache the originals read. Changes made to the sheet and not yet saved are in the copy.
| In the copy | |
|---|---|
A formula that names the sheet copied (Feb!B1) |
Names the copy. So do its chart series. |
| A formula that names no sheet, or another sheet | Unchanged. |
| A reference through several sheets that starts or ends on the sheet copied | #REF!, as in Excel. |
| A defined name that belongs to the sheet | Repeated for the copy. |
| A name of the workbook that refers to the sheet | Gets a twin that belongs to the copy and refers to it. |
| A hyperlink's target | Unchanged, as in Excel. |
| A hidden sheet | Its copy is hidden. |
With no name, the copy is called what Excel calls one: Feb (2), then Feb (3). A name of 31 characters is cut to fit.
Rename
using DocWright.Spreadsheet;
jan.Name = "Jan 2026";
Console.WriteLine("After the rename: " + summary.GetCell("A1").Formula + " and " + summary.GetCell("A2").Formula);
Formulas on every sheet, defined names, chart series, conditional formats, validations and a pivot table's source follow. A name that needs apostrophes gets them.
Move
using DocWright.Spreadsheet;
mar.MoveTo(0);
Console.WriteLine("After the move: " + summary.GetCell("A1").Formula + " in " + string.Join(", ", workbook.Sheets.Select(s => s.Name)));
A reference by name does not care where a sheet stands. A reference through several sheets does, in one case: when the sheet moved is one of its ends and now stands on the other side of the other end. That end then becomes the sheet that stood next to it inside the span. Otherwise the text is left, and the span covers whatever now stands between its ends. This is what Excel does, measured on eleven moves.
Delete
using DocWright.Spreadsheet;
mar.Delete();
Console.WriteLine("After the delete: " + summary.GetCell("A2").Formula);
try
{
mar.SetValue("A1", XlsxCellValue.Number(1m));
}
catch (InvalidOperationException gone)
{
Console.WriteLine(gone.Message);
}
A reference to the sheet becomes #REF!. A reference through several sheets that started or ended on it starts or ends on the next sheet inwards. The names that belong to the sheet go with it, and so do its own charts, tables, comments and pivot tables. A pivot cache that a pivot table on another sheet still reads stays.
A sheet from another workbook
using DocWright.Spreadsheet;
// One sheet of another workbook. A sheet it refers to that is here (Summary) is
// meant by name; one that is not here (Rates) is #REF!, and is reported.
XlsxDocumentSheet brought = workbook.CopySheetFrom(report);
Console.WriteLine("Brought alone: " + brought.GetCell("B1").Formula);
The sheet's text comes into this workbook's table of strings, and each cell format it uses comes into this workbook's styles as the record it was, so nothing a format holds is lost. The names that belong to the sheet come with it. A name of the other workbook that the sheet uses, or that refers to it, comes too. When this workbook already has a name spelled so, it comes as a name that belongs to the copy, which is what Excel does. A table whose name is taken gets another, and a pivot table comes with the cache it reads. The other workbook is not changed.
Important
References to sheets left behind. Excel, copying a sheet between two open workbooks, turns each reference to another sheet of the source into a link to the source file. A workbook held as bytes has no file to link to. Here such a reference means the sheet of that name in this workbook when there is one, and is #REF! when there is none. The references lost that way are reported as one DXP1005 warning through the Diagnostics the receiving workbook was opened with. To keep them, bring the sheets together with Merge.
Merge two workbooks
using DocWright.Spreadsheet;
// Every sheet of another workbook: they come together, so they go on meaning each other.
XlsxDocument whole = XlsxDocument.Create(new XlsxCreateSettings { FirstSheetName = "Summary" });
IReadOnlyList<XlsxDocumentSheet> added = whole.Merge(other);
Console.WriteLine("Merged: " + string.Join(", ", whole.Sheets.Select(s => s.Name)) + "; the report reads " + added[1].GetCell("B1").Formula);
Merge returns the sheets it added. A sheet whose name is taken gets the name and (2), and references to it follow.
XlsxMergeOptions |
Default | Meaning |
|---|---|---|
IncludeHiddenSheets |
true |
Hidden and very hidden sheets come too, hidden as they were. With false they stay behind. |
What is refused
using DocWright.Spreadsheet;
try
{
workbook.Sheets[1].Name = "summary"; // another sheet has it, in another case
}
catch (ArgumentException refused)
{
Console.WriteLine(refused.Message);
}
Nothing has changed when a call throws.
| Call | Exception | |
|---|---|---|
A name another sheet has in any case, no name, more than 31 characters, one of : \ / ? * [ ], an apostrophe at either end |
ArgumentException |
Excel refuses each. A sheet's own name in another case is a rename. |
Delete on the only visible sheet |
InvalidOperationException |
Excel: "A workbook must contain at least one visible worksheet." |
MoveTo or CopyTo outside the list |
ArgumentOutOfRangeException |
|
| Any use of a deleted sheet | InvalidOperationException |
|
| A sheet between a Strict Open XML workbook and one that is not | UnsupportedFeatureException |
Their parts are written in different namespaces. |
| A sheet with a slicer, a timeline or a table fed by a query; a dialog or macro sheet between workbooks | UnsupportedFeatureException |
|
| A sheet one of whose parts is not well-formed XML | InvalidDocumentException |
Output
The copy is called Feb (2) and holds 'Feb (2)'!B1*2
After the rename: SUM('Jan 2026:Mar'!B1) and 'Jan 2026'!B1+Mar!B1
After the move: SUM('Jan 2026:Feb'!B1) in Mar, Jan 2026, Feb, Feb (2), Summary
After the delete: 'Jan 2026'!B1+#REF!
The sheet 'Mar' was deleted from its workbook.
Brought alone: #REF!*Summary!A1
Merged: Summary, Rates, Report; the report reads Rates!A1*Summary!A1
The workbook already has a sheet named 'summary' (names compare without regard to case). (Parameter 'name')
Download the workbook it wrote: sheets.xlsx.
Where the result differs from Excel's
| Excel | DocWright |
|---|---|
| Links a reference to a sheet left behind to the source file | Keeps it by name or writes #REF!, and reports it (above). |
| Selects the sheet it moved or copied | The workbook goes on opening on the sheet it opened on. |
Keeps a name of a deleted sheet that another sheet uses, as a name of the workbook that is #REF! |
The name goes with its sheet. The cell that used it shows the same error. |
Calls a copied table Table13, from a counter of its own |
The table's name and the first number from 2 that is free. |
| Keeps a cell's named style when a sheet goes to another workbook | The cell keeps its look and is Normal. |
Themes. As in Excel, a colour or a font that names the theme goes on naming it. In a workbook with another theme, a copied sheet shows that workbook's theme colours and fonts. A sheet that has to look the same everywhere needs colours of its own.
Nothing is calculated by these (call Calculate for that, Calculate formulas): after a delete, a copy, or a move that changed a reference, the saved workbook asks Excel to calculate when it is next opened. Each of these calls costs in proportion to the number of sheets, so adding thousands of sheets one by one takes seconds. To insert rows and columns, see Insert and delete rows and columns.