Header Ads

WPF .NET Core: Import/Export CSV File

I have previously created a simple C#.NET nuget library for importing/exporting CSV data from file using Datatable as a primary data structure. Now, I have migrated this library exclusively for C#.NET Core projects into a separate nuget library i.e. CSVLibraryAK.Core. You can also utilize this library into your any .NET Core windows application development project as well.

Today, I shall be demonstrating integration of CSVLibraryAK.Core a C#.NET Core library with WPF .NET Core windows application development platform.


Prerequisites:

Following are some prerequisites before you proceed any further in this tutorial:
  1. Install CSVLibraryAK.Core NuGet library.
  2. Knowledge about Windows Presentation Form (WPF).
  3. Knowledge of C# Programming.
  4. Knowledge about C# LINQ.
You can install the library via Nuget packages. The example code is being developed in Microsoft Visual Studio 2019 Professional.

Download Now!

Let's begin now.

1) Create a new MVC web project and name it "WPFImportExportCSV".  
 
2) Open "Tools\Nuget Package Manage\Manage Nuget Packages for Solution...".

3) Install CSVLibraryAK.Core Nuget Package.
 
4) Create a new "Views\Pages\HomePage.xaml" file and add this library into the using directives of "Views\Pages\HomePage.xaml.cs" file as shown below i.e. i.e.

...

namespace namespace WPFImportExportCSV.Views.Pages

{
    ...

    using CSVLibraryAK.Core;

    ...
}

...

In the above code, I have simply included the CSVlibraryAK.Core library into my using directive.

5) Now, create a browse button to import CSV file from end-user and write following lines of code to import CSV file data into Datatables data type structure i.e.

...

// Import CSV file.
datatable = CSVLibraryAK.Import(CSV_FilePath, isHeaderExist);

...

In the above code, I have added code of the library to import CSV file data into Datatables data type structure and then load the resultant data from the Datatable structure object into WPF DataGrid control.

6) Next, create a WPF button control to export CSV data from the DataGrid control to a CSV file i.e.

...

// Export to CSV file.
CSVLibraryAK.Export(exportCSVFilePath, dataTableObj);

...

In the above code, I have added code of the library to export the CSV file data from the WPF DataGrid control Datatable data type structure object into a CSV file, which end-user will then store the CSV file to their desire target location.

7) Now, execute the project and you will be able to see the following in action i.e.



Conclusion

In this article, you will learn to integrate CSVLibraryAK.Core a C#.NET Core library with WPF .NET Core windows application development platform. You will also learn to import CSV file using CSVLibraryAK.Core Import(...) method and to export the CSV file using CSVLibraryAK.Core Export(...) method at your desire target location.

No comments