title: How to export ListView Items to excel in WPF MVVM application
body: <p>I am getting my data in my list view and I want to export this data to Excel in WPF MVVM. I am new to it can someone please suggest me the approach or suggest the code snippets or edits. it would be really helpful.</p>
  
  <p>I referred to some links but they have used DataGrids which cannot be used in WPF.</p>
  
  <p>Main Window.xaml</p>
  
  <pre><code>&lt;Window x:Class="MVVMDemo.UserRegistrationView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:viewmodel="clr-namespace:MVVMDemo"
      Title="Window1" Height="300" Width="575.851"&gt;
      &lt;Window.Resources&gt;
          &lt;viewmodel:ViewModel x:Key="ViewModel"/&gt;
          &lt;viewmodel:DatetimeToDateConverter x:Key="MyConverter"/&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid DataContext="{Binding Source={StaticResource ViewModel}}"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto"/&gt;
              &lt;ColumnDefinition Width="*"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;TextBlock Grid.Row="0" Grid.Column="0" Text="Name" HorizontalAlignment="Center"/&gt;
          &lt;TextBox Grid.Row="0" Grid.Column="1" Width="100" HorizontalAlignment="Center" Text="{Binding Student.Name, Mode=TwoWay}" Margin="76,0"/&gt;
          &lt;TextBlock Grid.Row="1" Grid.Column="0" Text="Age" HorizontalAlignment="Center"/&gt;
          &lt;TextBox Grid.Row="1" Grid.Column="1" Width="100" HorizontalAlignment="Center" Text="{Binding Student.Age, Mode=TwoWay}"/&gt;
          &lt;Button Content="Submit" Command="{Binding SubmitCommand}" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="0"/&gt;
  
          &lt;ComboBox Grid.Column="1" ItemsSource="{Binding FillCourseId}"  Name="cmb_CourseIDName" HorizontalAlignment="Left" Margin="164.191,5,0,0" 
                    Grid.Row="3" VerticalAlignment="Top" Width="168.342" Grid.RowSpan="2" DataContext="{Binding Source={StaticResource ViewModel}}"  FontSize="8" IsReadOnly="True" 
                    SelectedValue="{Binding Student.SCourseIDName,Mode=TwoWay}" RenderTransformOrigin="1.431,0.77"&gt;
              &lt;ComboBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal" &gt;
                          &lt;TextBlock Text="{Binding Path=CourseName, Mode=TwoWay}" FontSize="12"/&gt;
                          &lt;TextBlock Text=" - "/&gt;
                          &lt;TextBlock Text="{Binding Path=CourseID, Mode=TwoWay}" FontSize="12"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ComboBox.ItemTemplate&gt;
  
          &lt;/ComboBox&gt;
          &lt;ListView ItemsSource="{Binding Students}" Grid.Row="4" Grid.Column="1" Margin="62.551,0,78.762,76.829"
                    ScrollViewer.CanContentScroll="False" 
                   ScrollViewer.VerticalScrollBarVisibility="Visible" Height="85.152" VerticalAlignment="Bottom"&gt;
              &lt;ListView.View &gt;
                  &lt;GridView  &gt;
                      &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="60"/&gt;
                      &lt;GridViewColumn  Header="Age" DisplayMemberBinding="{Binding Age}" Width="60"/&gt;
                      &lt;GridViewColumn  Header="Joining Date" DisplayMemberBinding="{Binding JoiningDate, Converter={StaticResource MyConverter}}" Width="80" /&gt;
                      &lt;GridViewColumn Header="Course Name" DisplayMemberBinding="{Binding SCourseIDName.CourseName}" Width="80"/&gt;
                      &lt;GridViewColumn Header="CourseId" DisplayMemberBinding="{Binding SCourseIDName.CourseID}" Width="60"/&gt;
                  &lt;/GridView&gt;
              &lt;/ListView.View&gt;
          &lt;/ListView&gt;
      &lt;/Grid&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>ViewModelClass</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows.Input;
  using System.Collections.ObjectModel;
  using System.Data.SqlClient;
  using System.Data;
  using System.ComponentModel;
  
  namespace MVVMDemo
  {
      public class ViewModel : ViewModelBase
      {
          static String connectionString = @"Data Source=Ramco-PC\SQLEXPRESS;Initial Catalog=SIT_Ramco_DB;Integrated Security=True;";
          SqlConnection con;
          SqlCommand cmd;
          private Student _student;
          private ObservableCollection&lt;Student&gt; _students;
          private ICommand _SubmitCommand;
  
          public Student Student
          {
              get
              {
                  return _student;
              }
              set
              {
                  _student = value;
                  NotifyPropertyChanged("Student");
              }
          }
          private ObservableCollection&lt;Student&gt; _fillCourseId = new ObservableCollection&lt;Student&gt;();
          public ObservableCollection&lt;Student&gt; FillCourseId
          {
              get { return _fillCourseId; }
              set
              {
                  _fillCourseId = value;
                  OnPropertyChanged("SystemStatusData");
              }
          }
          public ObservableCollection&lt;Student&gt; Students
          {
              get
              {
                  return _students;
              }
              set
              {
                  _students = value;
                  NotifyPropertyChanged("Students");
              }
          }
  
          private Student _selectedcourseIdname;
  
          public Student SelectedCourseIdName
          {
              get { return _selectedcourseIdname; }
              set
              {
                  _selectedcourseIdname = value;
                  OnPropertyChanged("SelectedCourseIdName");
  
              }
  
          }
          public string SelectedCourseId
          {
              get { return _selectedcourseIdname.CourseID; }
              set
              {
                  _selectedcourseIdname.CourseID = value;
                  OnPropertyChanged("SelectedCourseId");
  
              }
  
          }
  
  
          public string SelectedCourseName
          {
              get { return _selectedcourseIdname.CourseName; }
              set
              {
                  _selectedcourseIdname.CourseName = value;
                  OnPropertyChanged("SelectedCourseName");
  
              }
  
          }
  
          public ICommand SubmitCommand
          {
              get
              {
                  if (_SubmitCommand == null)
                  {
                      _SubmitCommand = new RelayCommand(param =&gt; this.Submit(),
                          null);
                  }
                  return _SubmitCommand;
              }
          }
  
          //********************************************* Functions*******************************************// 
  
          public void GetCourseIdFromDB()
          {
              try
              {
                  con = new SqlConnection(connectionString);
                  con.Open();
                  cmd = new SqlCommand("select * from dev_Course", con);
                  SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                  DataTable dt = new DataTable();
                  adapter.Fill(dt);
                  // Student Student = new Student();
  
                  for (int i = 0; i &lt; dt.Rows.Count; ++i)
                      FillCourseId.Add(new Student
                      {
                          CourseID = dt.Rows[i][0].ToString(),
                          CourseName = dt.Rows[i][1].ToString()
                      });
  
              }
              catch (Exception ex)
              {
  
              }
  
          }
  
          public ViewModel()
          {
              Student = new Student();
              Students = new ObservableCollection&lt;Student&gt;();
              Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);
              GetCourseIdFromDB();
          }
  
          void Students_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
          {
              NotifyPropertyChanged("Students");
          }
  
          private void Submit()
          {
              Student.JoiningDate = DateTime.Today.Date;
              //Students.Add(SelectedCourseIdName);
              Students.Add(Student);
              Student = new Student();
          }
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged(string propertyname)
          {
              var handler = PropertyChanged;
              if (handler != null)
                  handler(this, new PropertyChangedEventArgs(propertyname));
          }
      }
  }
  </code></pre>
  
  <p>ModelClass</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace MVVMDemo
  {
      public class Student
      {
          public string Name { get; set; }
          public int Age { get; set; }
          public string Course { get; set; }
          public DateTime JoiningDate { get; set; }
          public string CourseName { get; set; }
          public string CourseID { get; set; }
  
          public Student SCourseIDName { get; set; }
      }
  }
  </code></pre>