How to Add a WinForms C# Excel XLSX Viewer to Your Desktop .NET Application
Tags: webdev, devops, dotnet, tutorial
Published: 2023-05-18 15:42:13

---
canonical_url: https://developer.mescius.com/blogs/how-to-add-a-winforms-c-sharp-excel-xlsx-viewer-to-your-desktop-application
description: Learn how to add a WinForms C# Excel XLSX viewer to your desktop application. See more from Spread.NET today.
---

**What You Will Need**
- .NET WinForms
- NuGet Package: **[GrapeCity.Spread.WinForms](https://www.nuget.org/packages/GrapeCity.Spread.WinForms)**

**Controls Referenced**
- [Spread.NET - C# .NET Spreadsheet Component](https://developer.mescius.com/spreadnet)
- [Documentation](https://developer.mescius.com/spreadnet/docs/latest/online-win/overview.html)
- [Download Demo Explorer](https://developer.mescius.com/spreadnet/demos)

**Tutorial Concept**
Learn to set up a C# .NET Excel Viewer to programmatically import Excel files, customize worksheets, and prevent changes by end-users.

---

Spreadsheets like Excel, LibreOffice, and Google Sheets are vital for businesses, offering quick data analysis and reporting. However, they can be challenging to manage and lack security.

To enhance users experience and workflows, enterprise developers are adopting spreadsheet UI components and API libraries. These tools streamline processes and allow for seamless integration with existing applications.

Implementing a WinForms C# Excel Viewer benefits both developers and users by providing a familiar interface for Excel files. In this tutorial, we will be using [Spread.NET](https://developer.mescius.com/spreadnet "https://developer.mescius.com/spreadnet") to show you how to easily add a C# spreadsheet component to a .NET 8 WinForms app, covering importing Excel files, UI customization, and user restrictions.

## Steps to Add a WinForms C# Excel Viewer to a Desktop App

1.  [Create a .NET 8 C# WinForms Project in Visual Studio](#create)

2.  [Include a .NET C# Spreadsheet UI Component in the Project](#include)

3.  [Import an Excel (.xlsx) File to a WinForms Desktop App Using C# Spreadsheet API](#import)

4.  [Customize the Spreadsheet UI by Protecting the Excel Worksheet](#customize)

[**Get hands-on with our sample app and experience the tutorial in action!**](https://cdn.mescius.io/assets/developer/blogs/spread/20240412-NET-WinForm-Excel-Viewer-SPNET/WinForms-Excel-Viewer.zip "https://cdn.mescius.io/assets/developer/blogs/spread/20240412-NET-WinForm-Excel-Viewer-SPNET/WinForms-Excel-Viewer.zip")

### <a id="create"></a>Create a .NET 8 C# WinForms Project in Visual Studio

First, open Visual Studio 2022 (Pre) and create a **New** **Project**. Select **Windows Forms App (C#)**, name the project, and select **.NET 8.0** as the framework.

![Excel Viewer](//cdn.mescius.io/umb/media/tmhjurxn/excel-viewer-1.gif?rmode=max&width=736&height=368)

### <a id="include"></a>Include a .NET C# Spreadsheet UI Component in the Project

Right-click the solution name in the _Solution Explorer_ and select **Manage NuGet Packages for Solution**. Search the NuGet.org _package source_ for [**GrapeCity.Spread.WinForms**](https://www.nuget.org/packages/GrapeCity.Spread.WinForms "https://www.nuget.org/packages/GrapeCity.Spread.WinForms"), and install the WinForms spreadsheet component.

![Excel Viewer](//cdn.mescius.io/umb/media/3lplhmw0/5-20240411-185855.png?rmode=max&width=748&height=374)

With Spread.NET for WinForms included in the project, select the **FpSpread** control from the **Toolbox** and drag to create the spreadsheet instance on the **Form**.

![Excel Viewer](//cdn.mescius.io/umb/media/4qrlrgf3/excel-viewer-3.gif?rmode=max&width=748&height=374)

### <a id="import"></a>Import an Excel (.xlsx) File to a WinForms Desktop App Using C# Spreadsheet API

Importing an Excel (.xlsx) file can be done by the developer programmatically using the .NET C# API or on the [client-side](https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-in-c-sharp-vb-net "https://developer.mescius.com/blogs/how-to-import-export-excel-xlsx-in-c-sharp-vb-net"). In this example, we will [open an Excel file programmatically using Spread.NET’s API](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-open-excelfile.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-open-excelfile.html").

To add the API code to do this, right-click on the spreadsheet component in the Designer and select **View Code**.

![Excel Viewer](//cdn.mescius.io/umb/media/znxj11pb/58-20240411-192133.png?rmode=max&width=754&height=377)

Within **Form1**, add the following to access the .NET spreadsheet workbook interface and invoke the [**OpenExcel** method](https://developer.mescius.com/spreadnet/docs/latest/online-win/FarPoint.Win.Spread~FarPoint.Win.Spread.FpSpread~OpenExcel.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/FarPoint.Win.Spread~FarPoint.Win.Spread.FpSpread~OpenExcel.html") to open and load the Excel file. In this example, the Excel file is in this same directory.

```
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                // Access the workbooks interface
                IWorkbook workbook = fpSpread1.AsWorkbook();
                workbook.WorkbookSet.CalculationEngine.CalcFeatures = CalcFeatures.All;
                // Open Excel file - Change the Excel files path
                fpSpread1.OpenExcel("Excel-Test.xlsx");
            }
        }
```

The WinForms application will now display the Excel file and users are able to interact with the data using the spreadsheet interface.

![Excel Viewer](//cdn.mescius.io/umb/media/s4xladf0/excel-viewer-4.gif?rmode=max&width=752&height=376)

### <a id="customize"></a>Customize the Spreadsheet UI by Protecting the Excel Worksheet

Customize the [sheet appearance](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-setappearance.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-setappearance.html"), [keyboard interactions](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-mappingkeys.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-mappingkeys.html"), and the [interaction in cells](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-interactcell.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-interactcell.html"), [rows, or columns](https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-interact-rowcol.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/spwin-interact-rowcol.html") to meet the specific needs of your .NET app. To use Spread.NET as a viewer only, protect the workbook to ensure the user cannot make changes to the Excel file's data. There are [several ways to protect Excel workbooks and their data using a spreadsheet API](https://developer.mescius.com/blogs/5-ways-to-protect-excel-xlsx-data-in-net-applications "https://developer.mescius.com/blogs/5-ways-to-protect-excel-xlsx-data-in-net-applications"). In this example, we will use the [Spread.NET Worksheet’s **Protect** method](https://developer.mescius.com/spreadnet/docs/latest/online-win/GrapeCity.Spreadsheet~GrapeCity.Spreadsheet.IWorksheet~Protect.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/GrapeCity.Spreadsheet~GrapeCity.Spreadsheet.IWorksheet~Protect.html") to protect the worksheet content and the [Workbook’s **Protect** method](https://developer.mescius.com/spreadnet/docs/latest/online-win/GrapeCity.Spreadsheet~GrapeCity.Spreadsheet.IWorkbook~Protect.html "https://developer.mescius.com/spreadnet/docs/latest/online-win/GrapeCity.Spreadsheet~GrapeCity.Spreadsheet.IWorkbook~Protect.html") to protect the workbook component.

```
    public Form1()
            {
                InitializeComponent();
                // Access the workbooks interface
                IWorkbook workbook = fpSpread1.AsWorkbook();

                workbook.WorkbookSet.CalculationEngine.CalcFeatures = CalcFeatures.All;

                // Open Excel file - Change the Excel files path
                fpSpread1.OpenExcel("C:/Users/mackenzie.albitz/source/repos/Spread.NET-Test/Spread.NET-Test/Excel-Test.xlsx");

                // Protect Workbook from user changes
                fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.All); // Protect the worksheet
                fpSpread1.AsWorkbook().Protect(WorkbookLocks.All); // Protects the workbook component
            }
```

With the protect methods invoked, users can no longer select and change the content in the worksheet or make changes to the workbook component, such as adding new worksheets or adjusting the row/column height.

![Excel Viewer](//cdn.mescius.io/umb/media/rsrozfxm/excel-viewer-5.gif?rmode=max&width=760&height=380)

Ready to bring your ideas to life? [Download the sample app and get started!](https://cdn.mescius.io/assets/developer/blogs/spread/20240412-NET-WinForm-Excel-Viewer-SPNET/WinForms-Excel-Viewer.zip "https://cdn.mescius.io/assets/developer/blogs/spread/20240412-NET-WinForm-Excel-Viewer-SPNET/WinForms-Excel-Viewer.zip")[](https://cdn.mescius.io/assets/developer/blogs/spread/20240412-NET-WinForm-Excel-Viewer-SPNET/WinForms-Excel-Viewer.zip "https://cdn.mescius.io/assets/developer/blogs/spread/20240412-NET-WinForm-Excel-Viewer-SPNET/WinForms-Excel-Viewer.zip")

### .NET C# Spreadsheet Components

This article only scratches the surface of the full capabilities of [**MESCIUS Spread.NET**, the world’s #1 selling .NET spreadsheet component](https://developer.mescius.com/spreadnet "https://developer.mescius.com/spreadnet"). Review the [documentation](https://developer.mescius.com/spreadnet/docs/ "https://developer.mescius.com/spreadnet/docs/") to see some of the many available features, and [download the demo explorer](https://developer.mescius.com/spreadnet/demos "https://developer.mescius.com/spreadnet/demos") to see the features in action and interact with the sample code. Integrating a spreadsheet component into your applications allows you to customize your users' experience and provide them with familiar spreadsheet functionality without referring them to an external program. To learn more about Spread.NET and the new features added with the latest release, check out our [Releases page here](https://developer.mescius.com/spreadnet/releases "https://developer.mescius.com/spreadnet/releases").