dotnet add package \ RecursiveAI.OneDocument.Spreadsheets
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Create(); book.Sheets[0]["A1"].SetString("Hello"); book.SaveAs("hello.xlsx");
Writes hello.xlsx with Hello in A1.
Download this example (.zip): unzip, then dotnet run.
The full program
The download makes a budget workbook with two items, a SUM formula and currency formatting, then reopens it to read the saved total.
using RecursiveAI.OneDocument.Spreadsheets; using (Workbook made = Workbook.Create()) { Worksheet sheet = made.Sheets[0]; sheet.Rename("Budget"); sheet.Range("A1:B3").Values = new object?[,] { { "Item", "Cost" }, { "Ladder", 189.5 }, { "Bulbs", 42 } }; sheet["A4"].SetString("Total"); sheet["B4"].Formula = "SUM(B2:B3)"; sheet.Range("B2:B4").NumberFormat = "$#,##0.00"; made.Recalculate(); made.SaveAs("budget.xlsx"); } using Workbook check = Workbook.Open("budget.xlsx"); Console.WriteLine("Wrote budget.xlsx"); Console.WriteLine($"Budget total: {check.Sheets["Budget"]["B4"].FormattedText}");
dotnet run prints:
Wrote budget.xlsx Budget total: $231.50
A formula is not calculated just because it is assigned: this program explicitly calls Recalculate before saving. If the output directory does not exist, saving raises DirectoryNotFoundException; create it first.
Variations
Write a rectangular block of values.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Create(); book.Sheets[0].Range("A1:B2").Values = new object?[,] { { "Item", "Cost" }, { "Bulbs", 42 } }; book.SaveAs("items.xlsx");
Writes items.xlsx with a header and one data row.
Related recipes
- Convert DOCX to PDF in C#
- Read an Excel file in C#
- Read a Word DOCX document in C#
- Convert Excel to PDF in C#
The short answer
To create an Excel XLSX file in C#, install OneDocument’s Spreadsheets package, call Workbook.Create, write values into its first worksheet, and call SaveAs with an .xlsx path. The snippet at the top creates hello.xlsx with one text cell. The download shows the next useful step: a small budget with data, formatting and a formula whose result is saved in the file.
Keep creation, calculation and saving separate in your code. They do different jobs. Writing a value edits the workbook model; assigning a formula stores the expression; Recalculate computes results; SaveAs writes the output file. That separation makes it clear whether the workbook you hand to another program contains current calculated results.
How can I create an Excel spreadsheet using C#?
A new workbook already has a worksheet. Use Sheets[0] to get it, then Rename if your output needs a stable name. The full example calls it Budget. Address individual cells with A1 coordinates, or assign a rectangular object?[,] array to a matching range. The array’s rows and columns correspond to the target rectangle; the variation demonstrates a header and one data row.
Use SetString for text and numeric values for amounts you want to calculate. A string that looks like a currency value is still text. In this example, 189.5 and 42 are numbers; the dollar sign comes from the cell’s number format. The spreadsheet documentation explains the value kinds and how cell values are stored.
Saving is explicit. SaveAs writes the file; using only frees memory. Keep the generated file’s path available to the next step in your application, whether that step is downloading the workbook, reading it back, or converting it to a printable report.
Can I create an Excel file in C# without installing Excel?
Yes. This code writes the workbook through OneDocument, not through Excel automation. The package is a .NET 10 library with no native dependencies. The downloadable project references the published 1.0.0 package and makes all its own data; no desktop application or template file is part of the example.
Use the .xlsx extension for this output. Do not turn a CSV into an XLSX by renaming it: writing a workbook means producing its workbook and worksheet structure, not just comma-separated text. OneDocument’s save path selects the output format. If you want another supported format, choose it deliberately and check what that format can represent.
How do I add formulas to an Excel file in C#?
The full program assigns SUM(B2:B3) to B4, without a leading equals sign, then calls Recalculate. It applies $#,##0.00 to B2:B4 so that the two amounts and total display consistently. Reopening the saved file returns $231.50 for the total, which is the result of this sample’s two numeric inputs.
Calculation and formatting are independent. A number format changes how a value is shown, not the number that SUM uses. Similarly, storing a formula is not the same as running the calculation engine. Recalculate before exporting the workbook to a format that reads cached results, such as PDF.
There is also a language constraint: the README states, “Formula text is en-US and invariant only; localized function names are not supported.” Write formulas using the documented invariant syntax, even if the person opening the workbook uses a localized Excel interface. Test the particular functions your workbook needs instead of assuming that every possible Excel formula is interchangeable.