How to Import and Export Excel XLSX in .NET Applications Using C#
Tags: webdev, devops, csharp, tutorial
Published: 2024-06-28 20:05:38

---
canonical_url: https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-in-net-applications-using-c-sharp
description: Learn how to import and export Excel XLSX in .NET applications using C#. See more from Spread.NET and Document Solutions today.
---
**What You Will Need**
- Visual Studio (This example uses VS2022)
- .NET 8
- NuGet Packages:
1. [GrapeCity.Spread.WinForms](https://www.nuget.org/packages/GrapeCity.Spread.WinForms)
2. [DS.Documents.Excel](https://www.nuget.org/packages/DS.Documents.Excel)

**Controls Referenced**
- [Spread.NET - .NET/C# Spreadsheet Component](https://developer.mescius.com/spreadnet)
- [Online Documentation](https://developer.mescius.com/spreadnet/docs) | [Demos](https://developer.mescius.com/spreadnet/demos)
- [Document Solutions for Excel, .NET Edition - .NET/C# Excel API Library](https://developer.mescius.com/document-solutions/dot-net-excel-api/)
- [Online Documentation](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html) | [Demos](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/importexcelfiletoworkbook)

**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](#Client)

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

### [Server-Side C# Excel XLSX Import & Export](#Server)

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

* * *

## <a id="Client"></a>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](https://developer.mescius.com/spreadnet/winforms-spreadsheet-components "https://developer.mescius.com/spreadnet/winforms-spreadsheet-components"). Learn how implementing a user-friendly button to access a [Spreadsheet Designer App](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-spd-iface.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-spd-iface.html") can improve user experience, or consider the benefits of incorporating a .NET Spreadsheet [Ribbon Bar Control](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-ribboncontrol.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-ribboncontrol.html") 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](https://developer.mescius.com/blogs/how-to-import-and-export-excel-spreadsheets-with-c-sharp-and-wpf "https://developer.mescius.com/blogs/how-to-import-and-export-excel-spreadsheets-with-c-sharp-and-wpf").

### <a id="WinForms"></a>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](//cdn.mescius.io/umb/media/zoeefzsm/create-new-c-windows-forms-app.png?rmode=max&width=748&height=525)

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](//cdn.mescius.io/umb/media/jr0lfoso/install-spread-winforms.png?rmode=max&width=746&height=373)

### <a id="Add"></a>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](//cdn.mescius.io/umb/media/qp3fqlzx/client-side-c-workbook.png?rmode=max&width=742&height=371)

[Check out the **Adding a Component to a Project** documentation for additional information.](https://developer.mescius.com/spreadnet/docs/latest/online-win/adding-component-to-visual-studio-2019.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/adding-component-to-visual-studio-2019.html")

### <a id="Import"></a>Client-Side Import & Export Options in C# .NET Apps

#### <a id="Designer"></a>Invoke the Spreadsheet Designer During Runtime

[Spread’s Designer App](https://developer.mescius.com/spreadnet/docs/latest/online-win/SDnugetpackage.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/SDnugetpackage.html") 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](https://developer.mescius.com/spreadnet/docs/latest/online-win/FarPoint.Win.Spread.Design~FarPoint.Win.Spread.Design.FpSpreadDesigner~ShowDialog.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/FarPoint.Win.Spread.Design~FarPoint.Win.Spread.Design.FpSpreadDesigner~ShowDialog.html") 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](//cdn.mescius.io/umb/media/ge5kchr2/invoke-spreadsheet-designer.png?rmode=max&width=744&height=372)

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);
    }
```

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](//cdn.mescius.io/umb/media/pishgcnz/spnet-designer-app-io-excel.gif?rmode=max&width=744&height=372)

#### <a id="Ribbon"></a>Add a .NET Spreadsheet Ribbon Bar Control 

Spread WinForms includes a spreadsheet [**Ribbon Bar** control](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-ribboncontrol.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-ribboncontrol.html") 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](//cdn.mescius.io/umb/media/3amjcsb0/ribbon-bar-control.png?rmode=max&width=742&height=371)

In the **Form1.cs** file, add the following code to [generate the default RibbonBar items and behaviors](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-ribboncontrol.html#i-heading-generate-ribbonbar-items-manually "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-ribboncontrol.html#i-heading-generate-ribbonbar-items-manually"), and [attach the control to the existing spreadsheet component](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-ribboncontrol.html#i-heading-attach-ribbonbar-control "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-ribboncontrol.html#i-heading-attach-ribbonbar-control").

```
    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);
            }
        }
    }
```

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](//cdn.mescius.io/umb/media/eielwz0w/spnet-ribbonbar-control-io-excel.gif?rmode=max&width=752&height=376)

[To learn more, check out the **How to Add a .NET Spreadsheet Ribbon Bar Control using C#** blog.](https://developer.mescius.com/blogs/how-to-add-net-spreadsheet-ribbon-bar-control-using-c-sharp "https://developer.mescius.com/blogs/how-to-add-net-spreadsheet-ribbon-bar-control-using-c-sharp")

* * *

## <a id="Server"></a>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)**](https://developer.mescius.com/document-solutions/dot-net-excel-api "https://developer.mescius.com/document-solutions/dot-net-excel-api")**,** 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.

### <a id="Console"></a>Create a .NET 8 Console App

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

![Visual C# Console App](//cdn.mescius.io/umb/media/ucdjrgod/visual-c-console-app.png?rmode=max&width=747&height=525)

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](//cdn.mescius.io/umb/media/sk3nxx25/dsexcel-nuget-package-manager.png?rmode=max&width=744&height=372)

### <a id="Initialize"></a>Initialize a C# Excel Workbook Instance

In the **Program.cs** file, initialize a new C# Excel workbook using the [**Workbook** class](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Workbook.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Workbook.html").

```
    using GrapeCity.Documents.Excel;

    // Create a new workbook
    Workbook workbook = new Workbook();
```

### <a id="Open"></a>Invoke the Open Method to Read Existing Excel Files

DsExcel’s API offers an [**Open** method](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IWorkbook~Open(String,String,OpenOptions).html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IWorkbook~Open(String,String,OpenOptions).html") to read existing Excel files in the server-side workbook.

```
    // Open XLSX file
    workbook.Open("Profit loss statement.xlsx");
```

### <a id="Save"></a>Invoke the Save Method to Write Excel Files

The C# Excel library also offers a [**Save** method](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IWorkbook~Save(String,String,SaveOptions).html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.IWorkbook~Save(String,String,SaveOptions).html") 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");
```

![Profit Loss Statement](//cdn.mescius.io/umb/media/cveayj5a/profit-loss-statement.png?rmode=max&width=750&height=375)

For more information, see the DsExcel .NET [documentation on opening and saving workbooks](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/open_and_save_workbook.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/open_and_save_workbook.html"), as well as the online demos for [saving to Excel](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/saveworkbooktoexcelfile "https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/saveworkbooktoexcelfile") and [importing Excel files](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/importexcelfiletoworkbook "https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/importexcelfiletoworkbook").

<div style="background: #dfe2e6; padding: 15px;">

Check out our other blogs that discuss **Importing & Exporting Excel files** using different development frameworks: [WPF](https://developer.mescius.com/blogs/how-to-import-and-export-excel-spreadsheets-with-c-sharp-and-wpf "https://developer.mescius.com/blogs/how-to-import-and-export-excel-spreadsheets-with-c-sharp-and-wpf") | [JavaScript](https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-using-javascript "https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-using-javascript") | [React](https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-using-react "https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-using-react") | [Angular](https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-using-angular "https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-using-angular") | [Vue](https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-using-vue "https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-using-vue")

</div>

* * *

## **Learn More About These .NET Spreadsheet Solutions**

This article only scratches the surface of the full capabilities of [**Spread.NET**](https://developer.mescius.com/spreadnet "https://developer.mescius.com/spreadnet")and [**DsExcel .NET**](https://developer.mescius.com/document-solutions/dot-net-excel-api "https://developer.mescius.com/document-solutions/dot-net-excel-api"). To learn more about the .NET client-side spreadsheet component, see Spread.NET’s [demo explorers](https://developer.mescius.com/spreadnet/demos "https://developer.mescius.com/spreadnet/demos") and [online documentation](https://developer.mescius.com/spreadnet/docs "https://developer.mescius.com/spreadnet/docs"). To learn more about the C# Excel library, see DsExcel .NET’s [online demo explorer](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/ "https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/") and [documentation](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html").