I have a html table which I am trying to write to a csv file. I have the rows seperated by a \n in my array and when I print it to the csv file everything works except one problem I'm having. I want each comma value to be printed in the next column. Unfortunately it is not doing that. My current code writes all the values from each row in the html table to the correct rows in the csv.
我有一个html表,我试图写入一个csv文件。我在我的数组中用\ n分隔行,当我将它打印到csv文件时,除了我遇到的一个问题外,一切都有效。我希望在下一列中打印每个逗号值。不幸的是,它没有这样做。我当前的代码将html表中每行的所有值写入csv中的正确行。
try
{
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\spreadsheet.csv", true);
foreach (var i in valueArray)
{
if (i.ToString() == "\n")
{
sw.Write("\n");
}
else
{
{
sw.Write(i.ToString());
sw.Write("\t");//my failed attempt
}
}
}
sw.Flush();
sw.Close();
saveOK = true;
}
values in the array would look like : "first,second,third,fourth,fith,last,\n, first, ....,last,\n,"
数组中的值看起来像:“first,second,third,4th,fith,last,\ n,first,....,last,\ n,”
in the csv it shows up as :
在csv中它显示为:
column 1 column 2
row1 :firstsecondthirdfourthfithlast
row2 :firstsecond....... last
1 个解决方案
#1
1
foreach (var i in valueArray)
{
if (i.ToString() == "\n")
{
sw.Write(sw.NewLine);
}
else
{
sw.Write(i.ToString() + ",");
}
}
#1
1
foreach (var i in valueArray)
{
if (i.ToString() == "\n")
{
sw.Write(sw.NewLine);
}
else
{
sw.Write(i.ToString() + ",");
}
}