Loading CSV Files in UWP

Video summarizing this post:

I’ve recently worked on a series of UWP apps that needed to load up a CSV file. I’m tired of looking this up every time, so I’ve started a small git repo for stuff that I have to do a lot when I’m playing around with my apps.

1) Based on Jonathan Wood’s CSVFileReader class, I add the CsvParse.cs file into my app.

2) With my snippets I add a file picker (snippet IoLoadFilesWithPicker) that will launch the UI to allow the user to select their csv files. The nice thing about this snippet is that it will also add file tokens so the user can re-access these files without having to pick them again the next time they open the application.

3) the csvReader snippet loads the basic code for opening a csv file and begin parsing through it.

Here’s the code I used:

// let the user load up a csv file 
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;

var file = await picker.PickSingleFileAsync();
// then user the CsvParse.cs file from https://github.com/matthiasxc/uwp-kickstart to parse the file
using (CsvParse.CsvFileReader csvReader = new CsvParse.CsvFileReader(await file.OpenStreamForReadAsync()))
    CsvParse.CsvRow row = new CsvParse.CsvRow();
    while (csvReader.ReadRow(row))
        string newRow = "";
        // add the columns one at a time
        for(int i =0; i <row.Count; i++)
            newRow += row[i] + ",";

        // add the row to our ObservableCollection object
        // we'll assign this to our UI ListView