New data exporters can be created for use in the framework by implementing the interface FDOT.GIS.Client.Domain.DataExporters.IDataExporter and making it exportable through MEF.
IDataExporter
Properties
Name | Type | Description |
---|
FormatName | string | This is the name shown in the list of DataExporters. |
IconResourceUri | string | The Uri for the icon that is displayed for the data exporter. |
IsInitialized | bool | Signals the framework if the Data Exporter is initialized. Only initialized exporters will be available for use. |
Methods
Name | Return Type | Description |
---|
Initialize(IDictionary<string, string> customSettings) | void | Called by the framework to initialize the data exporter. Allows custom settings from the configuration to be used. |
Export(IEnumerable<Feature> dataSource) | void | The logic for exporting the features is placed in this method. |
Example
[Export(typeof(IDataExporter))]
public class ExcelDataExporter : IDataExporter
{
public string FormatName { get { return "Excel"; } }
public void Export(IEnumerable<Feature> dataSource)
{
if (dataSource.Count() == 0)
{
MessageBox.Show("No results to export.");
return;
}
var dlg = new SaveFileDialog { Filter = "Excel Document (.xlsx)|*.xlsx", DefaultExt = ".xlsx" };
if (dlg.ShowDialog() == true)
{
var doc = new SpreadsheetDocument();
doc.ApplicationName = "FDOT.GIS.Framework";
doc.Creator = "FDOT.GIS.Framework";
var keys = dataSource.First().FeatureType.Attributes;
var i = 0;
foreach (var key in keys)
{
doc.Workbook.Sheets[0].Sheet.Rows[0].Cells[i].SetValue(key.Description);
i++;
}
var j = 1;
foreach (var feature in dataSource)
{
i = 0;
foreach (var key in keys)
{
var cell = doc.Workbook.Sheets[0].Sheet.Rows[j].Cells[i];
if (feature[key] != null && feature[key].GetType() != typeof(string))
{
Type rowDataType = feature[key].GetType();
if (rowDataType == typeof(DateTime))
{ cell.SetValue((DateTime)feature[key]); }
else if (rowDataType == typeof(bool))
{ cell.SetValue((bool)feature[key]); }
decimal rowValue = 0;
bool result = Decimal.TryParse(feature[key].ToString(), out rowValue);
if (result)
{ cell.SetValue(rowValue); }
else
{ cell.SetValue((feature[key] ?? "").ToString()); }
}
else
{ cell.SetValue((feature[key] ?? "").ToString()); }
i++;
}
j++;
}
using (var storage = new ZipStreamProvider(dlg.OpenFile() ))
{
doc.Save(storage);
}
}
LayoutManager.Instance.ShowMessage("Excel export complete.");
}
public string IconResourceUri
{
get { return "Assets/Images/excel_icon.png"; }
}
/// <summary>
/// Returns true if the command has been initialized.
/// </summary>
public bool IsInitialized { get; private set; }
/// <summary>
/// Initialize the command with the provided settings.
/// </summary>
/// <param name="customSettings"></param>
public void Initialize(IDictionary<string, string> customSettings)
{
if (!IsInitialized)
OnInitialize(customSettings);
IsInitialized = true;
}
protected virtual void OnInitialize(IDictionary<string, string> customSettings)
{ }
}
See Also