【问题标题】:How would I write this to a csv file in c#?我将如何将其写入 c# 中的 csv 文件?
【发布时间】:2013-06-29 21:27:36
【问题描述】:

我希望有人可以帮助我。我是 c# 和一般编程的初学者,我正在尝试完成这个程序。基本上,它在 XML 文件中查找,获取特定标记的所有出现,并且应该写入文件名以及这两个标记的任何实例之间的任何内容。到目前为止,我已经尝试过 TextWriter、StreamWriter、FileStream 和其他一些,但没有做我想做的事。我意识到这可能是一个愚蠢的问题,但我是一个超级菜鸟,我的特殊情况需要帮助。我的代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        var files = from file in Directory.GetFiles("W:\\SRC\\hDefMl\\1.0\\Instrument_Files")
                    orderby file
                        ascending
                    select file;

        StringBuilder sb_report = new StringBuilder();

        string delimiter = ",";

        sb_report.AppendLine(string.Join(delimiter, "Module", "Generator(s)"));

        foreach (var file in files)
        {
            string filename = Path.GetFileNameWithoutExtension(file);

            Console.WriteLine("The HDefML file for {0} contains these EEPROM Generators:", filename);

            XDocument hdefml = XDocument.Load(file);

            var GeneratorNames = from b in hdefml.Descendants("Generators")
                                 select new
                                     {
                                         name = (string)b.Element("GeneratorName")
                                     };

            string description;

            foreach (var generator in GeneratorNames)
            {
                Console.WriteLine("   GeneratorName is: {0}", generator.name);
                sb_report.AppendLine(string.Join(delimiter, filename,
                generator.name));
            }


        }
    }

【问题讨论】:

  • 想要它究竟做什么? TextWriter 和 StreamWriter 是非常灵活的文件写入方式。
  • 有一个优秀的库“KentBoogard”专门编写 CSV 文件..查看
  • 我想将 Console.WriteLine 基本上写入的内容写入 csv 文件。我不怀疑 TextWriter 或 StreamWriter 我不能用它们作为我无能/缺乏经验的例子。 @screenmutt
  • @nowhewhomustnotbenamed。我宁愿不使用 3rd 方库,我的项目并不真正保证它。

标签: c# xml visual-studio-2010 linq xml-parsing


【解决方案1】:

如果您使用字符串生成器构建的字符串格式正确,您应该能够执行类似的操作。

    static void WriteToCSV(string str, string path)
    {
        using (Stream stream = File.Create(path))
        using (StreamWriter writer = new StreamWriter(stream))
        {
            writer.WriteLine(str);
        }
    }

【讨论】:

  • 对不起;这里面的str是什么?
  • str 是 .csv 文件的内容,看起来您在 StringBuilder sb_report 中构建字符串。所以调用 WriteToCSV(sb_report.ToString(), "test.csv");如果 sb_report 采用正确的 .csv 格式,则可以使用。
【解决方案2】:
try{
    FileStream FS;
    StreamWriter SW;

    using (FS = new FileStream("HardCodedFileName.csv", FileMode.Append))
    {
        using (SW = new StreamWriter(FS))
        {
            foreach (var generator in GeneratorNames)
            {
                SW.WriteLine(string.Join(delimiter, filename,
                generator.name));
            }
        }
    }
}
catch (Exception e){
    Console.Writeline(e.ToString());
}

【讨论】:

  • 只需将它添加到我目前所拥有的内容上? @amalgamate
  • 附带说明,您应该将 FileStream 和 StreamWriters 包装在“使用语句”中(就像 Zenchovey 所做的那样),here's why
  • 替换您的“foreach(GeneratorNames 中的 var 生成器)”块,但我遗漏了您的 Console.Writeline 语句,因此您可能需要重新添加。
  • @ry8806 很公平...我会修改...现在修改。
  • @amalgamate 当我使用这个时,我在 .csv 中得到的只是最后一个文件的名称是目录吗?有什么东西覆盖了它之前的东西吗?
猜你喜欢
  • 2016-05-13
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 2020-09-10
  • 2013-09-16
  • 1970-01-01
相关资源
最近更新 更多