我知道这个问题已经问过很多次了(1,2和3),但我只是不知道如何在我的案例中应用它。我尝试玩了几个小时,但我做对了。
我有List<string>
形式的变量,其中每个列表包含的数据之间有换行符/多行数据。然后,我调用了一个事件,该事件会将这些数据导出到CSV文件中。下面是我的代码。
savestate.cs-我在其中初始化变量的类
public partial class Savestate
{
public static List<string> rtb1_list = new List<string>();
public static List<string> rtb2_list = new List<string>();
public static List<string> rtb3_list = new List<string>();
public static List<string> rtb4_list = new List<string>();
}
Form1.cs-事件
public void Savetocsv()
{
Type s = typeof(Savestate);
FieldInfo[] fields = s.GetFields(BindingFlags.Public | BindingFlags.Static);
StringBuilder csvdata = new StringBuilder();
string header = String.Join(",", fields.Select(f => f.Name).ToArray());
csvdata.AppendLine(header);
string rtb1 = String.Join(",", Savestate.rtb1_list.ToArray());
string rtb2 = String.Join(",", Savestate.rtb2_list.ToArray());
string rtb3 = String.Join(",", Savestate.rtb3_list.ToArray());
string rtb4 = String.Join(",", Savestate.rtb4_list.ToArray());
string newlinestring = string.Format("{0}; {1}; {2}; {3}", rtb1, rtb2, rtb3, rtb4);
csvdata.AppendLine(newlinestring);
string filepath = @"C:\new.csv";
File.WriteAllText(filepath, csvdata.ToString());
}
但是,当我打开CSV文件时,单词到处都是。例如,我写了 hi ,然后写了新行,然后写了再见。这是actual output,这是我的intended output。希望可以得到帮助。
答案 0 :(得分:1)
要在csv
文件中插入换行符,您需要将字符串用双引号引起来,以便通过以下代码生成所需的输出:
public partial class Savestate
{
public static List<string> rtb1_list = new List<string>() { "hi1", "bye1" };
public static List<string> rtb2_list = new List<string>() { "hi2", "bye2" };
public static List<string> rtb3_list = new List<string>() { "hi3", "bye3" };
public static List<string> rtb4_list = new List<string>() { "hi4", "bye4" };
}
public static void Savetocsv()
{
Type s = typeof(Savestate);
FieldInfo[] fields = s.GetFields(BindingFlags.Public | BindingFlags.Static);
StringBuilder csvdata = new StringBuilder();
string header = String.Join(",", fields.Select(f => f.Name).ToArray());
csvdata.AppendLine(header);
string rtb1 = String.Join("\n", Savestate.rtb1_list.ToArray());
string rtb2 = String.Join("\n", Savestate.rtb2_list.ToArray());
string rtb3 = String.Join("\n", Savestate.rtb3_list.ToArray());
string rtb4 = String.Join("\n", Savestate.rtb4_list.ToArray());
string newlinestring = string.Format("\"{0}\",\" {1}\",\" {2}\",\" {3}\"", @rtb1, @rtb2, @rtb3, @rtb4);
csvdata.AppendLine(newlinestring);
string filepath = @"new.csv";
File.WriteAllText(filepath, csvdata.ToString());
}
输出文件:
答案 1 :(得分:1)
我建议在处理CSV时使用CsvHelper Nuget软件包,然后尝试执行以下操作,我添加了扩展方法以将每个rtb列表打印为一个字符串:
public partial class Savestate
{
public static List<string> rtb1_list = new List<string>() { "hi", "bye" };
public static List<string> rtb2_list = new List<string>() { "hi", "bye" };
public static List<string> rtb3_list = new List<string>() { "hi", "bye" };
public static List<string> rtb4_list = new List<string>() { "hi", "bye" };
}
public static class SavestateExtensions
{
public static string GetRtbListAsString(this IEnumerable<string> rtb_list)
{
StringBuilder str = new StringBuilder();
foreach (var value in rtb_list)
{
str.AppendLine(value);
}
return str.ToString();
}
}
然后使用CsvWriter
中的CSVHelper
:
using (var writer = new StreamWriter("file.csv"))
{
using (var csvWriter = new CsvWriter(writer))
{
csvWriter.Configuration.Delimiter = ";";
csvWriter.Configuration.Encoding = Encoding.UTF8;
csvWriter.WriteField("rtb1_list ");
csvWriter.WriteField("rtb2_list ");
csvWriter.WriteField("rtb3_list ");
csvWriter.WriteField("rtb4_list ");
csvWriter.NextRecord();
csvWriter.WriteField(Savestate.rtb1_list.GetRtbListAsString());
csvWriter.WriteField(Savestate.rtb2_list.GetRtbListAsString());
csvWriter.WriteField(Savestate.rtb3_list.GetRtbListAsString());
csvWriter.WriteField(Savestate.rtb4_list.GetRtbListAsString());
csvWriter.NextRecord();
}
}
输出应如下所示: