0

我试图将我的 C# 列表导出到 Csv 文件。一切都安排妥当。但问题是字段分隔符无法正常工作。它的显示就像我的字符串以“结尾(例如:0000324df”)。这是我的控制器代码。

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");

我需要知道如何适当地分隔字段值。任何人都可以帮助我。

4

1 回答 1

3
  writer.Write("\"");

这行代码每次都会输出一个“。为什么有它?

此外,我不会在 WriteLine 之前使用逗号,因为不需要分隔文件的结尾。

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");
于 2012-08-16T09:02:29.353 回答