简体   繁体   中英

How to export ListView Items to excel in WPF MVVM application

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.

I referred to some links but they have used DataGrids which cannot be used in WPF.

Main Window.xaml

<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">
    <Window.Resources>
        <viewmodel:ViewModel x:Key="ViewModel"/>
        <viewmodel:DatetimeToDateConverter x:Key="MyConverter"/>
    </Window.Resources>
    <Grid DataContext="{Binding Source={StaticResource ViewModel}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="Name" HorizontalAlignment="Center"/>
        <TextBox Grid.Row="0" Grid.Column="1" Width="100" HorizontalAlignment="Center" Text="{Binding Student.Name, Mode=TwoWay}" Margin="76,0"/>
        <TextBlock Grid.Row="1" Grid.Column="0" Text="Age" HorizontalAlignment="Center"/>
        <TextBox Grid.Row="1" Grid.Column="1" Width="100" HorizontalAlignment="Center" Text="{Binding Student.Age, Mode=TwoWay}"/>
        <Button Content="Submit" Command="{Binding SubmitCommand}" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="0"/>

        <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">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="{Binding Path=CourseName, Mode=TwoWay}" FontSize="12"/>
                        <TextBlock Text=" - "/>
                        <TextBlock Text="{Binding Path=CourseID, Mode=TwoWay}" FontSize="12"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>

        </ComboBox>
        <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">
            <ListView.View >
                <GridView  >
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="60"/>
                    <GridViewColumn  Header="Age" DisplayMemberBinding="{Binding Age}" Width="60"/>
                    <GridViewColumn  Header="Joining Date" DisplayMemberBinding="{Binding JoiningDate, Converter={StaticResource MyConverter}}" Width="80" />
                    <GridViewColumn Header="Course Name" DisplayMemberBinding="{Binding SCourseIDName.CourseName}" Width="80"/>
                    <GridViewColumn Header="CourseId" DisplayMemberBinding="{Binding SCourseIDName.CourseID}" Width="60"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

</Window>

ViewModelClass

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<Student> _students;
        private ICommand _SubmitCommand;

        public Student Student
        {
            get
            {
                return _student;
            }
            set
            {
                _student = value;
                NotifyPropertyChanged("Student");
            }
        }
        private ObservableCollection<Student> _fillCourseId = new ObservableCollection<Student>();
        public ObservableCollection<Student> FillCourseId
        {
            get { return _fillCourseId; }
            set
            {
                _fillCourseId = value;
                OnPropertyChanged("SystemStatusData");
            }
        }
        public ObservableCollection<Student> 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 => 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 < 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<Student>();
            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));
        }
    }
}

ModelClass

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; }
    }
}

Exporting to csv is fairly simple. All you have to do is add each data separated with a comma(','). Here is a simple demostration.

public string ExportToCsv(IEnumerable<Student> students)
{
    var output = new StringBuilder();
    // Add header if necessary
    output.Append("Name,");
    output.Append("Age ,");
    output.Append("Course ,");
    output.Append("JoiningDate ,");
    output.Append("CourseName ,");
    output.Append("CourseID ,");
    output.AppendLine();
    // Add each row
    foreach (var student in students)
    {
        output.AppendFormat("{0},", student.Name);
        output.AppendFormat("{0},", student.Age);
        output.AppendFormat("{0},", student.Course);
        output.AppendFormat("{0},", student.JoiningDate);
        output.AppendFormat("{0},", student.CourseName);
        output.AppendFormat("{0},", student.CourseID);
        output.AppendLine();
    }
}

I presume that you'll have an 'Export' button somewhere on the page? Bind that button to an Export command (like with the SubmitCommand you already have).

The Export Command will need to take the list of students (pass as a bound command parameter, or from reference depending on where the command is held) and output it as either a CSV (see the answer by Surendra Shrestha) or as Excel. For writing styled Excel, I like the ClosedXml nuget package (check out their documentation ) - but if you don't need styles, CSV is easier, faster, machine-readable and will be compatible across more systems.

If you use the CSV approach, once you've built your CSV string you'll also need to save that as a file. You can use the System.IO.File.WriteAllLines() method to do that.

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