public static string cExportTXT(DataView dv) { try { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "txt(*.txt)|*.txt"; saveFileDialog1.FilterIndex = 0; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.CreatePrompt = true; saveFileDialog1.Title = "导出txt文件到 "; DateTime now = DateTime.Now; // saveFileDialog1.FileName = now.Second.ToString().PadLeft(2, '0'); saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_AA"; //now.Year.ToString().PadLeft(2)+now.Month.ToString().PadLeft(2, '0 ') +now.Day.ToString().PadLeft(2, '0 ')+ "_ " +now.Hour.ToString().PadLeft(2, '0 ') +now.Minute.ToString().PadLeft(2, '0 ') + saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName.Length==2) { return ""; } Stream myStream; myStream = saveFileDialog1.OpenFile(); //StreamWriter sw=new StreamWriter(myStream,System.Text.Encoding.GetEncoding( "gb2312 ")); //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312")); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("utf-8")); String str = ""; //写标题 for (int i = 0; i < dv.Table.Columns.Count; i++) { if (i > 0) { str += " "; } str += dv.Table.Columns[i].ColumnName; } sw.WriteLine(str); //写内容 for (int rowNo = 0; rowNo < dv.Count; rowNo++) { String tempstr = ""; for (int columnNo = 0; columnNo < dv.Table.Columns.Count; columnNo++) { if (columnNo > 0) { tempstr += " "; } //tempstr+=dg.Rows[rowNo,columnNo].ToString(); tempstr += dv.Table.Rows[rowNo][columnNo].ToString(); } sw.WriteLine(tempstr); } sw.Close(); myStream.Close(); MessageBox.Show("Export successful!"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return ex.ToString(); } return ""; }