Create Excel file in WPF
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can create Excel file in WPF.
Using Syncfusion Excel (XlsIO) library, you can create Excel file in Windows Forms, WPF, UWP, ASP.NET Web, ASP.NET MVC, ASP.NET Core and Xamarin applications. Also, you can open an existing Excel workbook by using the Open method from Syncfusion Excel (XlsIO) library.
Steps to create Excel file programmatically:
Step 1: Create a new WPF application project.
Create a new WPF application
Step 2: Install the Syncfusion.XlsIO.Wpf NuGet package as reference to your .NET Framework applications from NuGet.org.
Install Syncfusion.XlsIO.WPF Nuget packages
Step 3: Include the following namespaces in the MainWindow.xaml.cs to create Excel file.
C#
using Syncfusion.XlsIO; using System; using System.ComponentModel; using System.IO; using System.Windows;
VB.NET
Imports Syncfusion.XlsIO Imports System Imports System.ComponentModel Imports System.IO Imports System.Windows
Step 4: Add a new button in MainWindow.xaml to create Excel file as follows.
XAML
<Button Click="btnCreate_Click" Margin="0,0,10,12" VerticalAlignment="Bottom" Height="30" BorderBrush="LightBlue" HorizontalAlignment="Right" Width="180"> <Button.Background> <LinearGradientBrush EndPoint="0.5,-0.04" StartPoint="0.5,1.04"> <GradientStop Color="#FFD9E9F7" Offset="0"/> <GradientStop Color="#FFEFF8FF" Offset="1"/> </LinearGradientBrush> </Button.Background> <StackPanel Orientation="Horizontal" Height="23" Margin="0,0,0,-2.52" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100"> <Image Name="image2" Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" /> <TextBlock Text="Create Document" Height="15.96" Width="126" Margin="0,4,0,3" /> </StackPanel> </Button>
Step 5: Add the following code in btnCreate_Click to create Excel file in WPF with simple text “Syncfusion Essential XlsIO”.
C#
//Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { //Set the default application version as Excel 2016 excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016; //Create a workbook with a worksheet IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1); //Access first worksheet from the workbook instance IWorksheet worksheet = workbook.Worksheets[0]; //Insert sample text into cell “A1” worksheet.Range["A1"].Text = "Syncfusion Essential XlsIO"; //Save the workbook to disk in xlsx format workbook.SaveAs("XlsIO_Output.xlsx"); }
VB.NET
'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine 'Set the default application version as Excel 2016 excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016 'Create a workbook with a worksheet Dim workbook As IWorkbook = excelEngine.Excel.Workbooks.Create(1) 'Access first worksheet from the workbook instance Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Insert sample text into cell “A1” worksheet.Range("A1").Text = "Syncfusion Essential XlsIO" 'Save the workbook to disk in xlsx format workbook.SaveAs("XlsIO_Output.xlsx") End Using
A complete working example to create Excel file in WPF using Syncfusion Excel (XlsIO) library can be downloaded from Create Excel file.zip.
By executing the WPF sample program, you will get the output Excel file as shown below.
Generated output of create Excel file in WPF sample code
Open existing workbook or load the Excel file can be done with the help of UG Documentation. You can open an existing workbook from stream by using the overloads of Open methods.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
An online sample link to create Excel file.
See Also:
Create Excel file in Windows Forms using Syncfusion Essential XlsIO
Create Excel file in ASP.NET Core using Syncfusion Essential XlsIO
Create Excel file in ASP.NET MVC using Syncfusion Essential XlsIO
Create Excel file in Xamarin using Syncfusion Essential XlsIO
Create Excel file in UWP using Syncfusion Essential XlsIO
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.