There are various ways to export DataGrid data into excel sheet using Microsoft.Office.Interpo dll and using excel worksheet objects. If we want to export DataGrid data into excel sheet with help of Interpo dll we are doing the following steps:
- Taking reference of Microsoft.Office.Inerpo dll.
- Creating object of Excel class.
- Creating object of WorkSheet class.
- Creating object of Range class (We can insert data using cell object, but here I am explaining about range).
- And inserting data grid into range.
- Saving excel sheet and clearing object.
Here I am going to demonstrate anther way to export data grid data into excel sheet. To do this I have followed up the following 3 simple steps:
- Filled data grid with some data.
- Copied data grid data with header info into clip board.
- Saved these copied data into excel sheet.
Fill DataGrid
Fill your DataGrid with any collection, here I have taken student info collection data; the details about a student.
Copy DataGrid data to clipboard
Copy the data grid data to clipboard to save in excel with the help of ApplicationCommands. Here I have written the code that helps to copy data grid data:
- this.dgvStudents.SelectAllCells();
- this.dgvStudents.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
- ApplicationCommands.Copy.Execute(null, this.dgvStudents);
- this.dgvStudents.UnselectAllCells();
In this above code 1 I have selected all the cell and in code 2 set the ClipboardCopyMode property to IncludeHeader, that will help us to copy the datagrid data with column header. In line 3 I have executed the copy command with the help of class ApplicationcCommands. In line 4 again I
unselected all the DataGrid cells.
Save copied data from clipboard to excel sheet
The following code helps to store or save data from clipboard to excel sheet with the help of StreamWriter class.
- String result = (string) Clipboard.GetData(DataFormats.CommaSeparatedValue);
- try
- {
- StreamWriter sw = new StreamWriter("export.csv");
- sw.WriteLine(result);
- sw.Close();
- Process.Start("export.csv");
- }
- catch(Exception ex)
- {}
The following is the full code that will help you understand properly.
XAML
- <Window x:Class="WPFDataGridExport.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
- <DockPanel>
- <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" DockPanel.Dock="Bottom">
- <Button Content="Export" Click="Button_Click_1" Margin="5"></Button>
- </StackPanel>
- <DataGrid AutoGenerateColumns="False" Name="dgvStudents">
- <DataGrid.Columns>
- <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=Name, Mode=Default}"></DataGridTextColumn>
- <DataGridTextColumn Header="Roll no." Width="100" Binding="{Binding Path=RollNo, Mode=Default}"></DataGridTextColumn>
- <DataGridTextColumn Header="Age" Width="100" Binding="{Binding Path=Age, Mode=Default}"></DataGridTextColumn>
- </DataGrid.Columns>
- </DataGrid>
- </DockPanel>
- </Window>
Shubham JainPosted Nov 4, 2016, 3:18 AM
What if i have to show two datagrids on one excel sheet in wpf c#
Santhakumar MunuswamyPosted Nov 28, 2015, 2:14 AM
Good One
Former memberPosted Nov 3, 2015, 3:49 AM
Thanks sibeesh venu
Sibeesh VenuPosted Nov 2, 2015, 4:44 AM
Nice Share
Former memberPosted Nov 1, 2015, 11:27 PM
thanks harshad
Harshad PansuriyaPosted Nov 1, 2015, 11:00 PM
Nice one