datatable 数据导出为csv格式文件(C#描述)
 1
    //导出为svc文件
2
        public void ExportToSvc(System.Data.DataTable dt,string strName)
3
        {
4
             string strPath= Path.GetTempPath()+strName+".csv";
5
        
6
            if (File.Exists(strPath))
7
            {
8
                File.Delete(strPath);
9
            }
10
            //先打印标头
11
            StringBuilder strColu=new StringBuilder();
12
            StringBuilder strValue=new StringBuilder();
13
            int i=0;
14
    
15
            try
16
            {
17
                StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
18
19
                for( i=0;i<=dt.Columns.Count-1;i++)
20
                {
21
                    strColu.Append(dt.Columns[i].ColumnName);
22
                    strColu.Append(",");
23
                }
24
                strColu.Remove(strColu.Length-1,1);//移出掉最后一个,字符
25
26
                sw.WriteLine(strColu);
27
28
                foreach(DataRow dr in dt.Rows)
29
                {
30
                    strValue.Remove(0,strValue.Length);//移出
31
        
32
                    for(i=0;i<=dt.Columns.Count-1;i++)
33
                    {
34
                        strValue.Append(dr[i].ToString());
35
                        strValue.Append(",");
36
                    }
37
                    strValue.Remove(strValue.Length-1,1);//移出掉最后一个,字符
38
                    sw.WriteLine(strValue);
39
                }
40
                
41
                sw.Close();
42
            }
43
            catch(Exception ex)
44
            {
45
                MessageBox.Show(ex.Message);
46
47
            }
48
49
            System.Diagnostics.Process.Start(strPath);
50
                
51
        }
52
        
    //导出为svc文件2
        public void ExportToSvc(System.Data.DataTable dt,string strName)3
        {4
             string strPath= Path.GetTempPath()+strName+".csv";5
        6
            if (File.Exists(strPath))7
            {8
                File.Delete(strPath);9
            }10
            //先打印标头11
            StringBuilder strColu=new StringBuilder();12
            StringBuilder strValue=new StringBuilder();13
            int i=0;14
    15
            try16
            {17
                StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));18

19
                for( i=0;i<=dt.Columns.Count-1;i++)20
                {21
                    strColu.Append(dt.Columns[i].ColumnName);22
                    strColu.Append(",");23
                }24
                strColu.Remove(strColu.Length-1,1);//移出掉最后一个,字符25

26
                sw.WriteLine(strColu);27

28
                foreach(DataRow dr in dt.Rows)29
                {30
                    strValue.Remove(0,strValue.Length);//移出31
        32
                    for(i=0;i<=dt.Columns.Count-1;i++)33
                    {34
                        strValue.Append(dr[i].ToString());35
                        strValue.Append(",");36
                    }37
                    strValue.Remove(strValue.Length-1,1);//移出掉最后一个,字符38
                    sw.WriteLine(strValue);39
                }40
                41
                sw.Close();42
            }43
            catch(Exception ex)44
            {45
                MessageBox.Show(ex.Message);46

47
            }48

49
            System.Diagnostics.Process.Start(strPath);50
                51
        }52
        
                    
                
        
            
                
            
        
浙公网安备 33010602011771号