Quick Start Guide | |
---|---|
Tutorial Concept | Learn to set up a C# .NET Excel Viewer to programmatically import Excel files, customize worksheets, and prevent changes by end-users in a WinForms application. |
What You Will Need |
Visual Studio - This example using VS2022 .NET 6+ WinForms Application NuGet Package: GrapeCity.Spread.WinForms |
Controls Referenced |
Spreadsheets like Excel are essential for data analysis and reporting, but managing them in desktop apps can be complex. Many developers now use C# .NET Excel Viewer components to simplify integration and enhance user experience.
In this tutorial, we'll explore how to add a C# .NET Excel Viewer to a .NET 8 WinForms app using the WinForms spreadsheet component, Spread.NET. We’ll cover Excel file imports, UI customization, and user restrictions.
Start Using Spread.NET Today! Download a Free Trial
Steps to Implement a C# .NET Excel Viewer in Your WinForms Application
- Create a .NET C# WinForms Project in Visual Studio
- Add a C# .NET Excel Viewer Component to the Project
- Import Excel (.xlsx) Files into the WinForms App Excel Viewer
- Protect the Excel Worksheet in Your C# .NET Excel Viewer
Download a finished WinForms sample app to follow along with the blog!
Create a .NET C# WinForms Project in Visual Studio
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.
Add a C# .NET Excel Viewer Component to the Project
In the Solution Explorer, right-click the solution name and select Manage NuGet Packages for Solution. Search the NuGet.org package source for GrapeCity.Spread.WinForms and install the WinForms spreadsheet component.
Select the FpSpread control from the Toolbox and drag it to create the spreadsheet instance on the Form.
Import Excel (.xlsx) Files into the WinForms App Excel Viewer
To programmatically import an Excel (.xlsx) file using Spread.NET’s C# API, right-click on the spreadsheet component in the Designer and select View Code.
Add the following code in Form1 to access the workbook interface and invoke the OpenExcel method to open the specified Excel file:
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");
}
}
Learn how to add Excel XLSX exporting capabilities to WinForms apps in our other blog: How to Import and Export Excel XLSX in .NET Applications Using C#.
Protect the Worksheet in Your C# .NET Excel Viewer
To customize the spreadsheet UI and prevent users from altering the Excel file, you can protect the workbook and worksheet using Spread.NET’s Protect methods. This ensures your app will function as a viewer-only interface.
Add the following code within Form1 to invoke the Protect method to lock both the worksheet and the workbook:
public Form1()
{
InitializeComponent();
// Access the workbook interface
IWorkbook workbook = fpSpread1.AsWorkbook();
workbook.WorkbookSet.CalculationEngine.CalcFeatures = CalcFeatures.All;
// Open Excel file
fpSpread1.OpenExcel("C:/path/to/Excel-Test.xlsx");
// Protect the worksheet and workbook
workbook.ActiveSheet.Protect(WorksheetLocks.All);
workbook.Protect(WorkbookLocks.All);
}
With this setup, users won't be able to modify worksheet content or make changes to the workbook, such as adding new sheets or altering row and column sizes.
Ready to Get Started? Download a 30-day Free Trial of Spread.NET Today!
Learn More About C# .NET Spreadsheet Components
This article only scratches the surface of the full capabilities of Spread.NET, our .NET C# spreadsheet component. Review the documentation to see some of the many available features, and download our 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 provides them with familiar spreadsheet functionality without referring them to an external program. To learn more about Spread.NET and the new features added in the latest release, check out our release pages.