Export To CSV for Windows Store apps
Scope
In this sample we will show a sample for export data, that is showed in ListView, to a csv file. A CSV file can be opened in Excel, reason I suggest it for how wants export data to excel.
Introduction
In this sample I will show a sample for export data that is showed 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:
https://code.msdn.microsoft.com/windowsapps/site/view/file/73832/1/ClassDiagram1.png
Here is the class that is used to convert the data into a CSV file:
public class CsvExport<T> where T : class
{
public IList<T> Objects;
public CsvExport(IList<T> objects)
{
Objects = objects;
}
public string Export()
{
return Export(true);
}
public string Export(bool includeHeaderLine)
{
var sb = new StringBuilder();
//Get properties using reflection.
var propertyInfos = typeof(T).GetTypeInfo();
if (includeHeaderLine)
{
//add header line.
foreach (var propertyInfo in propertyInfos.DeclaredProperties)
{
sb.Append(propertyInfo.Name).Append(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator);
}
sb.Remove(sb.Length - 1, 1).AppendLine();
}
//add value for each property.
foreach (T obj in Objects)
{
foreach (var propertyInfo in propertyInfos.DeclaredProperties)
{
sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj, null))).Append(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator);
}
sb.Remove(sb.Length - 1, 1).AppendLine();
}
return sb.ToString();
}
//export to a file.
public async void ExportToFile(string path)
{
var storageFolder = KnownFolders.DocumentsLibrary;
var file = await storageFolder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, Export());
}
//export as binary data.
public byte[] ExportToBytes()
{
return Encoding.UTF8.GetBytes(Export());
}
//get the csv value for field.
private string MakeValueCsvFriendly(object value)
{
if (value == null) return "";
if (value is DateTime)
{
if (((DateTime)value).TimeOfDay.TotalSeconds == 0)
return ((DateTime)value).ToString("yyyy-MM-dd");
return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
}
string output = value.ToString();
if (output.Contains(",") || output.Contains("\""))
output = '"' + output.Replace("\"", "\"\"") + '"';
return output;
}
}
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 dependes the list of languages preferences:
If I choose English the ListSeparator is , and for my excel is ; because is that I have in my regional settings
If I choose Portugues the LisSeparator is ; and for my excel is ; too 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.http://c.statcounter.com/10099380/0/91f9aac1/1/