Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Export WPF ListView data to excel

Export WPF ListView data to excel

Scheduled Pinned Locked Moved WPF
questioncsharpcsswpf
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pankaj Chamria
    wrote on last edited by
    #1

    Hi, I am using a WPF ListView bound to an observable collection to display records in the form of a Grid. How can I export this data in ListView to excel?

    Pankaj Chamria, Software Programmer.

    E 1 Reply Last reply
    0
    • P Pankaj Chamria

      Hi, I am using a WPF ListView bound to an observable collection to display records in the form of a Grid. How can I export this data in ListView to excel?

      Pankaj Chamria, Software Programmer.

      E Offline
      E Offline
      Eslam Afifi
      wrote on last edited by
      #2

      Why do you think this has something to do with WPF? :confused: Write a method that loops over your collection and write the data to the excel file. Here[^].

      Eslam Afifi

      Z 1 Reply Last reply
      0
      • E Eslam Afifi

        Why do you think this has something to do with WPF? :confused: Write a method that loops over your collection and write the data to the excel file. Here[^].

        Eslam Afifi

        Z Offline
        Z Offline
        zameb
        wrote on last edited by
        #3

        The ListView in WPF is so different to the Forms one. :~ Maybe the problem is how to iterate inside the ListView items. Here it is:

                GridView grid = source.View as GridView;
                if (grid == null)
                {
                    //Sorry, only for GridView views
                    return;
                }
                foreach (GridViewColumn col in grid.Columns)
                {
                    //value of header is col.Header
                }
                foreach (Object item in source.Items)
                {
                    PropertyInfo\[\] dataFields = item.GetType().GetProperties();
                    int i = 0;
                    foreach (GridViewColumn col in grid.Columns)
                    {
                        try
                        {
                            //Get the name of field displayed on grid
                            string strPath = ((Binding)col.DisplayMemberBinding).Path.Path;
                            //Get the field value
                            object propValue = GetValue(item, source.Items, strPath);
                            //Value of cell is propValue
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
        

        I think it will be easier to export (to excel, pdf or anything) from here.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups