Table of Contents

Write Excel files

XlsxWorkbook, the generator in DocWright.Spreadsheet, writes an Excel workbook from your own data in one pass: typed cells, number formats, fonts, fills, borders, merged ranges, column widths, frozen panes, outlines, autofilter and images. It has no dependencies, and the same workbook always produces the same bytes. For everything else (opening, editing, formulas and their calculation, tables, charts, pivot tables and the rest) use XlsxDocument: start with Open an existing Excel workbook or create one from nothing.

dotnet add package DocWright.Spreadsheet

A workbook

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

var book = new XlsxWorkbook();
var sheet = new XlsxWorksheet("Sales");
sheet.Columns.Add(new XlsxColumn(1, 1, 24m));   // column A, width in characters
sheet.Columns.Add(new XlsxColumn(2, 3, 14m));   // columns B-C

XlsxCellStyle header = XlsxCellStyle.Default.With(
    bold: true,
    backgroundColor: ColorRgb.FromRgb(0xEE, 0xF2, 0xFF),
    bottomBorder: new XlsxBorder(XlsxBorderStyle.Thin, ColorRgb.FromRgb(0x1E, 0x3A, 0x8A)));
XlsxCellStyle money = XlsxCellStyle.Default.With(numberFormat: "#,##0.00");
XlsxCellStyle date = XlsxCellStyle.Default.With(numberFormat: "d mmm yyyy");

var heading = new XlsxRow(1);
heading.Cells.Add(new XlsxCell(1, XlsxCellValue.Text("Region"), header));
heading.Cells.Add(new XlsxCell(2, XlsxCellValue.Text("Revenue"), header));
heading.Cells.Add(new XlsxCell(3, XlsxCellValue.Text("Closed"), header));
sheet.Rows.Add(heading);

(string Region, decimal Revenue, DateTime Closed)[] data =
[
    ("Auckland", 182_000m, new DateTime(2026, 9, 30)),
    ("Wellington", 264_500m, new DateTime(2026, 9, 28)),
    ("Canterbury", 200_750m, new DateTime(2026, 9, 29)),
];

int r = 2;
foreach (var (region, revenue, closed) in data)
{
    var row = new XlsxRow(r++);
    row.Cells.Add(new XlsxCell(1, XlsxCellValue.Text(region)));
    row.Cells.Add(new XlsxCell(2, XlsxCellValue.Number(revenue), money));   // a real number: it sums
    row.Cells.Add(new XlsxCell(3, XlsxCellValue.Date(closed), date));
    sheet.Rows.Add(row);
}

sheet.FrozenRows = 1;   // keep the header visible while scrolling
book.Worksheets.Add(sheet);

using (FileStream output = File.Create("sales.xlsx"))
{
    XlsxWriter.Write(book, output);
}

Download the result: sales.xlsx.

Values, not text

XlsxCellValue is Text, Number, Boolean, Date, Error or Blank. A Number with a #,##0.00 format displays like the formatted text and sums in Excel. Text that looks like a number sums to zero.

  • Numbers are decimal. A double is rounded to 15 significant digits, the precision Excel displays, so output is identical on .NET Framework and .NET. A double outside what decimal carries at 15 digits — above about 7.9E28, or below 1E-14 in magnitude — is written in scientific notation from those 15 digits and reports IsOutOfDecimalRange; it is never silently written as 0.
  • Date stores Excel's serial number, including Excel's 1900 leap-year quirk, so dates agree with Excel's own. A cell displays as a date only when its style has a date format.

Rules enforced up front

Sheet names must be 1–31 characters, without : \ / ? * [ ]. XlsxWorksheet throws for an invalid name, and XlsxSheetName.Normalize(text, fallback) and XlsxSheetName.MakeUnique(name, taken) produce valid ones from any text.

XlsxWriteSettings.MaxOutputBytes caps the output size, for workbooks whose size depends on data you don't control.

Not in the generator

Formulas, pivot tables, charts, conditional formatting, data validation and comments: a workbook you build with XlsxWorkbook has no member for them. XlsxDocument has all of them, and opens a workbook that already exists and saves it back with everything intact: see Open an existing Excel workbook, Calculate formulas, Charts and Pivot tables.

Tip

For complete examples (a formatted workbook, a reusable list-to-sheet export, and an import with validation), see the tutorial Generate, export and import Excel files.

Tip

To export a report to Excel, with number formats, grouping outlines and charts as pictures, use ReportXlsxExporter from DocWright.Reporting.Export, which builds on this writer. See Run an SSRS report.