C# for .NET XLSX, ODS and CSV No Office needed

The C# Excel Library for XLSX Files

sgcDocs is a C# Excel library that creates and reads XLSX files with formulas, styles and number formats, with no Office installed, on Windows, Linux and macOS.

320 Excel functions calculated Fonts, fills, borders, number formats ASP.NET Core, services, Linux DOCX and PDF in the same API

Included in sgcUI .NET Enterprise. See pricing →

using esegece.sgcDocs;

using (var sheet = new TsgcDocsSpreadsheet())
{
    sheet.New();
    sheet.SetValues("A1", "Units");
    sheet.SetValues("A2", 12);
    sheet.SetValues("A3", 30);
    sheet.SetFormulas("A4", "=SUM(A2:A3)");
    sheet.Calculate();
    sheet.SaveToFile("units.xlsx");
    sheet.ExportToPDFFile("units.pdf");
}

How to Create an Excel File in C#

To create an Excel file in C# without Excel, create a TsgcDocsSpreadsheet, call New, write values and formulas by cell address, format the cells and call SaveToFile. The format comes from the file extension, so the same workbook also saves as ODS or CSV.

using System;
using esegece.sgcDocs;
using esegece.sgcGrid;

static class SalesReport
{
    public static void Create()
    {
        using (var sheet = new TsgcDocsSpreadsheet())
        {
            sheet.New();
            sheet.RenameSheet(0, "Sales");

            // a header row and three data rows, from one and two dimensional arrays
            sheet.SetRange("A1", new object[] { "Product", "Units", "Price", "Total" });
            sheet.SetRange("A2", new object[,]
            {
                { "Paper", 12, 4.5 },
                { "Toner", 3, 39.9 },
                { "Labels", 20, 2.75 }
            });

            // formulas, calculated by the engine and saved with the file
            sheet.SetFormulas("D2", "=B2*C2");
            sheet.SetFormulas("D3", "=B3*C3");
            sheet.SetFormulas("D4", "=B4*C4");
            sheet.SetValues("A5", "Total");
            sheet.SetFormulas("D5", "=SUM(D2:D4)");

            // a bold, centred header with a fill colour (ARGB)
            TsgcGridCellFormat header = sgcGrid_CellFormat.sgcGridCellFormatEmpty();
            header.Bold = true;
            header.Alignment = TsgcGridAlignment.gaCenter;
            header.FillColor = 0xFFDDEBF7;
            sheet.SetFormat("A1:D1", header);

            // a number format for the money columns
            TsgcGridCellFormat money = sgcGrid_CellFormat.sgcGridCellFormatEmpty();
            money.NumberFormat = "#,##0.00";
            sheet.SetFormat("C2:D5", money);

            // column widths in DIPs
            sheet.SetColumnWidths("A", 140);
            sheet.SetColumnWidths("D", 90);

            sheet.Calculate();
            Console.WriteLine("Total: " + sheet.Text("D5"));
            sheet.SaveToFile("sales.xlsx");
        }
    }
}

The file keeps the formulas and their calculated values. Text returns a cell as it is printed, with its number format applied. The C# class keeps the names of the Delphi edition: an indexed property such as Values['A1'] becomes the method pair Values and SetValues, and the same goes for Formulas, ColumnWidths and RowHeights.

Read and Update an XLSX File in C#

LoadFromFile and LoadFromStream load XLSX, ODS and CSV with values, formulas, styles, merged cells, layout, page setup, protection, charts and the conditional formats of XLSX files. Change what you need, calculate again and save.

using (var sheet = new TsgcDocsSpreadsheet())
{
    sheet.LoadFromFile("sales.xlsx");
    Console.WriteLine(sheet.SheetNames(0) + ": " + sheet.UsedRange());
    Console.WriteLine("D2 formula: " + sheet.Formulas("D2"));
    Console.WriteLine("Units of B3: " + sheet.Values("B3"));

    // change a value, calculate again, save and print to PDF
    sheet.SetValues("B3", 5);
    sheet.Calculate();
    Console.WriteLine("New total: " + sheet.Text("D5"));
    sheet.SaveToFile("sales_updated.xlsx");
    Console.WriteLine(sheet.ExportToPDFFile("sales.pdf") + " PDF page(s)");
}

