How to Import and Export Excel XLSX in WinForms C# and VB.NET
Tags: dotnet, csharp, webdev, tutorial
Published: 2022-07-29 19:46:00

---
canonical_url: https://www.grapecity.com/blogs/how-to-import-export-excel-xlsx-in-c-sharp-vb-net
description: Learn how to import and export Excel XLSX in WinForms C# and VB.NET. See more from Spread.NET today.
---

This post describes importing and exporting your Microsoft Excel XLSX and XLSM spreadsheets directly in your .NET WinForms applications using **Spread.NET**.

We will use the **Spread Designer** tool in run-time for editing Excel spreadsheet instances inside your running application with just one line of code. Finally, we will show how to create a simple front-end spreadsheet user interface that integrates the **FpSpread** spreadsheet control with the **NameBox** and **FormulaTextBox** controls using splitter panes to create the main user interface and how to implement menu items to handle **File - Open, File - Save**, and **File - Design** commands in **C#** and **VB**.

Here are the steps for importing and exporting Excel XLSX spreadsheets in C# and VB.net WinForms:

*   [Step 1: Create the Project](#step1)
*   [Step 2: Configure the Project](#step2)
*   [Step 3: Create the File Menu](#step3)
*   [Step 4: Create SplitContainer1](#step4)
*   [Step 5: Configure SplitContainer1](#step5)
*   [Step 6: Create SplitContainer2](#step6)
*   [Step 7: Create the FpSpread](#step7)
*   [Step 8: Create the NameBox](#step8)
*   [Step 9: Create the FormulaTextBox](#step9)
*   [Step 10: Create the WinForms Spreadsheet Designer Component](#step10)
*   [Step 11: Create Event Handlers for File Menu Items](#step11)
*   [Step 12: Add Event Handler Code](#step12)

These steps require:

*   **Microsoft Visual Studio 2022 (note: while these steps are for VS2022 and .NET 6,** [**Spread.NET**](http://spread.net/)**WinForms can also work in earlier versions of Visual Studio targeting .NET 4.6.2+, following these same steps)**
*   [**Spread.NET**](http://spread.net/) **for WinForms** (trial version or licensed)

## **<a id="step1"></a>Step 1: Create the Project**

![create a new project](https://files.grapecity.com/gc-website-media/tbpk5j2t/01-new.png?width=500&height=283.8479809976247)

Create a new project in **Visual Studio 2022** by selecting **C#, Windows**, and **Desktop** to filter the projects and then select either **C#** or **VB** **WinForms App**.

## **<a id="step2"></a>Step 2: Configure the Project**

![Configure a New Project](https://files.grapecity.com/gc-website-media/cmgn5khw/02-new.png?width=500&height=281.67330677290835)

Type **SpreadNetQuickStart** for the **Project Name**.

## **<a id="step3"></a>Step 3: Create the File Menu**

Now let’s add a **MenuStrip** in the **Toolbox** window (**F4**) under the category for **Menus & Toolbars** and double-click the **MenuStrip**component to create a new **MenuStrip** in the form:

![Menu Strip](https://files.grapecity.com/gc-website-media/dnxl134s/03-menustrip.png?width=269&height=146)

Create the menu items for **File – Open**, **File – Save**, **File – Save As**, **File – Design**, and **File – Exit** using the associated shortcut keys:

![Design](https://files.grapecity.com/gc-website-media/wzwpy0hg/04-design.png?width=196&height=200)

Note that the **File - Save** menu item should initially be disabled. The menu separators (use **"-"** for the menu item text to create a menu separator) and shortcut keys are optional but recommended. We will add code for those menu items in a later step.

## **<a id="step4"></a>Step 4: Create SplitContainer1**

**Note: **Adding splitters isn't strictly necessary to use FpSpread, but the SplitContainer control makes creating a friendly and flexible spreadsheet interface much easier because it automatically handles resizing for the spreadsheet, namebox, and formula text box controls without requiring any code.

In the **Toolbox** (**CTRL+ALT+X**), expand the category for **Containers**, then double-click the **SplitContainer** control to create **_splitContainer1_**:

![Split Container](https://files.grapecity.com/gc-website-media/3cgj5p0x/05-splitcontainer.png?width=249&height=150)

## **<a id="step5"></a>Step 5: Configure SplitContainer1**

In the **Properties** window (**F4**), for **_splitContainer1_**, set **Orientation** to **_Horizontal_**, then set **Palen1MinSize** and **SplitterDistance** to **_23_**:

![Orientation](https://files.grapecity.com/gc-website-media/rfwiyb4b/06-orientation.png?width=386&height=407)

The spreadsheet will be in the bottom pane, the formula bar interface will be in the top pane, and the splitter will determine the height of the **FormulaTextBox** control for showing long formulas that wrap to new lines.

## **<a id="step6"></a>Step 6: Create SplitContainer2**

In the **Toolbox** (**CTRL+ALT+X**), drag-and-drop a new **SplitContainer** inside the top pane (**Panel1**) of **_splitContainer1_** to create **_splitContainer2_**:

![Split Container](https://files.grapecity.com/gc-website-media/mh2kfqvb/07-splitcontainer2.png?width=500&height=292.6587301587302)

In the **Properties** window (**F4**), for **_splitContainer2_** set **Panel1MinSize** and **SplitterDistance** to **_150_**:

![SplitterDistance](https://files.grapecity.com/gc-website-media/grsp5her/08-splitterdistance.png?width=327&height=407)

## **<a id="step7"></a>Step 7: Create the FpSpread**

In **Solution Explorer**, expand the **Solution _SpreadNetQuickStart_** and **Project _SpreadNetQuickStart_**, then right-click **Dependencies **and select **Manage NuGet Packages…** (or press **ALT+P+N+N+N+ENTER**):

![Manage NuGet Package](https://files.grapecity.com/gc-website-media/rwbfok0g/09-managenugetpackages.png?width=265&height=248)

Then, in **NuGet Package Manager**, select the **Browse** in the upper-left, then type **_Spread.WinForms_** in the search box to find the latest **GrapeCity.Spread.WinForms**, then click **Install**:

![SpreadNet QuickStart](https://files.grapecity.com/gc-website-media/xuylpsij/10-new.png?width=500&height=340.0537634408602)

After installing **GrapeCity.Spread.WinForms**, go ahead and also install **GrapeCity.Spread.WinForms.Design** – this package contains the **fpSpreadDesigner** component for showing the **Spread Designer** tool in runtime.

Then, in the **Toolbox** (**CTRL+ALT+X**), select the **FpSpread** control:

![FpSpread](https://files.grapecity.com/gc-website-media/051mkgz5/11-fpspread.png?width=269&height=227)

Finally, draw an instance of **FpSpread** into the bottom pane (**Panel2**) of **SplitContainer1**. The **Spread Designer** tool may open when you create the control (that is the default behavior) – for now, close the **Spread Designer** if it appears. 

![Panel](https://files.grapecity.com/gc-website-media/xvxlchqb/12-panel1.png?width=429&height=295)

Using the **Property Grid (F4)**, set the **Dock** property to **Fill:**

**![Duckbill](https://files.grapecity.com/gc-website-media/31sfvz3k/13-dockfill.png?width=386&height=419)**

## **<a id="step8"></a>Step 8: Create the NameBox**

In the **Toolbox** (**CTRL+ALT+X**), expand the category for **GrapeCity Spread for WinForms** and select the **NameBox** control:

![Namebox](https://files.grapecity.com/gc-website-media/42idhzxc/14-namebox.png?width=308&height=227)

Draw a new **NameBox** inside the upper-left **_Panel1_** (**_SplitContiner2.Panel1_**):

![Panel](https://files.grapecity.com/gc-website-media/ckneov3m/15-panel1.png?width=176&height=72)

In the **Properties** window (**F4**), for **_nameBox1,_** set **Dock** to **_Fill_**:

![Dock fill](https://files.grapecity.com/gc-website-media/eyaftjn1/16-dockfill.png?width=386&height=419)

In the upper-right corner of **_nameBox1_**, click the indicator to open the **NameBox Tasks**, then click the **AttachTo** drop-down and select **_fpSpread1_** – this will generate code in the form code-behind to attach the **NameBox** control to the **FpSpread** control:

![FpSpread](https://files.grapecity.com/gc-website-media/zwxpk5na/17-fpspread1.png?width=374&height=115)

## **<a id="step9"></a>Step 9: Create the FormulaTextBox**

In the **Toolbox** (**CTRL+ALT+X**), expand **Containers** and select the **Panel** control:

![Panel control](https://files.grapecity.com/gc-website-media/fe0logm2/18-panelcontrol.png?width=308&height=167)

Inside **_Panel2_** (to the right of **_NameBox1_**), draw a new **Panel**:

![Draw New Panel](https://files.grapecity.com/gc-website-media/p4ybdkgx/19-drawnewpanel.png?width=500&height=132.8740157480315)

In the **Properties** window (**F4**), for **_panel1,_** set **BorderStyle** to **_FixedSingle_** and **Dock** to **_Fill_**:

![Fixed single](https://files.grapecity.com/gc-website-media/2cqmdhlk/20-fixedsingle.png?width=386&height=419)

In the **Toolbox (CTRL+ALT+X),** select the **FormulaTextBox** control:

![Formula Text Box](https://files.grapecity.com/gc-website-media/5q5bx3lw/21-formulatextbox.png?width=308&height=226)

Inside **_panel1_** (which is inside **_splitContainer2.Panel2_**), draw a new **FormulaTextBox** control:

![Panel 1](https://files.grapecity.com/gc-website-media/w1kneyn5/22-panel1.png?width=333&height=134)

In the **Properties** window (**F4**) for **_formulaTextBox1,_** set **BorderStyle** to **_None_** and **Dock** to **_Fill_**:

![Border Style](https://files.grapecity.com/gc-website-media/w21jk44c/23-borderstyle.png?width=386&height=419)

In the upper-right corner of **_formulaTextBox1_**, click the indicator to open the **FormulaTextBox Tasks,** then click the **AttachTo** drop-down and select **_fpSpread1_** – this will generate code in the form code-behind to attach the **FormulaTextBox** control to the **FpSpread**control:

![FpSpread1](https://files.grapecity.com/gc-website-media/xwkgch5c/24-fpspread1.png?width=316&height=114)

## **<a id="step10"></a>Step 10: Create the WinForms Spreadsheet Designer Component**

Now, in the **Toolbox** window (**CTRL+ALT+X**) under the category **GrapeCity Spread Design for WinForms,** double-click the **FpSpreadDesigner** component to create a new **FpSpreadDesigner** in the form:

![FpSpreadDesigner](https://files.grapecity.com/gc-website-media/jktleppe/25-fpspreaddesigner.png?width=268&height=99)

The **_fpSpreadDesigner1_** component just added should show in the form component tray next to the components **_menuStrip1 _**and**_fpSpread1_Sheet1_**:

![FpSpreadDesigner](https://files.grapecity.com/gc-website-media/c3cfgat3/26-fpspreaddesigner1.png?width=500&height=54.48717948717949)

## **<a id="step11"></a>Step 11: Create Event Handlers for File Menu Items**

![Create file menu handlers](https://files.grapecity.com/gc-website-media/5d1pekqg/27-createfilemenuhandlers.png?width=500&height=281.25)

For each menu item in the **File** menu, double-click that menu item in the design view until each menu item has an associated menu handler generated in the VB or C# code:

![Create file menu handlers](https://files.grapecity.com/gc-website-media/krrd41of/28-createfilemenuhandlers2.png?width=500&height=281.25)

## **<a id="step12"></a>Step 12: Add Event Handler Code**

Copy the following code to implement the event handlers:

**[C#]**

**Add Event Handler Code C#**

```
    private string mFileName = null;

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Excel Spreadsheet (*.XLSX;*.XLSM)|*.XLSX;*.XLSM";
        ofd.FilterIndex = 0;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            mFileName = ofd.FileName;
            fpSpread1.OpenExcel(mFileName);
            saveToolStripMenuItem.Enabled = true;
        }
    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        fpSpread1.SaveExcel(mFileName);
    }

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Excel Spreadsheet (*.XLSX;*.XLSM)|*.XLSX;*.XLSM";
        sfd.FilterIndex = 0;
        sfd.FileName = mFileName;
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            mFileName = sfd.FileName;
            fpSpread1.SaveExcel(mFileName);
            saveToolStripMenuItem.Enabled = true;
        }
    }

    private void designToolStripMenuItem_Click(object sender, EventArgs e)
    {
        fpSpreadDesigner1.ShowDialog(fpSpread1);
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DialogResult ret = MessageBox.Show("Do you want to save this file before closing?", "Save Spreadsheet", MessageBoxButtons.YesNoCancel);
        if (ret == DialogResult.Cancel)
            return;
        else if (ret == DialogResult.Yes)
            saveToolStripMenuItem_Click(null, EventArgs.Empty);
        Close();
    }
```
**[VB]**

**Add Event Handler Code VB**

```
    Private mFileName As String = Nothing

    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "Excel Spreadsheet (*.XLSX;*.XLSM)|*.XLSX;*.XLSM"
        ofd.FilterIndex = 0
        If ofd.ShowDialog() = DialogResult.OK Then
            mFileName = ofd.FileName
            FpSpread1.OpenExcel(mFileName)
            SaveToolStripMenuItem.Enabled = True
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
        FpSpread1.SaveExcel(mFileName)
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim sfd As SaveFileDialog = New SaveFileDialog()
        sfd.Filter = "Excel Spreadsheet (*.XLSX;*.XLSM)|*.XLSX;*.XLSM"
        sfd.FilterIndex = 0
        sfd.FileName = mFileName
        If sfd.ShowDialog() = DialogResult.OK Then
            mFileName = sfd.FileName
            FpSpread1.SaveExcel(mFileName)
            SaveToolStripMenuItem.Enabled = True
        End If
    End Sub

    Private Sub DesignToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DesignToolStripMenuItem.Click
        FpSpreadDesigner1.ShowDialog(FpSpread1)
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Dim ret As DialogResult = MessageBox.Show("Do you want to save this file before closing?", "Closing", MessageBoxButtons.YesNoCancel)
        If ret = DialogResult.Cancel Then
            Return
        ElseIf ret = DialogResult.Yes Then
            SaveToolStripMenuItem_Click(Nothing, EventArgs.Empty)
        End If
        Close()
    End Sub
```

The code for **File - Open** uses the **OpenFileDialog** to browse for a spreadsheet file, then uses the **FpSpread.OpenExcel** method to open the selected file. The code for **File - Save** uses **FpSpread.SaveExcel** to save the spreadsheet and the code for **File - Save As** uses the **SaveFileDialog** to allow the user to save the file to another location or use another name. This functionality allows for C# or VB.net Excel import and export.

The code in File-Design uses the **FpSpreadDesigner.ShowDialog** method to show the **Spread Designer** tool in run-time, make changes, and then apply those changes back to the spreadsheet instance in the form. Finally, the code in **File - Exit** prompts the user whether to save the file and then uses the **Close** method to close the form.

## **Ready to Build and Run!**

![Final Result](https://files.grapecity.com/gc-website-media/csddbim0/29-finalresult.png?width=500&height=281.25)

The project is ready to build and run. The File-Design menu will open the **Spread Designer** tool in run-time, as shown above, which can apply changes to the spreadsheet instance running in the form. See how Spread .NET can give you the power to import and export your Excel XLSX spreadsheets with C# or VB.net within your application.

In another article series, we demonstrate [How to Import and Export Excel Spreadsheets with C# and WPF](https://www.grapecity.com/blogs/how-to-import-and-export-excel-spreadsheets-with-c-sharp-and-wpf), [How to Import and Export Excel Spreadsheets using JavaScript](https://www.grapecity.com/blogs/how-to-importexport-excel-files-using-javascript-and-spread-sheets), and [How to Import and Export Excel Spreadsheets in Angular](https://www.grapecity.com/blogs/how-to-import-and-export-excel-spreadsheets-in-angular).

[**Download the C# Sample**](https://files.grapecity.com/gc-website-media/z3jo3eit/spreadnetquickstartcs.zip)** | **[**Download the VB Sample**](https://files.grapecity.com/gc-website-media/tmneup3v/spreadnetquickstartvb.zip)