DEV Community

Cover image for How to Import and Export Excel XLSX in .NET Applications Using C#
Chelsea Devereaux for MESCIUS inc.

Posted on • Originally published at developer.mescius.com

How to Import and Export Excel XLSX in .NET Applications Using C#

What You Will Need

Controls Referenced

Tutorial Concept

.NET C# Excel Import and Export - Learn how to import and export XLSX files from .NET applications from both the client-side and the server-side.


Importing and exporting Excel files is a crucial functionality for many .NET applications, enabling seamless data manipulation and sharing. In this tutorial, you'll learn how to handle XLSX files in .NET C# from both the desktop/client side in a WinForms application and alternatively programmatically import, modify, and save Excel files on the server.

This Blog Discusses C# Excel Import and Export Options for .NET Applications.

Desktop Client-Side C# Excel XLSX Import & Export

  1. Create a .NET 8 WinForms App
  2. Add the .NET C# Workbook Component to the App
  3. Desktop Client-Side Import & Export Options in C# Apps
  4. Invoke the Spreadsheet Designer During Runtime
  5. Add the .NET Spreadsheet Ribbon Bar Control

Server-Side C# Excel XLSX Import & Export

  1. Create a .NET 8 Console App
  2. Initialize a Server-Side C# Workbook Instance
  3. Invoke the Open Method to Read Existing Excel Files
  4. Invoke the Save Method to Write Excel Files

Client-Side C# Excel XLSX Import & Export

The client-side aspect of this piece focuses on two efficient solutions for importing and exporting XLSX files in .NET apps utilizing Spread WinForms. Learn how implementing a user-friendly button to access a Spreadsheet Designer App can improve user experience, or consider the benefits of incorporating a .NET Spreadsheet Ribbon Bar Control for streamlined data manipulation directly within the form. Join us as we explore these options, leveraging this .NET spreadsheet component to optimize client-side C# Excel import/export functionalities. Spread.NET also offers a WPF control - check out this other blog to learn how to implement import/export capabilities in a WPF application.

Create a .NET 8 WinForms App

In Visual Studio 2022, create a new project and select C# Windows Forms App (.NET Framework).

Create New C# Windows Forms App

You can download a fully functional 30-day trial version of Spread.NET directly from our website. You can also install the Spread.NET NuGet package. In the Solution Explorer, right-click Dependencies and select Manage NuGet Packages. In NuGet Package Manager, select NuGet.org as the Package source. Search for 'Spread.WinForms,' select GrapeCity.Spread.WinForms, and click Install.

Install Spread WinForms

Add the Client-Side C# Workbook UI to the App

From the VS Toolbox, drag the FpSpread component and drop it onto the form. Adjust the spreadsheet as needed.

Client-Side C# Workbook

Check out the Adding a Component to a Project documentation for additional information.

Client-Side Import & Export Options in C# .NET Apps

Invoke the Spreadsheet Designer During Runtime

Spread’s Designer App can be launched during the .NET app's runtime to allow end-users to import or export Excel XLSX files. Developers can optionally add a button that, when clicked, invokes the ShowDialog method which will display the Spread Designer as a modal dialog box for the specific .NET spreadsheet instance. 

To accomplish this, first drag and drop the FpSpreadDesigner component in the form in addition to the FpSpread component.

Invoke Spreadsheet Designer

In the Form1.cs file, invoke the ShowDialog method within the button click event like so:

    private void button1_Click(object sender, EventArgs e)
    {
      fpSpreadDesigner1.ShowDialog(fpSpread1);
    }
Enter fullscreen mode Exit fullscreen mode

The Spread Designer App can now be launched during the WinForms App's runtime, allowing end-users to easily import/export Excel files.

SPNET Designer App IO Excel

Add a .NET Spreadsheet Ribbon Bar Control 

Spread WinForms includes a spreadsheet Ribbon Bar control that can easily be added to the app by dragging and dropping the RibbonBar component from the VS Toolbox in the form.

Ribbon Bar Control

In the Form1.cs file, add the following code to generate the default RibbonBar items and behaviors, and attach the control to the existing spreadsheet component.

    using GrapeCity.Spreadsheet.WinForms.Ribbon;

    namespace SPNET_Import_Export_Excel_2024
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                ribbonBar1.GenerateDefaultItems();
                // Attach ribbonBar control to FpSpread control
                ribbonBar1.Attach(fpSpread1);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

The Ribbon Bar now provides a familiar UI directly in the form, consisting of default tabs, groups, and group items. This includes a File tab that allows users to easily choose to Open or Save XLSX files.

SPNET RibbonBar Control IO Excel

To learn more, check out the How to Add a .NET Spreadsheet Ribbon Bar Control using C# blog.


Server-Side C# Excel XLSX Import & Export

In the server-side portion of this blog, we will discuss how to utilize the C# Excel library, Document Solutions for Excel (DsExcel .NET), to enhance data processing. Learn how to efficiently handle Excel files by initializing the .NET Excel API Workbook, reading existing XLSX files, and saving or writing new ones. Join us as we demonstrate the capabilities of .NET Excel API libraries in improving server-side Excel import/export functions, ultimately transforming how data is managed within your applications.

Create a .NET 8 Console App

In Visual Studio 2022, create a new Visual C# Console App (.NET Core).

Visual C# Console App

Add the DsExcel .NET references to the project. In the Solution Explorer, right-click Dependencies and select Manage NuGet Packages. In NuGet Package Manager, select NuGet.org as the Package source. Search for 'ds.documents,' select DS.Documents.Excel, and click Install.

DsExcel NuGet Package Manager

Initialize a C# Excel Workbook Instance

In the Program.cs file, initialize a new C# Excel workbook using the Workbook class.

    using GrapeCity.Documents.Excel;

    // Create a new workbook
    Workbook workbook = new Workbook();
Enter fullscreen mode Exit fullscreen mode

Invoke the Open Method to Read Existing Excel Files

DsExcel’s API offers an Open method to read existing Excel files in the server-side workbook.

    // Open XLSX file
    workbook.Open("Profit loss statement.xlsx");
Enter fullscreen mode Exit fullscreen mode

Invoke the Save Method to Write Excel Files

The C# Excel library also offers a Save method to write the server-side workbook to an external XLSX file.

    // Modify the XLSX file
    workbook.Worksheets[0].Range["C1"].Value = "2024";
    workbook.Worksheets[0].Range["C1"].Formula = "=Now()";
    workbook.Worksheets[0].Range["C1"].NumberFormat = "m/d/yy";

    // Save workbook as XLSX file
    workbook.Save("Profit loss statement 2024.xlsx");
Enter fullscreen mode Exit fullscreen mode

Profit Loss Statement

For more information, see the DsExcel .NET documentation on opening and saving workbooks, as well as the online demos for saving to Excel and importing Excel files.

Check out our other blogs that discuss Importing & Exporting Excel files using different development frameworks: WPF | JavaScript | React | Angular | Vue


Learn More About These .NET Spreadsheet Solutions

This article only scratches the surface of the full capabilities of Spread.NETand DsExcel .NET. To learn more about the .NET client-side spreadsheet component, see Spread.NET’s demo explorers and online documentation. To learn more about the C# Excel library, see DsExcel .NET’s online demo explorer and documentation.

Top comments (0)