简体   繁体   中英

DataExport from DataGrid: Replace switch/case with something more generic

I have an datagrid which displays items. The datagrid is bound to a property DataGridItems of type ObservableCollection. In code I dynamically instantiate the property DataGridItems like this:

this.DataGridItems = Db.Instance.RetrieveFoldingBoxes();

or

this.DataGridItems = Db.Instance.RetrieveLabels();

Now I want to export the data displayed (and filtered/sorted) in the datagrid. I could do it like this:

    private void ExportData(ControlDataGrid dataGrid)
    {
        if (dataGrid == null) return;

        //items is type of Telerik.Windows.Data.DataItemCollection
        var items = dataGrid.GetFilteredCollection();

        switch (this.GetSelectedViewComboBoxItemName)
        {
            case "Folding Box":
                var exportItems1 = items.Cast<View_DataExport_FoldingBox>();
                this.GenericExport(exportItems1);
                break;

            //30 cases more...

            case "Label":
                var exportItems2 = items.Cast<View_DataExport_Label>();
                this.GenericExport(exportItems2);
                break;
        }
    }

    private void GenericExport<T>(IEnumerable<T> list) { ... }

But I really don't like this because of this high redundancy. It would be far more elegant If I could do something like this:

    private void ExportData(ControlDataGrid dataGrid)
    {
        if (dataGrid == null) return;

        //items == Telerik.Windows.Data.DataItemCollection
        var items = dataGrid.GetFilteredCollection();

        //Getting the generic type in pseudo code
        //Also far from a good solution because I think CurrentItem could be null
        if(items.CurrentItem == null) return;
        var type = Type.GetType(items.CurrentItem);

        var exportItems = items.Cast<type>();

        this.GenericExport<type>(exportItems);
    }

I'm sure it is possible somehow but currently I have no idea how.


EDIT: here is the code for the GenericExport method:

    private void GenericExport<T>(IEnumerable<T> list)
    {
        using (var excelPackage = new ExcelPackage())
        {
            var name = this.GetSelectedViewComboBoxItemName;

            excelPackage.Workbook.Properties.Company = "COMPANYNAME";
            excelPackage.Workbook.Properties.Author = Configuration.UserName;
            excelPackage.Workbook.Properties.Title = string.Concat("Export for ", name);
            excelPackage.Workbook.Properties.Category = "CATEGORY";

            var worksheet = excelPackage.Workbook.Worksheets.Add(name);

            worksheet.Cells["A1"].LoadFromCollection(list, true, TableStyles.Medium2);
            ExcelSheetFormatter.FormatWorksheet(worksheet, list);

            var fileName = this.GetSelectedViewComboBoxItemName;

            var fileDestination = DataExportHelper.ShowSaveFileDialog(fileName, false);

            if (fileDestination == null) return;

            try
            {
                excelPackage.SaveAs(fileDestination);
            }
            catch (InvalidOperationException)
            {
                throw new PulseException(ExceptionMessage.FileInUse, false);
            }

            DataExportHelper.OpenExportedFileDialog(fileDestination.FullName);
        }
    }

I'm using EPPlus 4.0 Beta 2 (epplus.codeplex.com/releases/view/118053) to export the data to excel.

Therefore I'm bound to the API of EPPlus which is:

    public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, ...);

If you can't change GenericExport signature, then, I'm afraid, reflection is your only friend. Assuming, that items implement IList , contains items of the same type, and it isn't empty:

// make IEnumerable<YourType>
var itemType = items[0].GetType();
var castDef = typeof(Enumerable).GetMethod("Cast");
var cast = castDef.MakeGenericMethod(itemType);
var enumerable = cast.Invoke(null, new[] { items });

// call GenericExport<YourType>
var methodDef = typeof(Program).GetMethod("GenericExport", BindingFlags.Instance | BindingFlags.NonPublic);
var method = methodDef.MakeGenericMethod(itemType);

method.Invoke(this, new[] { enumerable });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM