码迷,mamicode.com
首页 > Windows程序 > 详细

C#,WPF,DataGrid,Excel,导出

时间:2016-08-16 00:11:34      阅读:857      评论:0      收藏:0      [点我收藏+]

标签:

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 + "导出到Excel";
sfd.Filter = "Excel文件(*.xls)|*.xls|Csc文件(*.csv)|*.csv|所有文件(*.*)|*.*";
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("共有" + num + "条数据!\n" + "导出到" + path + "!\n" + "共耗时" + timeSpan + "毫秒!\n");
}
}

C#,WPF,DataGrid,Excel,导出

标签:

原文地址:http://www.cnblogs.com/Fred1987/p/5774579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!