Quick Start Guide | |
---|---|
Tutorial Concept |
Learn to set up a WPF Excel Viewer to programmatically import Excel files using C#, customize worksheets, and prevent changes by end-users in a .NET WPF application. |
What You Will Need |
Visual Studio - This example uses VS2022 .NET 8+ WPF Application NuGet Package: Mescius.Spread.WPF |
Controls Referenced |
If your WPF application needs to display or interact with Excel files, using a dedicated viewer is a clean and user-friendly solution. In this guide, we'll walk through how to add an Excel XLSX viewer to a WPF app using the WPF spreadsheet control, Spread.NET for WPF. You'll learn how to load a .xlsx file, lock down the worksheet, and allow users to edit only specific cells — perfect for budget forms, reports, or review-only sheets.
Start Using Spread.NET Today! Download a Free 30-day Trial
Steps to Implement a C# .NET Excel Viewer in Your WPF Application
- Create a .NET C#/VB WPF Project in Visual Studio
- Define the User Interface for the WPF Spreadsheet App
- Import Excel (.xlsx) Files into the WPF App Excel Viewer
- Protect the .NET Worksheet in the WPF Excel Viewer
Download a finished WPF sample app to follow along with the blog!
Create a .NET C#/VB WPF Project in Visual Studio
Open Visual Studio 2022 (pre or earlier) and create a New Project. Select WPF Application, name the project XLSX_Viewer_WPF, and select .NET 8.0 as the framework.
In the Solution Explorer, right-click the solution name and select Manage NuGet Packages for Solution. Search the NuGet.org package source for Mescius.Spread.Wpf and install the WPF spreadsheet component.
Define the User Interface for the WPF Spreadsheet App
In Visual Studio, open MainWindow.xaml in the editor. Open the VS Toolbox (CTRL+ALT+X) and notice a section named Spread for WPF. Drag and drop the GcSpreadSheet component onto the MainWindow.xaml design surface, preparing it to display Excel data in the WPF application.
Not seeing the MESCIUS Spread section in the Toolbox? Add it manually!
Users can edit the XAML element representing the spreadsheet component; for our needs, we will add the sheet name:
Import Excel (.xlsx) Files into the WPF App Excel Viewer
Now that the GcSpreadSheet control is added to the WPF UI, let's load an Excel file into it. In this example, we'll import a .xlsx file bundled with the application itself—no file picker or user input is required.
Start by adding your Excel file to the project. In this example, we add a file titled EventBudget.xlsx to the project folder. This ensures the file is copied to your build output so it can be loaded at runtime.
Next, open MainWindow.xaml.cs and add the following code to invoke Spread's OpenExcel method, loading the above Excel file when the WPF spreadsheet application starts:
Notice that the application now opens a selected Excel file on load, and users can interact with the spreadsheet content.
Protect the .NET Worksheet in the WPF Excel Viewer
By default, all cells in the WPF worksheet are locked, but the cell locking that prevents editing only takes effect after protection is enabled. So first, ensure the cells you want to remain locked are untouched.
Before applying protection, unlock the cells or ranges where users should be able to input values. Below is an example that unlocks a few cells in column B (i.e., the "Budget" column) using the Locked Property:
Once the necessary cells are unlocked, the next step is to protect the worksheet using the Protect method. This doesn't require a password and can be called directly. Applying protection activates the locking rules: unlocked cells remain editable while all others become read-only within the WPF viewer.
Run the WPF application and notice that users can only edit the cells contained in the unlocked B4:B10 range while the rest of the spreadsheet remains read-only, preventing any unauthorized editing.
Check out our Spread for WPF documentation to learn more about Worksheet Protection and Unlocking Cells.
C#/VB WPF Spreadsheet Components
This article only scratches the surface of the full capabilities of the Spread.NET WPF spreadsheet component. Review the documentation to see some of the many available features. Integrating a spreadsheet component into your applications allows you to customize your users' experience and provide them with familiar spreadsheet functionality that is all contained within your own controlled application.