public static void ZdExportToExcel(System.Windows.Controls.DataGrid dg, string name, MetroWindow frm)
{
dg.SelectAllCells();
dg.ClipboardCopyMode = System.Windows.Controls.DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dg);
string result = (string)Clipboard.GetData(DataFormats.Text);
string fileName = AppDomain.CurrentDomain.BaseDirectory+name+ ".xls";
dg.UnselectAllCells();
StreamWriter swr = new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312"));
swr.WriteLine(result.Replace(',', ' '));
swr.Close();
}
public static void ExportToExcel(System.Windows.Controls.DataGrid dg, string name, MetroWindow frm)
{
dg.SelectAllCells();
dg.ClipboardCopyMode = System.Windows.Controls.DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dg);
string result = (string)Clipboard.GetData(DataFormats.Text);
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = name;
sfd.Filter = "Excel文件(*.xls)|*.xls|Csc文件(*.csv)|*.csv|所有文件(*.*)|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = System.IO.Path.GetDirectoryName(sfd.FileName);
dg.UnselectAllCells();
StreamWriter swr = new StreamWriter(sfd.FileName, false, Encoding.GetEncoding("gb2312"));
swr.WriteLine(result.Replace(',', ' '));
swr.Close();
}
}