我试图将一个对象写入csv,问题是我的对象具有浮动值为例(14,9)我想将它们更改为(14.9)所以它不会导致任何csv格式的问题
string csv = "";
using (var ctx = new NBAEntities2())
{
var studentList = ctx.TotalStat.SqlQuery("Select * from TotalStat where IDPlayer<5")
.ToList<TotalStat>();
List<object> mycollection = new List<object>();
string type = "";
foreach (var item in studentList)
{
type = item.GetType().ToString();
mycollection.Add(item);
}
string iteem = mycollection.First().ToString();
IEnumerable<PropertyInfo> props = mycollection.First().GetType().GetProperties();
//header
List<string> test = new List<string>();
csv += String.Join(",", props.Select(prop => prop.Name)) + "\r\n";
//rows
foreach (var entityObject in mycollection)
{
csv += String.Join(",", props.Select(
prop => (prop.GetValue(entityObject, null) ?? "?").ToString()
))
+ "\r\n";
}
}
File.WriteAllText("D:/Test.csv", csv.ToString());
答案 0 :(得分:0)
可能在你的情况下,最简单的方法是更改当前的线程文化然后恢复它。
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
您还可以在中指定格式提供程序
(prop.GetValue(entityObject, null) ?? "?").ToString()
但在这种情况下,您可能需要检查prop.GetValue(entityObject, null)
是IFormattable
(或者是单身还是双人),然后应用指定ToString
的{{1}}。
答案 1 :(得分:0)
我找到了解决方案,忘了更新这篇文章,这是通过改变线程文化
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;