DEV Community

Cover image for How to Add a WinForms C# Excel XLSX Viewer to Your Desktop .NET Application
Chelsea Devereaux for MESCIUS inc.

Posted on • Updated on • Originally published at developer.mescius.com

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

Independent spreadsheet softwares like Microsoft Excel, LibreOffice, and Google Sheets have become essential tools for any business, small or large. Industries, including Financial, Healthcare, Manufacturing, Education, and more, require complex data management and analysis tasks. Spreadsheets are a diverse and easy-to-use tool for the job. Spreadsheets offer fast data analysis, visualization, form entry, reports…the list goes on and on.

While an independent spreadsheet software does provide many benefits, there are also several downsides to a company depending on them. Spreadsheets can become widely difficult to manage at scale, are prone to error (incorrect formulas or data entry mistakes), have limited security, and are difficult to automate processes.

.NET WinForms developers in almost every industry have been turning to spreadsheet API components and libraries to enhance their end-users' experience and workflows. A spreadsheet component provides an intuitive and familiar interface for viewing and editing Excel (.xlsx) files. The API libraries support importing Excel (.xlsx) files to view within the .NET app or generating/exporting Excel files.

Developers can also customize the spreadsheet API to meet the specific needs of their applications. They can add custom functionality, modify the user interface, and integrate the control with many different parts of their application. Overall, implementing a WinForms C# Excel (.xlsx) Viewer component can provide several benefits to a desktop application for both the developers and end-users.

In this piece, we will walk you through adding a WinForms C# Excel (.xlsx) Viewer to a .NET 7 WinForms app. First, by getting up and running with Spread.NET, a .NET spreadsheet component, then discussing how to programmatically import an existing Excel (.xlsx) file into the component and customize the worksheet to prevent changes for an end-user.

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

  1. Create a .NET 7 WinForms Project in Visual Studio
  2. Include the Spread.NET Spreadsheet UI Component
  3. Import an Excel (.xlsx) File to a WinForms Desktop App Using C#
  4. Customize the WinForms Spreadsheet Component by Protecting the Excel Worksheet

Create a .NET 7 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 7.0 as the framework.

Create a .NET 7 WinForms Project in Visual Studio

Include the Spread.NET Spreadsheet UI Component

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

Include the Spread.NET Spreadsheet UI Component

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.

WinForms C# Excel XLSX Viewer

Import an Excel (.xlsx) File to a WinForms App using C#

Importing an Excel (.xlsx) file can be done by the developer programmatically using the API library 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.

Import an Excel XLSX File to a WinForms App using C#

Within Form1, add the following to access the Spread.NET workbook interface and invoke the workbook's OpenExcel method to open and load the Excel file (make sure your Excel file is in this same directory).

C#:

    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");
            }
        }
Enter fullscreen mode Exit fullscreen mode

The Spread.NET component will now display the Excel file, and users will be able to interact with the data in the file.

Import an Excel (.xlsx) File to a WinForms App using C#

Customize the WinForms Spreadsheet Component by Protecting the Excel Worksheet

Spread.NET allows developers to customize the sheet appearance, keyboard interactions, and the interaction in cells, rows, or columns to meet the specific needs of their applications. To use Spread.NET as an Excel viewer, we will 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. Still, 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.

C#:

    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
            }
Enter fullscreen mode Exit fullscreen mode

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.

Customize the WinForms Spreadsheet Component by Protecting the Excel Worksheet

GrapeCity Spreadsheet Components

This article only scratches the surface of the full capabilities of GrapeCity’s 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 v16 release, check out our release blog.

Top comments (0)