Skip to main content Skip to footer

How to Add a WinForms C# Excel XLSX Viewer to Your Desktop .NET Application

Quick Start Guide
What You Will Need

.NET WinForms

NuGet Package: GrapeCity.Spread.WinForms

Controls Referenced

Spread.NET - C# .NET Spreadsheet Component

Documentation | Download Demo Explorer

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 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

  2. Include a .NET C# Spreadsheet UI Component in the Project

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

  4. Customize the Spreadsheet UI by Protecting the Excel Worksheet

Get hands-on with our sample app and experience the tutorial in action!

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

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, and install the WinForms spreadsheet component.

Excel Viewer

Download the Latest Release of Spread.NET Today to Install a 30-day Free Trial!

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

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. In this example, we will open an Excel file programmatically using Spread.NET’s API.

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

Excel Viewer

Within Form1, add the following to access the .NET spreadsheet workbook interface and invoke the OpenExcel method 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

Customize the Spreadsheet UI by Protecting the Excel Worksheet

Customize the sheet appearance, keyboard interactions, and the interaction in cells, rows, or columns 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. In this example, we will use the Spread.NET Worksheet’s Protect method to protect the worksheet content and the Workbook’s Protect method 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

Download the Latest Release of Spread.NET Today and Install a 30-day Free Trial!

Ready to bring your ideas to life? Download the sample app and get started!

.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. Review the documentation to see some of the many available features, and download the demo explorer 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.

Tags:

comments powered by Disqus