Table of Contents

Start an Excel workbook from nothing

XlsxDocument.Create() gives an empty workbook with one sheet, in the same type that opens an existing workbook. Everything that can be done to an opened workbook can be done to this one: cells, formulas, looks, widths, merged ranges, more sheets, find and replace.

dotnet add package DocWright.Spreadsheet

Create, fill, save

using DocWright.Core.Primitives;
using DocWright.Spreadsheet;

XlsxDocument book = XlsxDocument.Create();
XlsxDocumentSheet sheet = book.Sheets[0];

// Start every look from the workbook's own default, so the font stays the workbook's.
XlsxCellStyle heading = book.DefaultStyle.With(bold: true, backgroundColor: ColorRgb.FromRgb(221, 235, 247));
XlsxCellStyle money = book.DefaultStyle.With(numberFormat: "#,##0.00");
XlsxCellStyle day = book.DefaultStyle.With(numberFormat: "yyyy-mm-dd");

string[] titles = ["Item", "Price", "Ordered"];
for (int column = 1; column <= titles.Length; column++)
{
    sheet.SetValue(1, column, XlsxCellValue.Text(titles[column - 1]));
    sheet.SetStyle(1, column, heading);
}

sheet.SetValue("A2", XlsxCellValue.Text("Tea"));
sheet.SetValue("B2", XlsxCellValue.Number(4.5m));
sheet.SetValue("C2", XlsxCellValue.Date(new DateTime(2026, 11, 17)));
sheet.SetValue("A3", XlsxCellValue.Text("Coffee"));
sheet.SetValue("B3", XlsxCellValue.Number(6.25m));
sheet.SetValue("C3", XlsxCellValue.Date(new DateTime(2026, 11, 18)));
sheet.SetFormula("B4", "SUM(B2:B3)");

foreach (string cell in new[] { "B2", "B3", "B4" })
{
    sheet.SetStyle(cell, money);
}

sheet.SetStyle("C2", day);
sheet.SetStyle("C3", day);
sheet.SetColumnWidth(1, 18m);
book.AddSheet("Notes").SetValue("A1", XlsxCellValue.Text("Prices include tax."));

using (FileStream output = File.Create("new-workbook.xlsx"))
{
    book.Save(output);
}

Console.WriteLine($"sheets: {string.Join(", ", book.Sheets.Select(s => s.Name))}");
Console.WriteLine($"default look: {book.DefaultStyle.FontFamily} {book.DefaultStyle.FontSizePoints}");
Console.WriteLine($"B4 formula: {sheet.GetCell("B4").Formula}");

It prints the first three lines below; the rest come from the samples further down this page.

sheets: Sheet1, Notes
default look: Aptos Narrow 11
B4 formula: SUM(B2:B3)
two new workbooks are the same bytes: True
  [Content_Types].xml
  _rels/.rels
  xl/workbook.xml
  xl/_rels/workbook.xml.rels
  xl/worksheets/sheet1.xml
  xl/theme/theme1.xml
  xl/styles.xml
Ledger: Courier New 10.5
A1 is 2026-11-17, stored as 44881 in the 1904 system
refused: The default font size must be from 1 to 409 points, which is Excel's range; 410 is outside it. (Parameter 'settings')
from: Region = 1234.5, modified: False
after: Area = 2000, modified: True

Download the result and open it in Excel: new-workbook.xlsx. Excel opens it as it opens its own files, with no repair, and shows 10.75 in B4.

Important

Nothing is calculated unless you call Calculate (Calculate formulas). B4 holds the formula and no result. The saved workbook asks the application that opens it to calculate, and Excel does.

  • Start a look from book.DefaultStyle and not from XlsxCellStyle.Default. DefaultStyle is the look of a cell that states none in this workbook; XlsxCellStyle.Default is the default of the write-only model (Calibri 11) and would give the cell another font from the cells around it.
  • A date is a number. Give the cell a date number format for Excel to show it as a date.
  • IsModified is false on a new workbook until something is changed.

What a new workbook holds

using System.IO.Compression;
using DocWright.Spreadsheet;

byte[] first = XlsxDocument.Create().ToArray();
byte[] second = XlsxDocument.Create().ToArray();
Console.WriteLine($"two new workbooks are the same bytes: {first.AsSpan().SequenceEqual(second)}");

using var package = new ZipArchive(new MemoryStream(first), ZipArchiveMode.Read);
foreach (ZipArchiveEntry entry in package.Entries)
{
    Console.WriteLine($"  {entry.FullName}");
}

A new workbook is the shape of Excel's own new workbook, less anything that identifies a save:

Part Holds
xl/workbook.xml, xl/worksheets/sheet1.xml One empty sheet named Sheet1.
xl/styles.xml One font (Aptos Narrow 11), the two fills Excel reserves, one border, the Normal style.
xl/theme/theme1.xml The colours and the two font names of Excel's current theme, so a theme colour resolves to what it resolves to in Excel.
Content types and relationships What ties the parts together.

There are no document properties, no window settings and no ids, so two new workbooks are the same bytes. Excel adds its own document properties when it saves the file.

Why a style part at all: Excel opens a workbook that has none, but it then assumes its own standard font, and its first save writes eighteen fonts and its whole built-in style set. Given this style part and a theme, Excel keeps the one font. That was measured with Excel, not taken from the specification.

The settings

using DocWright.Spreadsheet;

XlsxDocument ledger = XlsxDocument.Create(new XlsxCreateSettings
{
    FirstSheetName = "Ledger",
    DefaultFontFamily = "Courier New",
    DefaultFontSizePoints = 10.5m,
    Uses1904DateSystem = true,
});
ledger.Sheets[0].SetValue("A1", XlsxCellValue.Date(new DateTime(2026, 11, 17)));

XlsxDocumentCell stored = ledger.Sheets[0].GetCell("A1");
Console.WriteLine($"{ledger.Sheets[0].Name}: {ledger.DefaultStyle.FontFamily} {ledger.DefaultStyle.FontSizePoints}");
Console.WriteLine($"A1 is {stored.ToDateTime():yyyy-MM-dd}, stored as {stored.Value.NumberValue} in the 1904 system");

try
{
    XlsxDocument.Create(new XlsxCreateSettings { DefaultFontSizePoints = 410m });
}
catch (ArgumentException refusal)
{
    Console.WriteLine($"refused: {refusal.Message}");
}
Setting Default Accepted
FirstSheetName Sheet1 A name Excel accepts (XlsxSheetName.IsValid): 1 to 31 characters, none of : \ / ? * [ ], no apostrophe at either end. A tab, a line break or a single space is accepted, as Excel accepts it.
DefaultFontFamily Aptos Narrow 1 to 31 characters, no control character. A family other than the theme's body font is written by name; the theme keeps its fonts.
DefaultFontSizePoints 11 1 to 409.
Uses1904DateSystem false The same day is a number 1,462 lower in the 1904 system. SetValue stores a date in the workbook's own system, and ToDateTime() gives the day back.

What Excel would refuse to open is refused at the call, with an ArgumentException that says why. Excel was measured to refuse a workbook whose font name is empty or 32 characters long, and one whose font size is below 1 or is 410.

Carry a generated workbook into it

A workbook built with XlsxWorkbook is written once and cannot be read back or edited. XlsxDocument.From carries it into the model that can:

using DocWright.Spreadsheet;

var generated = new XlsxWorkbook();
var report = new XlsxWorksheet("Report");
var row = new XlsxRow(1);
row.Cells.Add(new XlsxCell(1, XlsxCellValue.Text("Region"), XlsxCellStyle.Default.With(bold: true)));
row.Cells.Add(new XlsxCell(2, XlsxCellValue.Number(1234.5m), XlsxCellStyle.Default.With(numberFormat: "#,##0.00")));
report.Rows.Add(row);
generated.Worksheets.Add(report);

XlsxDocument editable = XlsxDocument.From(generated);
Console.WriteLine($"from: {editable.Sheets[0].GetCell("A1").Value.TextValue} = {editable.Sheets[0].GetCell("B1").Value.NumberValue}, modified: {editable.IsModified}");

editable.Sheets[0].SetValue("B1", XlsxCellValue.Number(2000m));
editable.ReplaceText("Region", "Area");
File.WriteAllBytes("report.xlsx", editable.ToArray());
Console.WriteLine($"after: {editable.Sheets[0].GetCell("A1").Value.TextValue} = {editable.Sheets[0].GetCell("B1").Value.NumberValue}, modified: {editable.IsModified}");

Download: report.xlsx.

  • From holds exactly the package XlsxWriter writes for the workbook. Saved untouched, it is those parts to the byte.
  • The generated workbook is read, not kept: changing it afterwards changes nothing in the XlsxDocument.
  • The other direction does not exist. An opened or created workbook is not turned into an XlsxWorkbook, which holds only what XlsxWriter can write.

Which way to make a workbook

You have Use
Rows of data to write once, as fast as possible XlsxWorkbook and XlsxWriter
A workbook to build step by step, read back, search or edit XlsxDocument.Create()
A workbook somebody designed, to fill in XlsxDocument.Open
A generated workbook that now needs an edit XlsxDocument.From(workbook)
A DataTable, a list or an array to put into either sheet.ImportData