Quick Start Guide | |
---|---|
What You Will Need |
Visual Studio 2022 .NET 4.8+ |
Controls Referenced | |
Tutorial Concept | Learn how to create C# or VB WPF spreadsheet application with Excel XLSX importing and exporting capabilities offered to the users from the app's UI. |
Excel XLSX spreadsheets are ubiquitous in the business world, making it crucial for developers to know how to integrate them into applications. Whether you're building a data analysis tool or a complex enterprise solution, handling Excel files can significantly enhance your app's functionality. In this blog, we will guide you through four essential steps to seamlessly import and export Excel spreadsheets within a WPF application using C# or VB. Follow along to learn how to create a robust spreadsheet application with Spread.NET and elevate your WPF projects.
The steps to importing and exporting Excel spreadsheets with WPF in C# or VB are as follows:
- Create a WPF Spreadsheet Application
- Define the User Interface for a WPF Spreadsheet App
- Add Excel XLSX Import Code – Invoke ExcelOpen Method
- Add Excel XLSX Export Code – Invoke SaveExcel Method
Download the finished sample app to follow along: C# Sample or VB Sample
Ready to try it for yourself? Download a free 30-day trial of Spread.NET today!
Create a C#/VB WPF Spreadsheet Application
First, you’ll need to download and install the trial version of Spread.NET. Be sure to include the WPF controls when installing the solution.
Open Visual Studio 2022 and create a new WPF (.Net Framework) desktop application project. We named our project SpreadWPF_IO.WPF:
Once the solution has completed loading in Visual Studio, open MainWindow.xaml in the editor, then open the Visual Studio Toolbox (CTRL+ALT+X). You should see a Toolbox section named MESCIUS Spread:
If you don't see this section, you will need to add the spreadsheet component to the toolbox manually – luckily, you’ll only ever have to do this once. Add the component by right-clicking in the Toolbox panel, and from the menu, select Choose Items. Scroll through the list to find and place a checkmark next to the GcSpreadSheet component located in the GrapeCity.WPF.SpreadSheet.UI namespace. Then, press the OK button:
Define the User Interface for a WPF Spreadsheet App
We'll create a window with two rows for our WPF application. The first row will contain the Spread.NET spreadsheet component, and the second row will have two buttons: one to import the symbols spreadsheet and another to export the processed results.
In MainWindow.xaml, replace the existing Grid element with this markup:
MainPage.xaml Grid definition
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button x:Name="btnLoadSpreadsheet" Grid.Row="1" Grid.Column="0"
Content="Load Spreadsheet" />
<Button x:Name="btnExportSpreadsheet" Grid.Row="1" Grid.Column="1"
Content="Export Spreadsheet" />
</Grid>
The designer window will now look similar to the following:
Next, drag the GcSpreadSheet component from the Toolbox (CTRL+ALT+X) and drop it into the first cell of the grid:
The first time you try to create the GcSpreadsheet control, the reference for the GcSpreadsheet will be added to the project, and the designer will need to restart. The dialog below will appear:
This is normal behavior in the latest VS2022 XAML editor and isn't cause for concern. Close this dialog, and drag and drop the GcSpreadsheet one more time to add it to the page.
Edit the XAML element representing the spreadsheet component so that it matches the following:
XAML for GcSpreadsheet element
<ss:GcSpreadSheet x:Name="spreadControl" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
The designer will now look similar to the following:
Add Excel XLSX Import Code
To add the ability to import Excel files into the WPF C#/VB app, use the OpenFileDialog class from the Microsoft.Win32 namespace. This dialog includes filters to simplify the selection of Excel (*.XLSX) documents. Once a file is chosen, the spreadsheet is loaded into the component with a single line of code by invoking the OpenExcel method.
In the MainWindow.xaml designer, double-click the Load Spreadsheet button to create the click handler, then implement the following code:
BtnLoadSpreadsheet_Click C#
private void btnLoadSpreadsheet_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.DefaultExt = ".xlsx";
ofd.Filter = "Excel Documents (*.xlsx)|*.xlsx";
var sel = ofd.ShowDialog();
if (sel == true)
{
// one line of code to import the Excel file into Spread.NET
spreadControl.OpenExcel(ofd.FileName);
}
}
BtnLoadSpreadsheet_Click VB
Private Sub btnLoadSpreadsheet_Click(sender As Object, e As RoutedEventArgs) Handles btnLoadSpreadsheet.Click
Dim ofd As Microsoft.Win32.OpenFileDialog = New Microsoft.Win32.OpenFileDialog()
ofd.DefaultExt = ".xlsx"
ofd.Filter = "Excel Documents (*.xlsx)|*.xlsx"
Dim sel = ofd.ShowDialog()
If (sel = True) Then
' one line of code to import the Excel file into Spread.NET
spreadControl.OpenExcel(ofd.FileName)
End If
End Sub
Run the application and select an Excel file. The XLSX file is imported and displayed in the spreadsheet control of the application.
Add Excel XLSX Export Code
Now that the import process is functioning, we can move on to implementing the export functionality. We will be using the SaveFileDialog class located in the Microsoft.Win32 namespace because it can set extension filters in the dialog. The ability to export an Excel document from the Spread.NET component is also a simple process, accomplished with (you got it) a single line of code! We must invoke the SaveExcelmethod.
In the MainWindow.xaml designer, double-click the Export Spreadsheet button to implement the click event handler code. Implement the handler code as follows:
btnExportSpreadsheet_Click C#
private void btnExportSpreadsheet_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.FileName = "SpreadNET.xlsx";
sfd.Filter = "Excel Documents (*.xlsx)|*.xlsx";
sfd.DefaultExt = ".xlsx";
var sel = sfd.ShowDialog();
if (sel == true)
{
spreadControl.SaveExcel(sfd.FileName, GrapeCity.Windows.SpreadSheet.Data.ExcelFileFormat.XLSX);
}
}
btnExportSpreadsheet_Click VB
Private Sub btnExportSpreadsheet_Click(sender As Object, e As RoutedEventArgs) Handles btnExportSpreadsheet.Click
Dim sfd As Microsoft.Win32.SaveFileDialog = New Microsoft.Win32.SaveFileDialog()
sfd.FileName = "SpreadNET.xlsx"
sfd.Filter = "Excel Documents (*.xlsx)|*.xlsx"
sfd.DefaultExt = ".xlsx"
Dim sel = sfd.ShowDialog()
If (sel = True) Then
spreadControl.SaveExcel(sfd.FileName, GrapeCity.Windows.SpreadSheet.Data.ExcelFileFormat.XLSX)
End If
End Sub
Run the application to see that the workbook instance can now be exported to Excel when the Export button is clicked.
Download the finished sample app here to review the demo app further: C# Sample or VB Sample
Ready to try it for yourself? Download a free 30-day trial of Spread.NET today!
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 without referring them to an external program.