如何利用 StreamWriter 写入 csv 文件?

发布于 2024-11-08 06:03:58 字数 1930 浏览 0 评论 0原文

这就是我正在做的事情。我正在尝试获取一个 XML 文件,从属性中提取信息,将它们附加在一起,然后将其写入 CSV 文件。我对编程还比较陌生,而另一位程序员今天不在办公室,所以我确实需要一些帮助。

我的第一个问题是关于 StringBuilder 的。我是否需要在 StringBuilder 的末尾添加一个 AppendLine,以便 foreach 循环输出的每个字符串都位于新行上?我需要在 foreach 循环内执行此操作吗?

我的第二个问题是关于将我的字符串实际写入 CSV 文件。它看起来会像什么吗?

swOutputFile.WriteLine(strAppendedJobData)

我认为这也会进入 foreach 循环,但我不太确定。

感谢您的帮助,我希望我以一种易于理解的方式表达了我的问题。

//Create a stream writer to write the data from returned XML job ticket to a new CSV
        StreamWriter swOutputFile;
        string strComma = ",";
        swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read));

        //Get nodes from returned XML ticket
        XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
        //Pull out data from XML attributes
        foreach (XmlElement xeJobUpdate in xmlJobs)
        {
            //Break down the job data
            string strProjectID = xeJobUpdate.GetAttribute("SharpOwlProjectID");
            string strJobNumber = xeJobUpdate.GetAttribute("JobNumber");
            string strClientCode = xeJobUpdate.GetAttribute("SharpOwlClientCode");
            string strClient = xeJobUpdate.GetAttribute("Client");
            string strVCAOffice = xeJobUpdate.GetAttribute("VCAOffice");
            string strLoadStatus = xeJobUpdate.GetAttribute("LoadStatus");

            //Build the string to be added to the new CSV file

            StringBuilder sbConcatJob = new StringBuilder();
            sbConcatJob.Append(strProjectID).Append(strComma).Append(strJobNumber)
                .Append(strComma).Append(strClientCode).Append(strComma).Append(strClient).Append(strComma)
                .Append(strVCAOffice).Append(strComma).Append(strLoadStatus).Append(strComma);
            string strAppendedJobData = sbConcatJob.ToString();

So here's what I'm working with. I'm trying to take an XML file, pull the info from the attributes, append them together, and write it to a CSV file. I'm still relatively new to programming, and the other programmer is out of the office today, so I could really use some assistance.

My first question, regards the StringBuilder. Do I need to have an AppendLine at the end of my StringBuilder, so that each string output from the foreach loop is on a new line? And would I need to do that inside the foreach loop?

My second question regards actually writing my string to the CSV file. Would it look something like?

swOutputFile.WriteLine(strAppendedJobData)

And I think this would also go inside the foreach loop, but I'm not too sure.

Thanks for the help, I hope I've worded my question in a somewhat easy to understand manner.

//Create a stream writer to write the data from returned XML job ticket to a new CSV
        StreamWriter swOutputFile;
        string strComma = ",";
        swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read));

        //Get nodes from returned XML ticket
        XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
        //Pull out data from XML attributes
        foreach (XmlElement xeJobUpdate in xmlJobs)
        {
            //Break down the job data
            string strProjectID = xeJobUpdate.GetAttribute("SharpOwlProjectID");
            string strJobNumber = xeJobUpdate.GetAttribute("JobNumber");
            string strClientCode = xeJobUpdate.GetAttribute("SharpOwlClientCode");
            string strClient = xeJobUpdate.GetAttribute("Client");
            string strVCAOffice = xeJobUpdate.GetAttribute("VCAOffice");
            string strLoadStatus = xeJobUpdate.GetAttribute("LoadStatus");

            //Build the string to be added to the new CSV file

            StringBuilder sbConcatJob = new StringBuilder();
            sbConcatJob.Append(strProjectID).Append(strComma).Append(strJobNumber)
                .Append(strComma).Append(strClientCode).Append(strComma).Append(strClient).Append(strComma)
                .Append(strVCAOffice).Append(strComma).Append(strLoadStatus).Append(strComma);
            string strAppendedJobData = sbConcatJob.ToString();

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

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

发布评论

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

评论(3

揪着可爱 2024-11-15 06:03:58

如果你想做得更优雅一点,你可以这样做:

 using(StreamWriter swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read)))
    {
        //Get nodes from returned XML ticket
        XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
        //Pull out data from XML attributes
        foreach (XmlElement xeJobUpdate in xmlJobs)
        {
           List<String> lineItems = new List<String>();
           lineItems.add(xeJobUpdate.GetAttribute("SharpOwlProjectID"));
           //add all the other items

           swOutputFile.WriteLine(String.Join(',', myLine.ToArray()));



        }
    //after the loop you close the writer
   }

    //all the work is done much easier

if you want to do it a bit more elegant you could do something like that:

 using(StreamWriter swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read)))
    {
        //Get nodes from returned XML ticket
        XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
        //Pull out data from XML attributes
        foreach (XmlElement xeJobUpdate in xmlJobs)
        {
           List<String> lineItems = new List<String>();
           lineItems.add(xeJobUpdate.GetAttribute("SharpOwlProjectID"));
           //add all the other items

           swOutputFile.WriteLine(String.Join(',', myLine.ToArray()));



        }
    //after the loop you close the writer
   }

    //all the work is done much easier
游魂 2024-11-15 06:03:58

我的第一个问题是关于
字符串生成器。我需要有一个
在我的末尾添加行
StringBuilder,让每个字符串
foreach 循环的输出位于
新线?我需要这样做吗
在 foreach 循环内?

我唯一的建议是尝试一下,因为看来您还没有尝试过。这是你学习的唯一方法。

swOutputFile.WriteLine(strAppishedJobData)

这会将整行文本写入文件。

My first question, regards the
StringBuilder. Do I need to have an
AppendLine at the end of my
StringBuilder, so that each string
output from the foreach loop is on a
new line? And would I need to do that
inside the foreach loop?

My only advice since it appears you have not attempted this would be to try it. It is the only way you will learn.

swOutputFile.WriteLine(strAppendedJobData)

This would write an entire line of text to a file.

月野兔 2024-11-15 06:03:58

这里确实有两个选择:

如果您在 foreach 循环内调用 sbConcatJob.AppendLine() ,您可以在一个字符串生成器中构建文件的内容,然后调用 swOutputFile.Write(sbConcatJob.ToString ()) 在 foreach 循环之外写入文件。

如果您保持代码不变,则可以在 foreach 循环内添加 sw.OutputFile.WriteLine(sbConcatJob.ToString()) 并一次写入文件一行。

You really have two options here:

If you call sbConcatJob.AppendLine() inside the foreach loop you can build the contents of the file in one string builder then call swOutputFile.Write(sbConcatJob.ToString()) outside of the foreach loop to write the file.

If you keep your code as it is now you can add sw.OutputFile.WriteLine(sbConcatJob.ToString()) inside the foreach loop and write the file one line at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文