Table of Contents

Insert and delete rows and columns

InsertRows, DeleteRows, InsertColumns and DeleteColumns move the cells of a sheet, and everything that pointed at them still points at them, by Excel's own rules.

dotnet add package DocWright.Spreadsheet

Insert rows

using DocWright.Spreadsheet;

// Two rows at row 3: the rows below move down, and every formula follows.
orders.InsertRows(3, 2);
orders.SetValue("A3", XlsxCellValue.Text("Late order"));
orders.SetValue("B3", XlsxCellValue.Number(15m));

Console.WriteLine("The total is in B7: " + orders.GetCell("B7").Formula);
Console.WriteLine("The other sheet reads " + summary.GetCell("A1").Formula);

Rows and columns are numbered from one. The rows from at on move down. A range grows when rows are inserted inside it, and a formula on another sheet follows the cell it named.

Note

Checked against Excel. The same ten inserts and deletes were made in Excel and here, on a workbook with formulas, names, a table, a chart, a comment, a conditional format, a validation, a sparkline, shapes, page breaks and a frozen pane. The saved files agree cell by cell and place by place, apart from the differences listed at the end, and Excel opens all 96 workbooks written this way from the test files without repair.

What follows the cells

References Formulas on every sheet, defined names, print areas and print titles, chart series, table column formulas, conditional format and validation formulas, sparklines.
Ranges Merged ranges, tables, conditional formats, validations, the filter, hyperlinks' cells.
Rows and columns Row heights, hidden rows, outline levels, column widths.
Page A manual break moves with the row after it. A frozen pane grows when rows are inserted at or above its split.
Objects Comments, and the anchors of pictures, shapes and charts.
Your own changes Cells, heights, widths and merges set before the call move too.

Insert columns, and the format of what is new

using DocWright.Spreadsheet;

// A column before the amounts, with no format taken from its neighbours.
orders.InsertColumns(2, 1, new XlsxInsertOptions { CopyFormatFrom = XlsxCopyFormat.None });
orders.SetValue("B1", XlsxCellValue.Text("Customer"));

Console.WriteLine("The total is in C7: " + orders.GetCell("C7").Formula);
XlsxInsertOptions Default Meaning
CopyFormatFrom Before Before: the row above or the column to the left, as in Excel. After: the row below or the column to the right. None: no format.
MaxFormattedCells 1,000,000 The most cells written to give new rows or columns their format. Past it the call throws ResourceLimitExceededException.

A new row takes the copied row's height and style and the style of each of its formatted cells. No value is copied.

Delete

using DocWright.Spreadsheet;

orders.DeleteRows(2);                         // one order: the range loses the row
Console.WriteLine("After a row of the list is deleted: " + orders.GetCell("C6").Formula);

XlsxDocumentSheet copy = workbook.AddSheet("Check");
copy.SetFormula("A1", "Orders!A5");
orders.DeleteRows(5);                         // the row that formula points at
Console.WriteLine("A reference to a deleted cell: " + copy.GetCell("A1").Formula);

A range loses the rows deleted from it. A reference to a deleted cell becomes #REF!, with its sheet kept. A merged range, a conditional format, a validation, a hyperlink, a sparkline or a comment that sat wholly on the deleted cells is taken out.

What is refused

using DocWright.Spreadsheet;

orders.SetValue(1048576, 1, XlsxCellValue.Text("the last row"));
try
{
    orders.InsertRows(1);
}
catch (InvalidOperationException refused)
{
    Console.WriteLine(refused.Message);
}

Nothing has changed when a call throws.

Call Exception
An insert that would push a cell off the end of the sheet InvalidOperationException Excel refuses it too. A cell that holds only a format counts.
Rows or columns that hold part of an array formula UnsupportedFeatureException Excel: "You can't change part of an array." The whole array may be deleted.
A delete that takes a table's header or every data row, and not the whole table UnsupportedFeatureException Excel refuses the header.
A delete that takes a whole table; a column inserted in a table or deleted from one UnsupportedFeatureException Excel allows these. They are not done yet.
Rows or columns outside the grid ArgumentOutOfRangeException

Output

The total is in B7: SUM(B2:B6)
The other sheet reads Orders!B7*2
The total is in C7: SUM(C2:C6)
After a row of the list is deleted: SUM(C2:C5)
A reference to a deleted cell: Orders!#REF!
The rows cannot be inserted: that would push cells off the end of the sheet, and Excel refuses it. The sheet's last cell is in row 1048576.

Download the workbook it wrote: orders-rows.xlsx.

Where the result differs from Excel's

Excel DocWright
Splits a conditional format that straddles the inserted rows when its formula points at another row Keeps one range, rewritten for its first cell. The same when the formula points at its own row.
Fills the rows added to a table with the column's formula Adds no cell.
Keeps a shape set to move without sizing at its size, and one set not to move at its place on the page Keeps both at their place in the grid: the same unless the rows that came or went are not of the default height.
Does not rewrite a hyperlink's target The same.

Nothing is calculated by the insert (call Calculate for that, Calculate formulas): the saved workbook asks Excel to calculate when it is next opened. To read where a formula points, see Read an Excel formula as its parts.