Export To CSV For Windows Store Apps

Scope

In this article we will show a sample to export data from a ListView to a CSV file. A CSV file can be opened in Excel, that is why I suggest it for exporting the data to Excel.

Introduction

In this sample we will show a sample to export data shown in ListView, to a CSV file.

Description

This sample could be called "Export to Excel", but it is not supported, the only way is to write the data in a CSV file that can be opened in Excel.

Here is the class diagram:



Here is the class for converting the data into a CSV file:

  1. public class CsvExport<T> where T : class  
  2.     {  
  3.         public IList<T> Objects;  
  4.     
  5.         public CsvExport(IList<T> objects)  
  6.         {  
  7.             Objects = objects;  
  8.         }  
  9.     
  10.         public string Export()  
  11.         {  
  12.             return Export(true);  
  13.         }  
  14.     
  15.         public string Export(bool includeHeaderLine)  
  16.         {  
  17.     
  18.             var sb = new StringBuilder();  
  19.                
  20.             //Get properties using reflection.  
  21.            var propertyInfos = typeof(T).GetTypeInfo();  
  22.     
  23.             if (includeHeaderLine)  
  24.             {  
  25.                 //add header line.  
  26.                 foreach (var propertyInfo in propertyInfos.DeclaredProperties)  
  27.                 {  
  28.                     sb.Append(propertyInfo.Name).Append(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator);  
  29.                 }  
  30.                 sb.Remove(sb.Length - 1, 1).AppendLine();  
  31.             }  
  32.     
  33.             //add value for each property.  
  34.             foreach (T obj in Objects)  
  35.             {  
  36.                 foreach (var propertyInfo in propertyInfos.DeclaredProperties)  
  37.                 {  
  38.                     sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj,null))).Append(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator);  
  39.                 }  
  40.                 sb.Remove(sb.Length - 1, 1).AppendLine();  
  41.             }  
  42.     
  43.             return sb.ToString();  
  44.         }  
  45.     
  46.         //export to a file.  
  47.         public async void ExportToFile(string path)  
  48.         {  
  49.            var storageFolder = KnownFolders.DocumentsLibrary;  
  50.            var file = await storageFolder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);  
  51.            await FileIO.WriteTextAsync(file, Export());  
  52.         }  
  53.     
  54.         //export as binary data.  
  55.         public byte[] ExportToBytes()  
  56.         {  
  57.             return Encoding.UTF8.GetBytes(Export());  
  58.         }  
  59.     
  60.         //get the csv value for field.  
  61.         private string MakeValueCsvFriendly(object value)  
  62.         {  
  63.             if (value == nullreturn "";  
  64.                
  65.             if (value is DateTime)  
  66.             {  
  67.                 if (((DateTime)value).TimeOfDay.TotalSeconds == 0)  
  68.                     return ((DateTime)value).ToString("yyyy-MM-dd");  
  69.                 return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");  
  70.             }  
  71.             string output = value.ToString();  
  72.     
  73.             if (output.Contains(",") || output.Contains("\""))  
  74.                 output = '"' + output.Replace("\"""\"\"") + '"';  
  75.     
  76.             return output;  
  77.     
  78.         }  
  79.     } 

There is an important point in that class.

The ListSeparator that you have defined in:



Can be found programmatically using System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator.

But is depends on the list of languages preferences:



If I choose English the ListSeparator is "," (comma) and for my Excel it is ";" (semicolon) because that is what I have in my regional settings.

If I choose Portugues then the ListSeparator is ";" (semicolon) and for my Excel it also is ";" (semicolon) and it works well.

Running the app



The myexportresult.csv file opened in Excel:



Source Code Files

  • BoardItem is my item that has Name, Value and Count properties
  • ConvertingToCSVFileViewModel: is my view model to connect data with the view (I use binding)
  • CsvExport is the class that convert the data into CSV file.

Source Code

The source code is available in MSDN Samples


Similar Articles