本文介绍了出口C#列表到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图导出我的C#列表到CSV文件。全部设置好。但事实是场分离器工作不正常。它显示像,我的字符串结尾(例如:0000324df)。这是我的控制器code。
的IEnumerable stockexpo = stockexp; //赋值
MemoryStream的输出=新的MemoryStream();
StreamWriter的作家=新的StreamWriter(输出,Encoding.UTF8);
writer.Write(货号,);
writer.Write(重复计数);
writer.WriteLine();
的foreach(在stockexpo StockResult顺序)
{
writer.Write(的String.Format({0:D},order.ItemNumber));
writer.Write(\\);
writer.Write(,);
writer.Write(\\);
writer.Write(order.Count);
writer.Write(\\);
writer.Write(,);
writer.WriteLine();
}
writer.Flush();
output.Position = 0; 返回文件(输出,文/逗号分隔值,stockexp.csv);
我需要知道我可以适当地单独的字段值。任何人都可以帮我的。
解决方案
writer.Write(\\);
code的这条线将是一个每次都输。为什么它在所有?
另外,我不会有的WriteLine前一逗号,因为没有必要限定该文件的末尾。
的IEnumerable stockexpo = stockexp; //赋值
MemoryStream的输出=新的MemoryStream();
StreamWriter的作家=新的StreamWriter(输出,Encoding.UTF8);
writer.Write(货号,);
writer.Write(重复计数);
writer.WriteLine();
的foreach(在stockexpo StockResult顺序)
{
writer.Write(order.ItemNumber);
writer.Write(,);
writer.Write(order.Count);
writer.WriteLine();
}
writer.Flush();
output.Position = 0; 返回文件(输出,文/逗号分隔值,stockexp.csv);
I was trying to export my C# list to Csv file. All is set well. But the thing is field seperator is not working properly. its showing like, my string with " at the end (eg: 0000324df"). Here is my Controller code.
IEnumerable stockexpo = stockexp; // Assign value
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
writer.Write("ItemNo,");
writer.Write("Repeat Count");
writer.WriteLine();
foreach (StockResult order in stockexpo)
{
writer.Write(String.Format("{0:d}", order.ItemNumber));
writer.Write("\"");
writer.Write(",");
writer.Write("\"");
writer.Write(order.Count);
writer.Write("\"");
writer.Write(",");
writer.WriteLine();
}
writer.Flush();
output.Position = 0;
return File(output, "text/comma-separated-values", "stockexp.csv");
I need to know how i can seperate the field values appropriately. Anyone can help me for this.
解决方案
writer.Write("\"");
This line of code will be outputting a " every time. Why have it at all?
Also, I wouldn't have a comma before the WriteLine, since there is no need to delimit the end of the file.
IEnumerable stockexpo = stockexp; // Assign value
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
writer.Write("ItemNo,");
writer.Write("Repeat Count");
writer.WriteLine();
foreach (StockResult order in stockexpo)
{
writer.Write(order.ItemNumber);
writer.Write(",");
writer.Write(order.Count);
writer.WriteLine();
}
writer.Flush();
output.Position = 0;
return File(output, "text/comma-separated-values", "stockexp.csv");
这篇关于出口C#列表到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!