#707 – Dragging a List of Items from a WPF Application into Excel

You can use drag-and-drop in WPF to drag a comma-separated list of values into Excel.  When you drop a CSV list into Excel, it will automatically put each item into a different cell.

In the example below, we have a list of items in a ListBox.


We can drag the list of items by handling the MouseLeftButtonDown event for the blue label.  We create a string containing a comma-separated list of the items.  We then create a DataObject with a format of CommaSeparatedValue.

        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            List<string> stringItems = new List<string>();

            foreach (ListBoxItem lbi in lbEncyclopedias.Items)

            string someValues = string.Join(",", stringItems);

            DataObject data = new DataObject(DataFormats.CommaSeparatedValue, someValues);

            DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Copy);

When we drag this data to Excel, it shows the destination range during the drag.


When we release the mouse button, the items from the list are dropped into cells in this range.