Skip to main content Skip to footer

How to Convert Excel XLSX to CSV Files in a WPF App using C#

  • 0 Comments
Quick Start Guide
Tutorial Concept

Learn how to build a C# WPF app that exports spreadsheet data to CSV and converts Excel (.xlsx) files to CSV using a WPF Spreadsheet Component.

What You Will Need
Controls Referenced

Spread.NET - WPF Spreadsheet Component

Documentation

When building WPF applications that handle spreadsheet data, exporting to CSV is a common requirement—especially for data exchange with other systems or legacy tools. While CSV is lightweight and easy to process, many users also work with rich Excel files (.xlsx). This tutorial shows how to use the WPF spreadsheet control, included in Spread.NET, in a .NET app to load Excel workbooks and export their data to CSV—all from the application’s UI.

Download a 30-Day Trial of this Powerful WPF Spreadsheet Component!

Import XLSX and Convert to CSV in a .NET WPF Apps using C#:

  1. Configure a .NET WPF Spreadsheet Application
  2. Load an Excel (.xlsx) File into the WPF Spreadsheet- OpenExcel Method
  3. Export a .NET WPF Spreadsheet Data to a CSV File - SaveAs Method

Download a Sample WPF Spreadsheet App to Follow Along with This Tutorial!


Configure a .NET WPF Spreadsheet Application

Open Visual Studio 2022 and create a New Project. Select WPF Application, name the project XLSX_to_CSV_WPF, and select .NET 8.0 as the framework.

Create a .NET 8 WPF Spreadsheet Application

In the MainWindow.xaml.cs file, add place holder functions that we will later add logic to.

public partial class MainWindow : Window
{
    public static RoutedUICommand OpenXlsxCommand = new RoutedUICommand(
        "Open XLSX", "OpenXlsx", typeof(MainWindow));
    public static RoutedUICommand SaveCsvCommand = new RoutedUICommand(
        "Save CSV", "SaveCsv", typeof(MainWindow));
    public MainWindow()
    {
        InitializeComponent();
        // Bind Command to Menu Items
        CommandBindings.Add(new CommandBinding(OpenXlsxCommand, OpenXlsxExecuted));
        CommandBindings.Add(new CommandBinding(SaveCsvCommand, SaveCsvExecuted));
    }
    private void OpenXlsxExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        // Load an Existing XSX File
    }
    private void SaveCsvExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        // Export Instace to CSV
    }
}

Next, add a Menu control from the Toolbox under All WPF Controls. Create two menu items under File: Open XLSX and Save CSV. Then, in the MainWindow.xaml editor, bind each menu item to the corresponding command defined in MainWindow.xaml.cs.

<DockPanel>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="_File">
            <MenuItem Header="_Open XLSX" Command="{x:Static local:MainWindow.OpenXlsxCommand}"></MenuItem>
            <MenuItem Header="_Save CSV" Command="{x:Static local:MainWindow.SaveCsvCommand}"></MenuItem>
        </MenuItem>
    </Menu>
</DockPanel>

Menu Item in WPF Spreadsheet Application - Import XLSX and Export CSV

Drag a Visual Studio Grid control into your layout. Define four RowDefinitions—use fixed heights, such as 30 for the top row to reserve space for the spreadsheet formula bar, and 350 for a lower row that will hold the WPF spreadsheet control.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition/>
        <RowDefinition Height="350"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
</Grid>

Split the WPF Grid for a Formula Bar and Spreadsheet Control

Next, on the Solution Explorer, expand the project, XLSX_to_CSV_WPF; then, right-click Dependencies and select Manage NuGet Packages… Search for and install Mescius.Spread.Wpf, the WPF spreadsheet component we will use in this tutorial.

WPF Spreadsheet NuGet Package

Drag and drop a GcSpreadSheet control to the bottom grid row. Then in the top row, add a GcSpreadNameBox and ensure the GcFormulaBar is associated to the spreadsheet instance.

 <gss:GcFormulaBar x:Name="formulaBar1" Grid.Row="0" ShowEditingButtons="True" Expandable="True"  AssociatedSpread="{x:Reference Name=spreadSheet1}"/>
 <gss:GcSpreadSheet x:Name="spreadSheet1" Grid.Row="1" Grid.RowSpan="3"/>

The WPF application will now have the WPF spreadsheet control and associated formula bar added to the grid:

Create a WPF Spreadsheet Application - .NET 8


Load an Excel (.xlsx) File into the WPF Spreadsheet

We can now add the ability to import an existing Excel XLSX file into the current WPF spreadsheet instance using Spread for WPF’s OpenExcel method.

private void OpenXlsxExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        // Load an Existing XSX File
        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 
            spreadSheet1.OpenExcel(ofd.FileName);
        }
   }

Load an Excel (.xslx) File into a .NET WPF Application

Check out the WPF Spreadsheet’s Load Files documentation to learn more.


Export WPF Spreadsheet Data to a CSV File using C#

CSV exporting capabilities can be invoked in the WPF application using the Spread for WPF’s SaveAs method and specifying the data as a .csv file using the CSV field of the FileFormat enumeration.

private void SaveCsvExecuted(object sender, ExecutedRoutedEventArgs e)
{
    // Export WPF Instace to CSV
    Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
    sfd.FileName = "SPNET_XLSX_to_CSV.csv";
    sfd.Filter = "Excel Documents (*.csv)|*.csv";
    sfd.DefaultExt = ".csv";
    var sel = sfd.ShowDialog();
    if (sel == true)
    {
        spreadSheet1.Workbook.SaveAs(sfd.FileName, GrapeCity.Spreadsheet.IO.FileFormat.CSV);
    }
}

Export WPF Spreadsheet Data to CSV File using C#

See Spread for WPF’s Save as Comma-Separated Value (.csv) documentation to learn more.


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.