Values returns an object: null for an empty cell, a DateTime for a date and the error text, such as #DIV/0!, for an error. Evaluate runs any formula against the workbook without writing it to a cell.

Formulas, Styles and Data in Your XLSX Files

The workbook engine behind TsgcDocsSpreadsheet is managed C# code. It needs no Excel, no COM interop and no native library on the machine that runs it.

320 Excel functions

A text that starts with = is a formula. The engine calculates 320 Excel functions, LET, LAMBDA and named LAMBDA functions included, and DefineName adds workbook names such as VAT or a reusable LAMBDA.

Styles and number formats

SetFormat sets the font size, bold, italic, colours, fill, alignment, wrapping, number format, borders, indent and rotation of a range. MergeCells merges a range.

Layout and sheets

Column widths and row heights, AddSheet, RenameSheet, which rewrites the formulas that refer to the sheet, DeleteSheet, and sheet and workbook protection.

Data from your database

ImportDataSet copies a DataTable or an IDataReader into a sheet, with the column captions or field names as the header row, and returns the number of rows.

Charts kept

Formats, merged cells, charts and conditional formats are kept when an XLSX file is loaded and saved again, and the charts of a sheet are printed with it.

PDF, HTML and PNG output

ExportToPDFFile prints one sheet or every visible sheet with the page setup of the workbook, ExportToHTML writes an HTML table per sheet, and ExportSheetPageToPNG renders one printed page.

Excel Files in ASP.NET Core and on Linux

The package runs in console programs, services, ASP.NET Core and desktop applications on Windows, Linux and macOS. SaveToStream writes the workbook to any stream, so a web API returns an XLSX file without a temporary file.

using esegece.sgcDocs;

var app = WebApplication.CreateBuilder(args).Build();

// GET /report.xlsx returns a workbook built for the request
app.MapGet("/report.xlsx", () =>
{
    var stream = new MemoryStream();
    using (var sheet = new TsgcDocsSpreadsheet())
    {
        sheet.New();
        sheet.SetValues("A1", "Generated");
        sheet.SetValues("B1", DateTime.Now);
        sheet.SaveToStream(stream, TsgcDocsFormat.dfXLSX);
    }
    stream.Position = 0;
    return Results.File(stream,
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "report.xlsx");
});

app.Run();

The components are not thread safe: create the ones a request needs in the thread that serves it, and separate instances run in parallel. On Linux and in containers, install the TrueType fonts your documents use, or add a font folder with AddFontFolder. The .NET demos include ConversionServer, an ASP.NET Core minimal API that converts documents on a server.

Also a C# DOCX Library: Word and PDF Files

The same package creates and reads Word documents. TsgcDocsWordProcessor reads and writes DOCX, RTF, HTML, Markdown and text, runs mail merges from a DataTable, CSV or JSON, and exports to PDF. TsgcDocsConverter converts between documents, spreadsheets, PDF files and images in one call.

// a Word document saved as DOCX and PDF
using (var word = new TsgcDocsWordProcessor())
{
    word.New();
    word.Title = "Quarterly report";
    word.AppendParagraph("Quarterly report", "Heading 1");
    word.AppendParagraph("Sales grew 12% in the third quarter.");
    word.SaveToFile("report.docx");
    word.ExportToPDFFile("report.pdf");
    Console.WriteLine("Pages: " + word.PageCount);
}

// one call conversions, the formats come from the file extensions
using (var converter = new TsgcDocsConverter())
{
    converter.Convert("sales.xlsx", "sales.csv");
    converter.Convert("report.docx", "report.html");
}

Word documents

Fill a DOCX template made in Word with ReplaceAll, or merge it with your data, and save the result as DOCX or PDF.

PDF processing

TsgcDocsPDFProcessor merges, splits and rotates pages, extracts text, fills forms, encrypts with AES-256, converts to PDF/A and signs.

Barcodes and ZIP

TsgcDocsBarcode writes QR Code, Data Matrix, Code 128 and more as PNG, SVG or on a PDF page, and TsgcDocsZipArchive builds ZIP64 archives.

Install the C# Excel Library from NuGet

Add the package esegece.sgcDocs to the project. NuGet adds the engines it builds on: esegece.sgcUICore, esegece.sgcPDF, esegece.sgcRichEdit, esegece.sgcGrid, esegece.sgcSpreadsheet and esegece.sgcCharts.

dotnet add package esegece.sgcDocs

esegece.sgcDocs is a Registered package, included in sgcUI .NET Enterprise and All-Access, and it is not published on nuget.org. PNG and SVG pages of documents and sheets need one more package, esegece.sgcUICore.Rendering, so servers that only write XLSX, DOCX and PDF files do not carry native rendering libraries.

PackageTarget frameworksRuns on
esegece.sgcDocs.NET Standard 2.0, .NET Framework 4.7.2, .NET 6, 8, 9 and 10Windows, Linux and macOS, in console programs, services, ASP.NET Core and desktop applications
esegece.sgcUICore.RenderingAdded only when neededPNG and SVG pages of documents and sheets

Where the C# Excel Library Is Included

sgcDocs is not sold on its own and has no free Community edition. sgcUI .NET Enterprise includes the full C# source code and 1 year of updates.

sgcUI .NET Enterprise

Every sgcUI product for .NET, sgcDocs included, with full C# source code for WinForms, WPF and Avalonia, from €699 per developer.

All-Access

Every eSeGeCe product, sgcUI Enterprise and sgcDocs included.

See All-Access →

Also for Delphi and C++ Builder

The same API with the same class names for Delphi 7 to 13 and C++ Builder 2007 to 13, on Win32, Win64 and Linux64.

sgcDocs for Delphi →

Frequently Asked Questions

Add the esegece.sgcDocs package, create a TsgcDocsSpreadsheet, call New, write cells with SetValues and SetFormulas, and call SaveToFile("report.xlsx"). The workbook is written by managed C# code, so neither Excel nor Office has to be installed on the machine.

Yes. A text that starts with = is a formula, and the engine calculates 320 Excel functions before the file is saved, so the XLSX holds both the formulas and their values. SetFormat sets fonts, colours, fills, alignment, wrapping, borders and number formats on a range, and SetColumnWidths and SetRowHeights size the layout.

Yes, XLSX, ODS and CSV files. LoadFromFile or LoadFromStream loads the values, formulas, styles, merged cells, page setup, protection and charts, and Values, Formulas and Text read any cell. The binary XLS format is not supported.

Yes. The package targets .NET Standard 2.0, .NET Framework 4.7.2 and .NET 6 to 10, and runs in console programs, services, ASP.NET Core and desktop applications on Windows, Linux and macOS. Create one component per request, because the components are not thread safe, and install the TrueType fonts your documents use on the server.

Yes. TsgcDocsWordProcessor in the same package creates, reads and edits DOCX, RTF, HTML and Markdown documents, fills templates, runs mail merges and exports to PDF. TsgcDocsPDFProcessor merges, encrypts, converts to PDF/A and signs PDF files.

No. sgcDocs has no free Community edition. The .NET package is Registered only, it is included in sgcUI .NET Enterprise and All-Access, and it is not published on nuget.org. For a .NET evaluation, contact us.

Yes. The sgcUI .NET Enterprise license includes the full C# source code and 1 year of updates. See the sgcUI pricing page.

Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Create Excel Files from Your C# Code

XLSX, DOCX and PDF from one .NET package, with no Office on the machine.