Welcome Guest, you are in: Login

FDOT Wiki

RSS RSS

GisFramework



Search the wiki
»

Developing New Data Exporters

RSS
Modified on Monday, 18 July 2011 08:48 AM by 156.75.200.64 Categorized as Data Exporters
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.

Table of Contents [Hide/Show]


IDataExporter

Image

Properties

NameTypeDescription
FormatNamestringThis is the name shown in the list of DataExporters.
IconResourceUristringThe Uri for the icon that is displayed for the data exporter.
IsInitializedboolSignals the framework if the Data Exporter is initialized. Only initialized exporters will be available for use.

Methods

NameReturn TypeDescription
Initialize(IDictionary<string, string> customSettings)voidCalled by the framework to initialize the data exporter. Allows custom settings from the configuration to be used.
Export(IEnumerable<Feature> dataSource)voidThe 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

  Name Size
- DataExportersButton.png 4.95 KB
Any Questions or Comments? Email
Some of the icons created by FamFamFam.