private void Btnexport_click (object sender, RoutedEventArgs e)
{
System.Diagnostics.Stopwatch SW = new System.Diagnostics.Stopwatch ();
Sw. Start ();
Dg. Selectallcells ();
Dg. Clipboardcopymode = Datagridclipboardcopymode.includeheader;
ApplicationCommands.Copy.Execute (NULL, DG);
String result = (string) clipboard.getdata (Dataformats.text);
SaveFileDialog sfd = new SaveFileDialog ();
String dtnow = DateTime.Now.ToString ("Yyyymmddhhmmssffff");
SfD. FileName = Dtnow + "Export to Excel";
SfD. Filter = "Excel file (*.xls) |*.xls| csc file (*.csv) |*.csv| All Files (*. *) |*.* ";
if (SFD. ShowDialog () ==true)
{
String path = System.IO.Path.GetDirectoryName (sfd. FileName);
Dg. Unselectallcells ();
StreamWriter SWR = new StreamWriter (SFD). FileName);
SWR. WriteLine (result. Replace (', ', '));
SWR. Close ();
Sw. Stop ();
String TimeSpan = sw. Elapsedmilliseconds.tostring ();
Long num = dg. Items.Count;
MessageBox.Show ("total" + num + "bar data!\n" + "Export to" + Path + "!\n" + "total time elapsed" + TimeSpan + "millisecond!\n");
}
}
C#,wpf,datagrid,excel, exporting