01 imported from the DataGrid to Excel

There are many network on 01 export data to Excel's method, I found a relatively simple and practical way online (refer to the users of the method)

string fileName = "";
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt = ".xls"; // Default file extension
dlg.Filter = "Excel 工作簿|*.xls"; // Filter files by extension
// Show save file dialog box
Nullable<bool> resultDlg = dlg.ShowDialog();
// Process save file dialog box results
if (resultDlg == true)
{
// Save document
fileName = dlg.FileName;
}
//将DataGrid中的数据导入到excel文件中
Searchresult.SelectAllCells();
Searchresult.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, Searchresult);
//String resultat = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
String result = (string)Clipboard.GetData(DataFormats.UnicodeText);
Searchresult.UnselectAllCells();
//string textToAdd = "Example text in file";
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter file1 = new StreamWriter(fs, Encoding.Default);
file1.WriteLine(result.Replace(',', ' '));
file1.Close();

 02 pro-test utility

Guess you like

Origin www.cnblogs.com/Record-experience/p/12515216.html