May 31, 2014

WPF Datagrid to Excel export

private void ExportToExcel(DataGrid grid) { grid.SelectAllCells(); grid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader; ApplicationCommands.Copy.Execute(null, grid); grid.UnselectAllCells(); 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) { MessageBox.Show(ex.Message); } }

0 comments:

Post a Comment

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib