csv文件導出

 

 void outCsv(string strsql, string filepath)
    
{
        MyCRM.DB db 
= new MyCRM.DB();
        
if (System.IO.File.Exists(filepath))
        
{
            System.IO.File.Delete(filepath);
        }

        System.IO.FileStream fsDestFile 
= System.IO.File.Open(filepath, System.IO.FileMode.OpenOrCreate);
        System.IO.StreamWriter srDestFile 
= null;
        
        DataTable dt 
= db.ececSelectSql(strsql);
        
string str = "";
        
        
foreach (DataRow dr in dt.Rows)
        
{
            
string str1 = "";
            
for (int i = 0; i < dr.ItemArray.Length; i++)
            
{
                
switch (dr[i].ToString())
                
{
                    
case "":
                        str1 
+= " ,"
                        
break;
                    
default:
                        str1 
+= dr[i].ToString().Trim() + ",";
                        
break;
                }

                 
            }

            str1 
= str1.Remove(str1.Length - 11+ " ";            
            str 
+= str1;
        }

        srDestFile 
= new System.IO.StreamWriter(fsDestFile, System.Text.Encoding.Default); //重要
        srDestFile.WriteLine(str);
        srDestFile.Flush();
        srDestFile.Close();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章