这里有个方法可以把填充listview的DataTable导出成csv文件,速度较快public static void SaveAsExcel(DataTable dt1)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "导出文件 (*.csv)|*.csv";
sfd.FilterIndex = 0;
sfd.RestoreDirectory = true;
sfd.Title = "导出文件保存路径";
sfd.ShowDialog();
string strFilePath = sfd.FileName;
StringBuilder strValue = new StringBuilder(); StreamWriter sw = new StreamWriter(new FileStream(strFilePath, FileMode.CreateNew), Encoding.Default);
// 输出表头
BusiDetail bd = new BusiDetail(); 这里的BusiDetail是你执行导出操作所在的BusiDetail.xaml
bd.WriteHeader(sw); foreach (DataRow dr in dt1.Rows)
{
strValue.Remove(0, strValue.Length); for (int i = 0; i <= dt1.Columns.Count - 1; i++)
{
strValue.Append(dr[i].ToString());
strValue.Append(",");
}
strValue.Remove(strValue.Length - 1, 1);//移出掉最后一个,字符
sw.WriteLine(strValue);
}
sw.Close(); System.Windows.MessageBox.Show("导出文件成功!","成功", MessageBoxButton.OK,MessageBoxImage.Information);
} // 输出表头
private void WriteHeader(StreamWriter sw)
{
string strHeader = "时间,中文名称,英文名称,地市名称";
sw.WriteLine(strHeader);
}