ClosedXML.Report Documentation

repository·develop·Indexed 20 days ago

https://github.com/closedxml/closedxml.report

A .NET library for generating Microsoft Excel reports using XLSX templates. It allows developers to export .NET data into formatted Excel files via the XLTemplate class without requiring Excel to be installed on the host machine. Key features include support for vertical and horizontal tables, conditional formatting, pivot tables, and dynamic formulas using C# and LINQ syntax.

Tokens
684
Snippets
2
Records
3
Agent score
21%

What's inside ClosedXML.Report

  1. Core features of ClosedXML.Report

    develop

    ClosedXML.Report provides several advanced Excel capabilities for report generation:

    • Formatting: Copying cell formatting and propagating conditional formatting.
    • Data Layout: Support for vertical and horizontal tables, subranges, and pivot tables.
    • Formulas: Ability to implement Excel formulas and use dynamically calculated formulas using C# and LINQ syntax.
    • Tabular Operations: Built-in support for sorting, grouping, and total functions on tabular data.
  2. Generate reports using XLTemplate

    develop

    ClosedXML.Report uses an XLSX file as a template. You can design your report in Microsoft Excel (applying formatting, conditional formatting, pivot tables, etc.) and then use the XLTemplate class to inject .NET data into that template.

    Workflow:

    1. Create a Template: Design an Excel file (.xlsx) with your desired layout and formatting.
    2. Load Template: Instantiate XLTemplate with the path to your template file.
    3. Add Data: Use template.AddVariable(object) to pass .NET objects/data to the template.
    4. Generate: Call template.Generate() to process the data into the template.
    5. Save: Use template.SaveAs(path) to export the final report.
    protected void Report()
    {
        const string outputFile = @".\Output\report.xlsx";
        var template = new XLTemplate(@".\Templates\report.xlsx");
    
        using (var db = new DbDemos())
        {
            // Load data from your source
            var cust = db.customers.LoadWith(c => c.Orders).First();
            
            // Map the data to the template
            template.AddVariable(cust);
            
            // Execute the generation logic
            template.Generate();
        }
    
        // Save the resulting file
        template.SaveAs(outputFile);
    
        // Optional: Open the report
        Process.Start(new ProcessStartInfo(outputFile) { UseShellExecute = true });
    }