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

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