Windows Forms: How to Generate Excel Report in C#

This post shows you How to generate excel report using ClosedXml.Report in C# Windows Forms Application.

Creating a new Windows Forms Application project, then open your form designer.

Next, Drag the Button, TextBox, Label and DataGridView control from the Visual Studio toolbox into your form designer. You can design a simple UI that allows you to select an excel template file, then fill data and generate an excel report from based on the excel template file.

After you finish designing the form, you need to right click on your project, then select Manage Nuget Packages.

c# generate excel report

Next, Search and Install ClosedXml.Report to your project.

closedxml.report example

ClosedXML.Report is a tool for report generation and data analysis in .NET applications through the use of Microsoft Excel. ClosedXML.Report is a .NET-library for report generation Microsoft Excel without requiring Excel to be installed on the machine that's running the code.

Right-clicking on your project, then add an ADO.NET Entity Data Model to your project.

We will use the Customer table in the Northwind database to practice this demo

Next, You need to rebuild your project, then add a bindingsource to your DataGridView.

Adding a Form_Load event handler that allows you to retrieve customer data from the Northwind database, then set datasource to the bindingsource.

private void Form1_Load(object sender, EventArgs e)
{
    NorthwindEntities db = new NorthwindEntities();
    customerBindingSource.DataSource = db.Customers.ToList();
}

Adding a click event handler to the Browse button that allows you to get the excel template name.

private void btnBrowse_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xlsx", Multiselect = false })
    {
        if (ofd.ShowDialog() == DialogResult.OK)
            txtFileName.Text = ofd.FileName;
    }
}

Finally, Add a click event handler to the Generate button that allows you to generate excel report based on row you select from DataGridView.

//generate excel report using c#
private void btnGenerate_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(txtFileName.Text))
    {
        MessageBox.Show("Please select your template file.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
    }
    using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Excel Workbook|*.xlsx", ValidateNames = true })
    {
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            try
            {
                var template = new XLTemplate(txtFileName.Text);
                var customer = customerBindingSource.Current as Customer;
                if (customer != null)
                {
                    template.AddVariable(customer);
                    template.Generate();
                    template.SaveAs(sfd.FileName);
                }
                MessageBox.Show("You have successfully created an excel report.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

Next, Create an excel template report with data fields you want to fill data.

c# excel template report

This is a simple c# example help you generate excel reports from the excel template file in c# using ClosedXml.Report library.

Related Posts