Skip to main content
 首页 » 编程设计

c#之Stringbuilder 到 CSV

2024年02月06日14zhengyun_ustc

<分区>

我有以下问题,我正在尝试将内容从我的 StringBuilder 获取到我的 .csv 文件:

public String GetCSV() 
            { 
                System.Text.StringBuilder sb = new StringBuilder(); 
 
                sb.Append(m_ID.ToString()); 
                sb.Append(";"); 
                sb.Append(m_Starttime.ToString()); 
                sb.Append(";"); 
                sb.Append(m_EndTime.ToString()); 
                sb.Append(";"); 
                sb.Append(m_IsSerie.ToString()); 
                sb.Append(";"); 
                sb.Append(m_Title); 
                sb.Append(";"); 
                sb.Append(m_Description); 
                sb.Append(";"); 
                sb.Append(m_Lastchange.ToString()); 
                sb.Append(";"); 
 
                return sb.ToString(); 
                WriteCSV(); 
            } 
 
 
        public void WriteCSV() 
        {                 
            string csvpath = @"c:\Temp\Kalender.csv"; 
 
            if (File.Exists(csvpath)) 
            { 
                File.Delete(csvpath); 
            } 
 
                using (StreamWriter sw = File.CreateText(csvpath)) 
                { 
                    foreach (string CalendarItem in CalendarItem) 
                    { 
                        if (sb.Length > 0) 
                            sb.Append(", "); 
 
                        sb.Append(part); 
                    } 
 
                } 
 
        } 

现在我已经知道如何创建 CSV 文件了,但我不知道如何用 StringBuilder 中的内容填充它 我确定这将由 foreach 循环处理,对吧?