当双值具有小数点时,双值将转换为字符串值

发布于 2025-01-23 22:51:15 字数 1642 浏览 4 评论 0原文

我正在尝试使用CSVHelper库将数据写入MemoryStream,然后使用该MemoryStream生成CSV文件。

问题是当双值具有小数点时,双值将转换为怪异的字符串值。预期的输出和怪异的输出在底部。

有人知道如何克服这个问题吗?还是以下代码中有任何错误?

public class Foo
{
    public Foo()
    {
    }

    public double valOne { get; set; }

    public double valTwo { get; set; }
}

public class FooMap : ClassMap<Foo>
{
    public FooMap()
    {
        Map(m => m.valOne).Index(0).Name("Val One");
        Map(m => m.valTwo).Index(1).Name("Val Two");
    }
}

var records = new List<Foo> {
    new Foo{valOne = 3224.12, valTwo = 4122},
    new Foo{valOne = 2030.20, valTwo = 5555},
};

var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ",", HasHeaderRecord = true };

using (var memoryStream = new MemoryStream())
using (var writer = new StreamWriter(memoryStream))
using (var csv = new CsvWriter(writer, config))
{
    csv.Context.RegisterClassMap<FooMap>();

    csv.WriteHeader<Foo>();

    csv.NextRecord();
    foreach (var record in records)
    {
        csv.WriteRecord(record);
        csv.NextRecord();
    }

    writer.Flush();
    
    var result = Encoding.UTF8.GetString(memoryStream.ToArray());

    byte[] bytes = Encoding.ASCII.GetBytes(result);

    return new FileContentResult(bytes, "text/csv")
    {
        FileDownloadName = "Sample_Report_Name"
    };
}

预期输出:

Val One, Val Two
3224.12,4122
2030.20,5555

怪异的输出:

Val One, Val Two
"3224,12",4122
"2030,20",5555

I am trying to use CSVHelper library to write data into a MemoryStream and then generate a CSV file using that MemoryStream.

The problem is Double values get converted to weird string values when the double values have decimal points. The expected output and weird output are there at the bottom.

Is anyone know how to overcome this issue? or Is there any mistake in the below code?

public class Foo
{
    public Foo()
    {
    }

    public double valOne { get; set; }

    public double valTwo { get; set; }
}

public class FooMap : ClassMap<Foo>
{
    public FooMap()
    {
        Map(m => m.valOne).Index(0).Name("Val One");
        Map(m => m.valTwo).Index(1).Name("Val Two");
    }
}

var records = new List<Foo> {
    new Foo{valOne = 3224.12, valTwo = 4122},
    new Foo{valOne = 2030.20, valTwo = 5555},
};

var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ",", HasHeaderRecord = true };

using (var memoryStream = new MemoryStream())
using (var writer = new StreamWriter(memoryStream))
using (var csv = new CsvWriter(writer, config))
{
    csv.Context.RegisterClassMap<FooMap>();

    csv.WriteHeader<Foo>();

    csv.NextRecord();
    foreach (var record in records)
    {
        csv.WriteRecord(record);
        csv.NextRecord();
    }

    writer.Flush();
    
    var result = Encoding.UTF8.GetString(memoryStream.ToArray());

    byte[] bytes = Encoding.ASCII.GetBytes(result);

    return new FileContentResult(bytes, "text/csv")
    {
        FileDownloadName = "Sample_Report_Name"
    };
}

Expected Output:

Val One, Val Two
3224.12,4122
2030.20,5555

Weird Output:

Val One, Val Two
"3224,12",4122
"2030,20",5555

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分開簡單 2025-01-30 22:51:15

问题是运行代码的计算机的当前文化使用逗号而不是时间来指示小数点。使用CultureInfo.InvariantCulture而不是culturenfo.currentcurtulture应解决格式问题。

另外,您可以使用csv.writerecords(Records)来简化代码。

var records = new List<Foo> {
    new Foo{valOne = 3224.12, valTwo = 4122},
    new Foo{valOne = 2030.20, valTwo = 5555},
};

var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ",", HasHeaderRecord = true };

using (var memoryStream = new MemoryStream())
using (var writer = new StreamWriter(memoryStream))
using (var csv = new CsvWriter(writer, config))
{
    csv.Context.RegisterClassMap<FooMap>();

    csv.WriteRecords(records);

    writer.Flush();
    
    var result = Encoding.UTF8.GetString(memoryStream.ToArray());

    byte[] bytes = Encoding.ASCII.GetBytes(result);

    return new FileContentResult(bytes, "text/csv")
    {
        FileDownloadName = "Sample_Report_Name"
    };
}

The issue is the CurrentCulture of the computer running the code uses commas instead of periods to indicate the decimal point. Using CultureInfo.InvariantCulture instead of CultureInfo.CurrentCulture should fix the formatting issue.

Also, you can simplify your code by using csv.WriteRecords(records).

var records = new List<Foo> {
    new Foo{valOne = 3224.12, valTwo = 4122},
    new Foo{valOne = 2030.20, valTwo = 5555},
};

var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ",", HasHeaderRecord = true };

using (var memoryStream = new MemoryStream())
using (var writer = new StreamWriter(memoryStream))
using (var csv = new CsvWriter(writer, config))
{
    csv.Context.RegisterClassMap<FooMap>();

    csv.WriteRecords(records);

    writer.Flush();
    
    var result = Encoding.UTF8.GetString(memoryStream.ToArray());

    byte[] bytes = Encoding.ASCII.GetBytes(result);

    return new FileContentResult(bytes, "text/csv")
    {
        FileDownloadName = "Sample_Report_Name"
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文