Click here to Skip to main content
13,768,884 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.4K views
4 bookmarked
Posted 22 Feb 2017
Licenced CPOL

Easy WPF Copy/Paste Excel

Rate this:
Please Sign up or sign in to vote.

Introduction

I have developed an easy way to copy/paste data between WPF application and Excel. This code is compatibility for the controls inherit from System.Windows.Controls.ItemsControls these are the controls with the ItemsSource property. Controls of this type are DataGrid, ListBox, ListView, etc.

Is OpendSource and its code is in GitHub.

Its use is very easy, we will install the utility by nuget and we will setup any properties of our control in the XAML code.

This is how it works:

Prerequisites

The utility has been tested in WPF applications, and we don’t know if run in (Silverlight, WP or WUP) apps.

It is necessary 4.5.2 NET Framework version or later.

Installation

We will install from nuget:

SetUp

After installation has been completed, we will setup the WPF window we adding the next import in XAML:

xmlns:ml="clr-namespace:MoralesLarios.Utilities.Excel;assembly=MoralesLarios.Utilities"

We will Add the following properties in the ItemsControl in XAML:

<DataGrid x:Name="dataGrid" 

          ml:ExcelActions.EnabledCopyExcel ="True"  <!—- enabled copy/copiall excel  -->
          ml:ExcelActions.EnabledPasteExcel="True"  <!—- enabled paste        excel  -->
          />

This simple code enabled the copy/paste data between DataGrid control and Excel.

In terms of its simple form, we will make the actions with the keyboard:

  • Ctrol + A -> CopyAll            -> Copy all DataGrid Rows
  • Ctrol + C -> Copy Selected -> Copy DataGrid selected Rows.
  • Ctrol + P -> Paste                -> Paste data in DataGrid.

Other AttachProperties

We can add functionality with the next AttachProperties:

ml:ExcelActions.EnabledCopyExcel  ="True" 
ml:ExcelActions.EnabledPasteExcel ="True" 
ml:ExcelActions.ContainsHeader    ="False"
ml:ExcelActions.CreateContextMenu ="True"
ml:ExcelActions.CancelWithErrors  ="False"
ml:ExcelActions.PaintFlash        ="True"
ml:ExcelActions.ColorFlash        ="Yellow"
ml:ExcelActions.ShowErrorMessages ="True"

CreateContextMenu

Enabled or disabled showed the context menu in the ItemsControl.

ml:ExcelActions.CreateContextMenu="True"

True is its default value.

ContainsHeader

Enabled or disabled copy control headers.

ml:ExcelActions.ContainsHeader="False"

True is its default value.

PaintFlash

Show or hide the color flash mark in the control that occurs when we copy or we paste in the control.

ml:ExcelActions.PaintFlash="True"

ColorFlash

Setup the color of flash of Copy Paste Action (is only visible if the property PaintFlash is true).

ml:ExcelActions.ColorFlash="Yellow"

Bruhes.Gray is its default value.

ShowErrorMessages

This property specify whether show error message if an errors occurs. For example if the data copied isn’t compatibility with the datasource of control.

ml:ExcelActions.ShowErrorMessages="True"

True is its default value.

In moving:

CancelWithErrors

When we copy many rows from excel, if any row isn’t correct and if an error occur, this property specifies if paste the correct rows or don’t paste any row.

  • True   -> Paste correct rows
  • False  -> Don’t paste nohing.

 

True is a default value.

Recommendations

It is necessary that the ItemsSource property of ItemsControl is an ObservableCollection type, because this type refresh the items correctly and inform the removes and add changes.

Limitations

In this version, in the paste action, only insert data, doesn’t update the rows, or paste incomplete types.

If someone wants asks me and I develop this part.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Juan Francisco Morales Larios
Software Developer (Senior) Cecabank
Spain Spain
MVP C# Corner 2017

MAP Microsoft Active Professional 2014

MCPD - Designing and Developing Windows Applications .NET Framework 4
MCTS - Windows Applications Development .NET Framework 4
MCTS - Accessing Data Development .NET Framework 4
MCTS - WCF Development .NET Framework 4

You may also be interested in...

Comments and Discussions

 
QuestionUnable to Paste? Pin
CCB20107-Mar-18 5:27
memberCCB20107-Mar-18 5:27 
QuestionSelectionUnit="Cell" ? Pin
CCB201028-Feb-18 10:51
memberCCB201028-Feb-18 10:51 
QuestionSend Data from form to IE Pin
ajw198524-Feb-17 0:23
memberajw198524-Feb-17 0:23 
SuggestionMulti language support Pin
Member 1091956223-Feb-17 22:06
memberMember 1091956223-Feb-17 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181117.1 | Last Updated 23 Feb 2017
Article Copyright 2017 by Juan Francisco Morales Larios
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid