Articles in this section
Category / Section

Create Excel file in WPF

3 mins read

Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can create an Excel file in WPF.

Using Syncfusion® Excel (XlsIO) library, you can create an 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 the Syncfusion® Excel (XlsIO) library.

Steps to create an Excel file programmatically:

Step 1: Create a new WPF application project.

Create WPF application in Visual Studio

Create a new WPF application

Step 2: Install the Syncfusion.XlsIO.Wpf NuGet package as a reference to your .NET Framework applications from NuGet.org.

Adding XlsIO reference to the project

Install Syncfusion.XlsIO.WPF NuGet packages

Step 3: Include the following namespaces in the MainWindow.xaml.cs to create an 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 an 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 an 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 the 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 the 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&reg; 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.

Output of Excel File

Generated output of create Excel file in WPF sample code

Open an existing workbook or load the Excel file can be done with the help of UG Documentation. You can open an existing workbook from a stream by using the overloads of the Open methods.

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

Refer here to explore the rich set of Syncfusion&reg; 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

Note:

Starting with v16.2.0.x, if you reference Syncfusion&reg; assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion&reg; license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about create Excel file in WPF.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion&reg; components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied