MVVM Excel Export

4 posts, 0 answers
  1. Minh
    Minh avatar
    43 posts
    Member since:
    May 2014

    Posted 21 Oct 2015 Link to this post

    I currently implemented Excel Export behind code, I'm trying to have it be implemented in the ViewModel Instead. How abouts would I do that?

     

     

     

            private void MenuItem_OnClick(object sender, RoutedEventArgs e)
            {
                try
                {
                    string content;

                    var extension = "xml";
                    content = DrillThroughGridView.ToExcelML(true);

                    string path = String.Format("Export.{0}", extension);

                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }

                    using (FileStream fs = File.Create(path))
                    {
                        Byte[] info = Encoding.Default.GetBytes(content);
                        fs.Write(info, 0, info.Length);
                    }

                    Process.Start(path);
                }
                catch (Exception)
                {
                    Notification.Error("Process Busy", "Please exit excel instance.");
                }
            }​

     

     

  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 22 Oct 2015 Link to this post

    Hello Minh,

    You can create a command in your view model and bind it to the MenuItem's Command property, so it will be executed when the item is clicked.

    Regards,
    Petya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Minh
    Minh avatar
    43 posts
    Member since:
    May 2014

    Posted 28 Oct 2015 Link to this post

    Hi Petya, 

    I the viewmodel command part is relatively easy, how would I get "content = DrillThroughGridView.ToExcelML(true)" on the ViewModel side? 
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 30 Oct 2015 Link to this post

    Hi Minh,

    You can pass the RadGridView instance as a parameter to the command. There are several examples using this approach in our demos, for example, Customizing ExcelML export. I also encourage you to take a look at the ExportToXlsx option we introduced in one of our recent releases in this article and the Exporting to Xlsx and Pdf demo.

    Regards,
    Petya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top